Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Howto add eventhandler to web custom usercontrol?

Reply
Thread Tools

Howto add eventhandler to web custom usercontrol?

 
 
jimmy.liang@gmail.com
Guest
Posts: n/a
 
      05-16-2006
hi,

Is there any way to add eventhandler to custom usercontrol and could
set it at webForm?

i can add eventhandler on code, such as at Page_Load, add:
MyControl1.SelectedIndexChanged = this.OnSelectedIndexChange;

but, if i want to add eventhandler at webForm, like:
<uc1:MyControl ID="MyControl1" runat="server"
SelectedIndexChanged="OnSelectedIndexChange" />

it will genarel a compile error : cannot create an object of type
'System.EventHandler' from string. How can I do that?

thanks,


Detail as blow:
-----------------------
MyControl.ascx
-----------------------
<%@ Control Language="C#" ClassName="MyControl" %>
<script runat="server">
public EventHandler SelectedIndexChanged;
public string SelectedValue
{
get
{
if (DropDownList1.SelectedIndex < 0)
return "";
else
return DropDownList1.SelectedValue;
}
}

protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
if (SelectedIndexChanged != null)
SelectedIndexChanged(this, null);
}
</script>

<aspropDownList ID="DropDownList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedInde xChanged"
AutoPostBack=true>
<asp:ListItem Value=1 Text="item 01"></asp:ListItem>
<asp:ListItem Value=2 Text="item 02"></asp:ListItem>
<asp:ListItem Value=3 Text="item 03"></asp:ListItem>
</aspropDownList>


-----------------------------
MyTestControl.aspx
-----------------------------
<%@ Page Language="C#" %>
<%@ Register Src="Controls/MyControl.ascx" TagName="MyControl"
TagPrefix="uc1" %>

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

<script runat="server">
void Page_Load(object sender, EventArgs e)
{
//MyControl1.SelectedIndexChanged = this.OnSelectedIndexChange;
}

void OnSelectedIndexChange(object sender, EventArgs e)
{
Label1.Text = "My selection is: " + MyControl1.SelectedValue;
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>My Control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:MyControl ID="MyControl1" runat="server"
SelectedIndexChanged="OnSelectedIndexChange"
/> <hr />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>

 
Reply With Quote
 
 
 
 
jimmy.liang@gmail.com
Guest
Posts: n/a
 
      05-16-2006
OK, i make it like blow and it just work.
---------

code in MyControl.ascx
....
void Page_Load(object sender, EventArgs e)
{
if (strEvent != null && strEvent != "")
{
Delegate d = Delegate.CreateDelegate(typeof(EventHandler),
Page, strEvent);
SelectedIndexChanged = (EventHandler)d;
}

}

private string strEvent;
private EventHandler SelectedIndexChanged;

public string OnSelectedIndexChanged
{
set { strEvent = value; }
}
....

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add EventHandler to AudioClip? SimonSimon Java 1 05-08-2006 08:19 PM
How to add items to a dropdownlist control within a Javascript eventhandler? Oscar ASP .Net 6 07-31-2005 09:17 PM
Re: Need to add EventHandler to control created at run time Karl ASP .Net 0 08-24-2004 10:43 PM
how can HyperLink add a OnClick eventHandler? yesming2002 ASP .Net Web Controls 1 06-18-2004 09:40 PM
Add an eventhandler on a button_click =?Utf-8?B?QWxlc3NhbmRybyBSb3NzaQ==?= ASP .Net 1 02-17-2004 07:53 PM



Advertisments