查找嵌套Repeater控件内部控制 [英] Find Controls nested inside Repeater Control

查看:132
本文介绍了查找嵌套Repeater控件内部控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图寻找这是在一个中继器的渲染文本框的值,虽然的一个用户控件,即中继器具有为用户控件的占位符,该用户控件里面就是文本框标记实际上存在。我曾与文本框的之前做过直接在的一个中继器,这是相当简单的,我想知道为什么这显然不能以同样的方式来完成的。下面是转发器,其中包含一个占位符...

I'm trying to find the values of TextBoxes which are rendered in a Repeater though a UserControl, i.e. the Repeater has a Placeholder for the UserControl, and inside the UserControl is where the TextBox markup actually exists. 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. Here is the Default page with the Repeater, which contains a 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...

 <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>

...但是,当以下code为显示按钮的OnClick运行时,我总是对任何与空值在该用户的所有文本框(和下拉菜单)...

...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...

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文本框位于内用户控件,所以请使用以下助手方法来检索它。

txtPercentage Textbox is located inside Usercontrol, so use the following helper method to retrieve it.

helper方法

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

用法

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

这篇关于查找嵌套Repeater控件内部控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆