查找嵌套在中继器控件中的控件 [英] Find Controls nested inside Repeater Control

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

问题描述

我试图找到在中继器中呈现的文本框的值虽然一个用户控件,即中继器有一个用户控件的占位符,而用户控件内部是文本框标记的实际位置存在.我之前用TextBoxes 直接在Repeater 的内部 做过这个,这是相当直接的,我想知道为什么这显然不能以同样的方式完成.这是带有中继器的默认页面,其中包含一个占位符...

 <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"><form class="formee"><字段集><legend>教师信息</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:标签 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></字段集></表格><div id="repeaterDiv"><asp:Repeater ID="rptBudget" runat="server" ClientIDMode="Static"><项目模板><asp:PlaceHolder ID="phBudget" runat="server" EnableViewState="true"/><br/></项目模板></asp:中继器><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>

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

...以及插入到占位符位置的 UserControl...

 <fieldset><legend>教师工资表</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></表></字段集>

...但是当为显示按钮的 OnClick 运行以下代码时,对于 UserControl 中的任何和所有 TextBox(和 DropDowns),我总是得到一个空值...

protected void DisplayEntries(object sender, EventArgs e){foreach(在 rptBudget.Items 中重复的RepeaterItem){TextBox txtPercentage = (TextBox)repeated.FindControl("txtPercentage");if (txtPercentage == null){lblCtrls.Text += " null; ";}别的{lblCtrls.Text += txtPercentage.Text + "; ";}}}

访问这些值的最佳方式是什么?谢谢.

解决方案

txtPercentage Textbox 位于 Usercontrol 内部,所以使用下面的 helper 方法来检索它.

辅助方法

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

用法

foreach(RepeaterItem 在 rptBudget.Items 中重复){文本框 txtPercentage =(TextBox)FindControlRecursive(repeated, "txtPercentage");...}

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>

...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 Textbox is located inside Usercontrol, so use the following helper method to retrieve 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);
}

Usage

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

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

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