vb.net - RaiseEvent between User Controls where there are multiple instances of the same controls on one page ASP.net -
i have problem i'm trying send string between user controls, "input.ascx" , "output.ascx", when have more 1 of each user control.
here aspx parent page:
<uc:input runat="server" id="input1" /> <uc:input runat="server" id="input2" /> <uc:output runat="server" id="output1" /> <uc:output runat="server" id="output2" />
user control input ascx:
<asp:textbox id="textbox1" runat="server"></asp:textbox> <asp:button id="button1" runat="server" text="button" />
user control input vb.net:
public shared event button1click(byval s string) protected sub button1_click(sender object, e eventargs) handles button1.click dim s string = textbox1.text raiseevent button1click(s) end sub
user control output ascx:
<%@ reference control="~/input.ascx" %> <asp:label id="label1" runat="server" text="label"></asp:label>
user control output vb.net:
public sub new() addhandler uctestucinput.button1click, addressof displaytext end sub private sub displaytext(byval s string) label1.text = s end sub
the problem when input either 1 of "input.ascx" gets displayed in both "output.ascx" when should displayed in corresponding output (like input1 corresponds output1).
the solution found problem old article 2002 on code project. here's link article asp.net user controls - notify 1 control of changes in other control. after reading article , looking through sample code, here following changes made code above.
there no changes aspx files except following line in output aspx file should removed.
<%@ reference control="~/input.ascx" %>
first add new class called labelchangeeventargs.
public class labelchangeeventargs inherits eventargs private _labelstr string public property labeltext() string return _labelstr end set(value string) _labelstr = value end set end property end class
user control input vb.net: add delegate , use new lablechangeeventargs class handle string.
public delegate sub labelchangeeventhandler(byval sender object, byval e labelchangeeventargs) partial class ucinput inherits system.web.ui.usercontrol public event onlabelchanged labelchangeeventhandler protected sub onbuttonclick(sender object, e eventargs) dim args new labelchangeeventargs args.labeltext = textbox1.text raiseevent onlabelchanged(me, args) end sub end class
user control output vb.net: make sub receive args , change label.
public sub labelchange(sender object, args labelchangeeventargs) if args isnot nothing label1.text = args.labeltext end if end sub
and in parent page codefile wire controls together.
protected sub wirehandles() handles me.init addhandler ucinput1.onlabelchanged, addressof ucoutput1.labelchange addhandler ucinput2.onlabelchanged, addressof ucoutput2.labelchange end sub
here's download link src files link
Comments
Post a Comment