调用javascript函数&&处理事件
本文关键字:处理事件 函数 javascript 调用 | 更新日期: 2023-09-27 18:03:06
我有一个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
简短的步骤是:
- 你定义了一个委托
- 使用上面的委托 在用户控件上定义一个事件在后面的用户控制代码中编写必要的管道代码
- 注意在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
事件。在这里我可以决定触发事件的逻辑。
<%@ 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 webform上的事件回调。
标题>标题>标题>标题>Script标签应该正确结束。
尝试使用:
cs.RegisterStartupScript
试试这个:
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "error", "alert('Hello');", true);