chamar javascript no c#

alfinete

Power Member
estou em asp.net c#

code c#

Código:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using log4net;  
using Multicert.Products.SignatureTimestamp;
using System.Text.RegularExpressions; 

namespace Sabatica_DGRHE
{
  public partial class teste : System.Web.UI.Page
  {

    private static readonly ILog log = LogManager.GetLogger(typeof(teste));   
    protected System.Web.UI.WebControls.TextBox TextBox2;
    protected System.Web.IU.WebControls.Button Button1;
    protected System.Web.UI.WebControls.TextBox TextBox1;

    Hashtable parameters;
    SignatureTS obj;
    protected System.Web.UI.WebControls.TextBox TextBox3;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.Label Label3;
    VerifResult verRes;

    private void ShowMessage(string message)
    {
      String scriptString = "<script language=JavaScript>";
      scriptString += "alert('" + message + "');";
      scriptString += "</script>";
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "scrt", scriptString);
        //if (!this.IsStartupScriptRegistered("scrt"))
        //  Page.RegisterStartupScript("scrt", scriptString);
    }

    private void Page_Load(object sender, System.EventArgs e)
    {
     try
     {
      if (!IsPostBack )
        {
      parameters = new Hashtable();
      parameters.Add("configFile", "C:\\documents and Settings\\jaribeiro\\Ambiente de trabalho\\Sabatica 19-07-08\\Sabatica_DGRHE\\bin\\encrypted_signatureTS.cfg");
      parameters.Add("encrypted_configFile", true);    //pois o ficheiro de configuracao encontra-se cifrado
      obj = new SignatureTS(parameters);
      Button1.Attributes.Add("OnClick", "preenche()");
      return;
       }
     }
      catch (Exception ex) 
       {
         if (log.IsErrorEnabled)
           log.Error("Ocorreu um erro na assinatura digital ", ex);
       } 
    
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.Button1.Click += new System.EventHandler(this.Button1_Click);
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    protected void Button1_Click(object sender, EventArgs e)
    {
      string sSignatureVerificationCode;
      string sTSVerificationCode;

      if (TextBox2.Text != null && TextBox2.Text != "")
      {
        TSVerifResult tsRes = obj.CreateTimestamp(TextBox2.Text);
        if (tsRes.ResultCode == "0" && tsRes.HasTSDate == true)
        {
          TextBox3.Text = tsRes.TSResponse;
          Label1.Text = "tempo :" + tsRes.Time.ToString();
        }

        verRes = obj.Verify(TextBox2.Text, TextBox1.Text, null);

        sSignatureVerificationCode = verRes.SignatureVerificationCode;

        if (sSignatureVerificationCode == "0" && verRes.HasSignature == true)
        {
          Label2.Text = "Verificação de assinatura OK";
        }
        else
        {
          Label2.Text = "Verificação de assinatura ERROR";
        }
        if (TextBox3.Text != null && TextBox3.Text != "")
        {
          verRes = obj.Verify(null, TextBox2.Text, TextBox3.Text);
          sTSVerificationCode = verRes.TSVerificationCode;
          string ola = verRes.SignatureVerificationMessage;
          if (sTSVerificationCode == "0" && verRes.HasTS == true && verRes.HasTSDate == true)
          {
            Label3.Text = "Verificação de TimeStamping OK";
          }
          else
          {
            Label3.Text = "Verificação de TimeStamping ERROR";
          }
        }
      }
    }

    

 
  }
}
code aspx

Código:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="teste.aspx.cs" Inherits="Sabatica_DGRHE.teste" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    
    <object id="ActiveX"  codebase="CryptoX_Multicert.CAB#version=0,0,0,6" classid="CLSID:27426F7E-0E7E-442B-A56D-D42C08877159"    >
        </object>
        <script type="JavaScript">
            function assina( dados )
            {
                return( ActiveX.assina( dados ) );
            }
            function preenche()
            {
                var res, fields, field, parts, i;

                alert("Vou assinar os dados");

                res = assina( document.Form1.TextBox1.value );

                alert("Assinei os dados");
                alert(res);
                
                document.Form1.TextBox2.value = res;

                return true;
            }
            function printErrorMessage( res ) {
                if( res == "ERROR00" ) {
                    alert( "Necessita de introduzir os dados a assinar !" );
                }
                else {
                    alert( "Ocorreu um erro inesperado." );
                }
            }
        </script>
</head>
<body>
    <form id="Form1" method="post" runat="server">
            <asp:TextBox id="TextBox1" style="Z-INDEX: 100; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server"
                Width="395px" Height="24px"></asp:TextBox>
            <asp:Label id="Label3" style="Z-INDEX: 107; LEFT: 536px; POSITION: absolute; TOP: 56px" runat="server"
                Height="32px" Width="368px"></asp:Label>
            <asp:TextBox id="TextBox3" style="Z-INDEX: 103; LEFT: 520px; POSITION: absolute; TOP: 112px"
                runat="server" Height="333px" Width="504px" ReadOnly="True" TextMode="MultiLine"></asp:TextBox>
            <asp:TextBox id="TextBox2" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 112px" runat="server"
                Width="504px" Height="333px" ReadOnly="True" TextMode="MultiLine"></asp:TextBox>&nbsp;
            <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 456px" runat="server"
                Width="144px" Text="Button"  ></asp:Button>
            <asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 528px; POSITION: absolute; TOP: 456px" runat="server"
                Height="24px" Width="488px"></asp:Label>
            <asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 24px; POSITION: absolute; TOP: 56px" runat="server"
                Height="32px" Width="368px"></asp:Label>
        </form>
</body>
</html>
para funcionar tem de chamar a função preenche() javascript

mas não está a dar

e ja agra por passa duas vezs no load ao arranque

gostava de um help
 
Última edição:
mas eu so quero que ele chame preenche() , ao clicar no botão?

ous seja primeiro tem de passar pelo javascript para preencher a caixa depois passa pelo evento onclick do botão em c# para fazer o resto.


obrigada na mesma
 
esse problema ja esta resolvido , o prob era :

a tag script está <script type="jacascript">

e deve estar <script language="javascript" type="text/javascript">

agra tenho outro problema quando clico no botão ele passa sempre duas vezes no evento load da pagina, e so deve passar uma como posso resolver o prob
 
Back
Topo