我有一个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);
}
}
我只需要在触发事件时显示警告消息。当我启动应用程序时,我没有得到任何结果,但如果我把事件的代码放在页面加载中,我将看到警报。
答案 0 :(得分:1)
解决此问题的推荐模式是用户控件。你这个数字是正确的。但是,您必须正确处理布线。
您可以查看以下文章以获取指南:http://www.codeproject.com/Articles/51671/How-To-Expose-Events-from-UserControl
简短的步骤是:
要突出显示第4点,请在用户控件中包含一个包含名称列表的下拉列表。您已定义了一个事件,例如NameChanged
,如下所示:
public event NameChangedHandler NameChanged;
在您的aspx标记中,确保您的用户控件定义如下:
<uc1:FooControl runat="server" OnNameChanged="FooCallback"></uc1:FooControl>
在这里记下惯例;在您之后的代码中,已将事件声明为NameChanged
。在您的标记中,字母On
会为其添加前缀:OnNameChanged
。
编辑:
这是一个示例应用;它与上述相同。
<%@ 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>
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);
}
}
}
记下&#34;管道&#34;已经完成了。我已将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>
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上的事件回调。
答案 1 :(得分:0)
脚本标记应正确结束。检查结束脚本标记...
答案 2 :(得分:0)
尝试使用:
cs.RegisterStartupScript
答案 3 :(得分:0)
试试这个:
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "error", "alert('Hello');", true);