To raise events of controls inside of a user control, just create a method in the code-behind of the usercontrol. That method then calls the individual methods of the controls. This is called "exposing". The main page then just calls that exposed method byMyUserControlName.MethodName()
Hi,
Thank you for your post!
Here,this is for you:Bubble Event from UserControl
Here is the full sample code:
<%@. Page Language="C#" %>
<%@. Register src="http://pics.10026.com/?src=TestUc.ascx" TagName="TestUc" 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">
protected void TestUc1_TestCustomEvent(object sender, CommandEventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
</script><html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<uc1:TestUc id="TestUc1" runat="server" OnTestCustomEvent="TestUc1_TestCustomEvent">
</uc1:TestUc>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TestUc1" EventName="TestCustomEvent" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html><%@. Control Language="C#" ClassName="TestUc" %>
<script runat="server">
public delegate void ClickEventHandler(object sender, CommandEventArgs e);
public event ClickEventHandler TestCustomEvent;
protected void lb_Command(object sender, CommandEventArgs e)
{
if (TestCustomEvent != null) TestCustomEvent(this, e);
}
</script><asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="LinkButton1" OnCommand="lb_Command">LinkButton1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="LinkButton2" OnCommand="lb_Command">LinkButton2</asp:LinkButton>
If you have further questions,let me know!
Best Regards,
No comments:
Post a Comment