Accessing input values across user controls
-
I have an ASP.NET page with a form that contains two user controls:
<%@ Register TagPrefix="x" TagName="c1" Src="ctl1.ascx" %> <%@ Register TagPrefix="x" TagName="c2" Src="ctl2.ascx" %> <body> <form runat="Server"> <x:c1 id="con1" runat="Server"> <x:c2 id="con2" runat="Server"> <form> </body>
In one control (
con1
) I have a dropdownlist. In the other control (con2
), I have a set of textboxes that I would like to pre-populate with values that depend on what item is selected in the dropdownlist in the first control. How can the code forcon2
access the value of the dropdownlist incon1
? Thanks for any help or suggestions. -----G -
I have an ASP.NET page with a form that contains two user controls:
<%@ Register TagPrefix="x" TagName="c1" Src="ctl1.ascx" %> <%@ Register TagPrefix="x" TagName="c2" Src="ctl2.ascx" %> <body> <form runat="Server"> <x:c1 id="con1" runat="Server"> <x:c2 id="con2" runat="Server"> <form> </body>
In one control (
con1
) I have a dropdownlist. In the other control (con2
), I have a set of textboxes that I would like to pre-populate with values that depend on what item is selected in the dropdownlist in the first control. How can the code forcon2
access the value of the dropdownlist incon1
? Thanks for any help or suggestions. -----Gu must public the dropdownlist for your form and use it. for example: public DropDownList drop1 { get{return yourdropdownlistid;} } and use con1.drop1.SelectedItem in your form code -------------------------------------------------------- or define a dropdownlist in con2 for example: private DropDownList DropDownList1; public DropDownList con1dropdownList { get{return this.DropDownList1;} set{DropDownList1 = value;} } and in form use from: con2.con1dropdownList = con1.drop1; We Can Do Anything, If We Want It
-
u must public the dropdownlist for your form and use it. for example: public DropDownList drop1 { get{return yourdropdownlistid;} } and use con1.drop1.SelectedItem in your form code -------------------------------------------------------- or define a dropdownlist in con2 for example: private DropDownList DropDownList1; public DropDownList con1dropdownList { get{return this.DropDownList1;} set{DropDownList1 = value;} } and in form use from: con2.con1dropdownList = con1.drop1; We Can Do Anything, If We Want It
Well, that isn't right, because the crux of the problem is I'm working within the code-behind page of a user control, not the main page. As a result, I don't have direct accss to con2 or con1 as properties. So I can't use, for example "con1.drop1.SelectedItem", because it will say "con1 not defined". And if I try something like "Page.con1.drop1.SelectedItem" it will say that con1 is not a property of Page. I actually found my answer, though, just FYI: I needed to use the Page.FindControl() method, which solved my problem.