How to find nested controls within nested user controls
I have a user control (Control1) which has a placeholder that may contain several additional user controls (of the same type - see below) which are added dynamically. How do I navigate the user control hierarchy to find the values of the nested sets of controls when the button located in Control 1 is clicked?
Control 1:
<%@ Control Language="C#" AutoEventWireup="True" CodeBehind="Control1.ascx.cs" Inherits="Control1" %>
<%@ Reference Control="Control2.ascx" %>
<div id="div1">
<div id="divPh"><asp:PlaceHolder ID="phControls" runat="server" /></div>
<div id="divContinue"><asp:Button ID="btnContinue" Text="Continue" OnClick="submit_Click" runat="server" /></div>
</div>
Code behind for Control1.aspx:
protected void submit_Click(object sender, EventArgs e)
{
// iterate through list of divControl2 controls and find out which radio button is selected
// for example, there may be 3 divControl2's which are added to the placeHolder on Control1, rdoBth1 might be selected on 2 of the controls
// and rdoBtn2 might be selected on 1 - how do I navigate this control structure?
}
Control 2 (Several of these may be added to the placeHolder on Control开发者_如何学运维1):
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Control2.ascx.cs" Inherits="Control2" %>
<div id="divControl2">
<p><strong><asp:RadioButton ID="rdoBtn1" GroupName="Group1" Checked="true" runat="server" /> Check this</strong></p>
<p><strong><asp:RadioButton ID="rdoBtn2" GroupName="Group1" Checked="false" runat="server" /> No, check this one</strong></p>
</div>
Check below code and let me know if you have any queries.
protected void submit_Click(object sender, EventArgs e)
{
for (int count = 0; count < phControls.Controls.Count; count++)
{
UserControl uc = (UserControl)(phControls.Controls[count]);
if (uc != null)
{
RadioButton rdoBtn1 = new RadioButton();
RadioButton rdoBtn2 = new RadioButton();
rdoBtn1 = (RadioButton)(uc.FindControl("rdoBtn1"));
rdoBtn2 = (RadioButton)(uc.FindControl("rdoBtn2"));
if (rdoBtn1.Checked == true)
{
Response.Write("1st checked ");
}
else if (rdoBtn2.Checked == true)
{
Response.Write("2nd checked");
}
}
}
This isn't the best design in the world, but you can accomplish what you're looking for with some relative ease. The problem is that the page where these controls are will have to have intimate knowledge of the inner workings of the dynamically added controls. And, you're going to want them to implement a common abstract class or interface so that you can look for the right ones by type.
The following code assumes that you've created properties for accessing the internal controls values rather than having to reference the internal controls yourself. This is just good practice when you use any kind of control.
protected void submit_Click(object sender, EventArgs e) {
foreach (var control in phControls.Controls) {
IMySpecialControl mySpecialControl = control as IMySpecialControl;
if (mySpecialControl != null) {
// access some properties (and probably need a cast to the specific control type :(
}
}
}
Instead, why not just access the fields via Request.Form collection instead?
string rdoBtn1Value = Request.Form["rdoBtn1"];
精彩评论