Find controls embedded in repeater management system

I am trying to find the TextBoxes values ​​that are displayed in the Repeater, although the UserControl, i.e. Repeater has a Placeholder for the UserControl, and inside the UserControl there is a TextBox markup. Earlier, I did this with TextBoxes directly inside the repeater, which was pretty straight forward, and I wonder why this, apparently, cannot be achieved the same way. Here is the default repeater page containing Placeholder ...

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <form class="formee"> <fieldset> <legend>Faculty Information</legend> <div class="grid-4-12"> <asp:Label ID="lblFirstName1" runat="server" Text="First Name"></asp:Label> <asp:Label ID="lblFirstName2" runat="server" Text="" ></asp:Label> <asp:Label ID ="lblSalary" runat="server" Text="" ClientIDMode="Static"></asp:Label> </div> <div class="grid-6-12"> <asp:Label ID="lblLastName1" runat="server" Text="Last Name"></asp:Label> <asp:Label ID="lblLastName2" runat="server" Text=""></asp:Label> </div> </fieldset> </form> <div id="repeaterDiv"> <asp:Repeater ID="rptBudget" runat="server" ClientIDMode="Static"> <ItemTemplate> <asp:PlaceHolder ID="phBudget" runat="server" EnableViewState="true" /> <br /> </ItemTemplate> </asp:Repeater> <asp:Button ID="btnAddBudgetControl" runat="server" Text="Add" CausesValidation="false" OnClick="AddBudgetControl" CssClass="addBudgetControl"/> <asp:Button ID="btnDisplayEntries" runat="server" Text="Display Entries" CausesValidation="false" OnClick="DisplayEntries" /> </div> <div> <asp:TextBox ID="txtTotalPercent" runat="server" ClientIDMode="Static"></asp:TextBox> <asp:TextBox ID="txtGrandTotal" runat="server" ClientIDMode="Static"></asp:TextBox> <asp:Label ID="lblCtrls" runat="server" Text=""></asp:Label> </div> 

... and UserControl, which is inserted instead of Placeholder ...

  <fieldset> <legend>Faculty Salary Form</legend> <table cellspacing="10" id="values"> <tr> <td> <asp:Label ID="lblServiceType" runat="server" Text="Service"></asp:Label> <asp:DropDownList runat="server" ID="ddlServiceType" CssClass="serviceType" /> </td> <td> <asp:Label ID="lblSpeedCode" runat="server" Text="Speed Code"></asp:Label> <asp:DropDownList runat="server" ID="ddlSpeedCode" CssClass="speedType" /> </td> <td> <asp:Label ID="lblPercentage" runat="server" Text="Percentage"></asp:Label> <asp:Textbox ID="txtPercentage" runat="server" CssClass="percentCommitment" ClientIDMode="Static" EnableViewState="true" /> </td> <td> <asp:Label ID="lblTotal" runat="server" Text="Total"></asp:Label> <asp:TextBox ID="txtTotal" runat="server" CssClass="amountCommitment" ClientIDMode="Static" EnableViewState="true"/> </td> <td> <asp:Button ID="btnRemove" runat="server" Text="Remove Item" OnClick="RemoveItem" ClientIDMode="Static" CssClass="btnRemove" /> </td> </tr> <tr> </tr> </table> </fieldset> 

... but when the following code runs for the Display OnClick button, I always get a null value for all and all text fields (and DropDowns) in UserControl ...

 protected void DisplayEntries(object sender, EventArgs e) { foreach (RepeaterItem repeated in rptBudget.Items) { TextBox txtPercentage = (TextBox)repeated.FindControl("txtPercentage"); if (txtPercentage == null) { lblCtrls.Text += " null; "; } else { lblCtrls.Text += txtPercentage.Text + "; "; } } } 

What is the best way to access these values? Thanks.

+4
source share
3 answers

The txtPercentage text field is inside the Usercontrol, so use the following helper method to get it.

Helper method

 public static Control FindControlRecursive(Control root, string id) { if (root.ID == id) return root; return root.Controls.Cast<Control>() .Select(c => FindControlRecursive(c, id)) .FirstOrDefault(c => c != null); } 

Using

 foreach (RepeaterItem repeated in rptBudget.Items) { TextBox txtPercentage = (TextBox)FindControlRecursive(repeated, "txtPercentage"); ... } 
+10
source

You need to get UserControl first and then use FindControl in UC itself.

+1
source

First try loading the placeholder, and then find the text fields in the placeholder:

Something like that:

pseudo

 var ph = repeated.FindControl("phBudget"); var txtBox = ph.FindControl("txtPercentage"); 

/pseudo

+1
source

All Articles