简体   繁体   English

查找嵌套在Repeater Control中的控件

[英]Find Controls nested inside Repeater Control

I'm trying to find the values of TextBoxes which are rendered in a Repeater though a UserControl, ie the Repeater has a Placeholder for the UserControl, and inside the UserControl is where the TextBox markup actually exists. 我试图找到通过 UserControl在Repeater中呈现的TextBoxes的值,即Repeater具有UserControl的占位符,而UserControl内部是TextBox标记实际存在的位置。 I've done this before with TextBoxes directly inside of a Repeater before, which was fairly straight forward, and I'm wondering why this apparently can't be accomplished the same way. 之前我已经用直接在 Repeater 内部的TextBoxes完成了这个,这是相当直接的,我想知道为什么这显然不能以同样的方式完成。 Here is the Default page with the Repeater, which contains a Placeholder... 这是带有Repeater的Default页面,其中包含一个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 the UserControl which is inserted in the place of the Placeholder... ...以及插入占位符位置的UserControl ...

 <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 button's OnClick, I always get a null value for any and all TextBoxes (and DropDowns) in the UserControl... ...但是当以下代码运行Display按钮的OnClick时,我总是得到UserControl中任何和所有TextBoxes(和DropDowns)的空值...

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's the best way to access these values? 访问这些值的最佳方法是什么? Thanks. 谢谢。

txtPercentage Textbox is located inside Usercontrol, so use the following helper method to retrieve it. txtPercentage Textbox位于Usercontrol内,因此请使用以下帮助程序方法来检索它。

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);
}

Usage 用法

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

您需要先获取UserControl, 然后在UC本身上使用FindControl

Try loading the placeholder first and then looking for the textboxes in the placeholder: 首先尝试加载占位符,然后在占位符中查找文本框:

Something like this: 像这样的东西:

pseudo

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

/pseudo /伪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM