Reputation: 9081
I have an asp.net application in which i have to call a javascript function when an event is fired .I tried this :
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());
}
}
Code behind of the user control
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);
}
}
I just need to display an alert message when the event is fired . When i launched the application i don't get any result but if i put the code of the event in the page load i will see the alert.
Upvotes: 0
Views: 321
Reputation: 11975
The recommended pattern to solve this problem is a user control. You are correct on this count. However, you've to take care of wiring stuff properly.
You can look at the following article for a guideline: http://www.codeproject.com/Articles/51671/How-To-Expose-Events-from-UserControl
The steps in short are:
To highlight the 4th point, say its a dropdown inside your user control which contains a list of names. You've defined an event, say NameChanged
as follows:
public event NameChangedHandler NameChanged;
In your aspx markup make sure your user control is defined as follows:
<uc1:FooControl runat="server" OnNameChanged="FooCallback"></uc1:FooControl>
Make note of the convention here; in the code behind you've declared event as NameChanged
. In your markup, the letters On
gets prefixed to it: hence OnNameChanged
.
Edit:
Here is an example app; its the same as described above.
<%@ 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);
}
}
}
Make note of how the "plumbing" is done. I've put AutoPostback="true"
for the dropdownlist; hence I can handle its SelectedIndexChanged
event in the code behind of the user control. This is where I can decide on my logic to raise an event.
<%@ 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...
}
}
}
Make note of the event callback on the target aspx webform.
Upvotes: 1
Reputation: 2542
Try this:
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "error", "alert('Hello');", true);
Upvotes: 0