簡體   English   中英

調用javascript函數&&處理事件

[英]calling a javascript function && handling an event

我有一個asp.net應用程序,當事件被觸發時,我必須在其中調用javascript函數。

protected Consultation controlconsultation  = new Consultation();
 protected void Page_Load(object sender, EventArgs e)
        {

           controlconsultation.imageinfo += controlconsultation_imageinfo;
           Session["controlconsultation"] = controlconsultation;
       }

        void controlconsultation_imageinfo(object sender, CommandEventArgs e)
        {String csName = "myScript";
            Type csType = this.GetType();

            // Get a ClientScriptManager reference from the Page class.
            ClientScriptManager cs = Page.ClientScript;

            // Check to see if the client script is already registered.
            if (!cs.IsClientScriptBlockRegistered(csType, csName))
            {
                StringBuilder csText = new StringBuilder();
                csText.Append("<script type=\"text/javascript\"> ");
                csText.Append("alert(" + "Espace_Candidat/InfoEdition.ascx" +"); </");
                csText.Append("script>");
                cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
            }
        }

用戶控件后面的代碼

public event CommandEventHandler imageinfo ; 
protected void Page_Load(object sender, EventArgs e)
        {
 Consultation current = (Consultation)Session["controlconsultation"];
                imageinfo = current.imageinfo;
       }
  protected void Valider (object sender, CommandEventArgs e)
          {
            if (imageinfo != null)
              {
                  string pageNumber = (string)e.CommandArgument;
                  CommandEventArgs args = new CommandEventArgs("Control", pageNumber);
                  imageinfo(this, args);
              }
          }

事件觸發時,我只需要顯示一條警報消息即可。 當我啟動該應用程序時,我沒有得到任何結果,但是如果我將事件代碼放在頁面加載中,我將看到警報。

  • 那么,如何在每次事件發生時更改代碼以顯示警報?

解決此問題的推薦模式是用戶控件。 您認為這一點是正確的。 但是,您必須正確地布線。

您可以查看以下文章以獲取指南: http : //www.codeproject.com/Articles/51671/How-To-Expose-Events-from-UserControl

簡短的步驟是:

  1. 您定義一個委托
  2. 使用上面的委托在該用戶控件上定義一個事件
  3. 在后面的用戶控制代碼中編寫必要的管道代碼
  4. 請注意在aspx標記中連接代碼。

要突出顯示第四點,請說出它在用戶控件內的下拉列表,其中包含名稱列表。 您已經定義了一個事件,說出NameChanged如下:

public event NameChangedHandler NameChanged;

在aspx標記中,確保您的用戶控件定義如下:

<uc1:FooControl runat="server" OnNameChanged="FooCallback"></uc1:FooControl>

在這里記下約定; 在您將事件聲明為NameChanged之后的代碼中。 在您的標記中,字母On被添加了前綴: OnNameChanged

編輯:

這是一個示例應用程序; 其與上述相同。

MyUserControl.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="WebApp.UserControlExample.MyUserControl" %>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem>Jim</asp:ListItem>
    <asp:ListItem>John</asp:ListItem>
    <asp:ListItem>Rosemary</asp:ListItem>
    <asp:ListItem>Catherine</asp:ListItem>
</asp:DropDownList>

MyUserControl.ascx.cs:

using System;

namespace WebApp.UserControlExample
{
    public delegate void NameChangedEventHandler(string name);
    public partial class MyUserControl : System.Web.UI.UserControl
    {
        public event NameChangedEventHandler NameChanged;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (NameChanged != null)
                NameChanged(DropDownList1.SelectedValue);
        }
    }
}

記下“管道”的完成方式。 我在下拉列表中添加了AutoPostback="true" 因此,我可以在用戶控件后面的代碼中處理其SelectedIndexChanged事件。 在這里,我可以決定引發事件的邏輯。

WebForm1.aspx的:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.UserControlExample.WebForm1" %>

<%@ Register src="MyUserControl.ascx" tagname="MyUserControl" tagprefix="uc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
        <uc1:MyUserControl ID="MyUserControl1" runat="server" OnNameChanged="MyUserControl1_NameChanged" />
        <br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </form>
</body>
</html>

WebForm1.aspx.cs中:

using System;

namespace WebApp.UserControlExample
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void MyUserControl1_NameChanged(string name)
        {
            Label1.Text = "Selected name is <b>" + name + "</b>";
            //you probably want to call your ClientScript.RegisterStartupScript in here...
        }
    }
}

記下目標aspx網絡表單上的事件回調。

腳本標記應正確結束。 檢查結束腳本標簽...

嘗試使用:

cs.RegisterStartupScript

嘗試這個:

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "error", "alert('Hello');", true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM