2013-03-29 42 views
4

我試圖找到在Repeater中呈現的TextBoxes的值雖然 UserControl,即Repeater具有UserControl的佔位符,並且UserControl內部是TextBox標記實際上存在。我之前使用TextBoxes 直接在Repeater的之前完成了這項工作,這很直接,我想知道爲什麼這顯然無法以同樣的方式完成。下面是與中繼器,其中包含一個佔位符的默認頁面...查找嵌套在Repeater Control中的控件

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

... ...和用戶控件被插入佔位符的地方...

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

...但是當下面的代碼運行顯示按鈕的OnClick時,我總是得到UserControl中的任何和所有文本框(和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 + "; "; 
     } 
    } 
} 

訪問這些值的最佳方法是什麼?謝謝。

+0

快速的問題:爲什麼你的佔位符。爲什麼不直接將UserControl直接添加到'ItemTemplate'? –

回答

10

txtPercentage文本框位於內Usercontrol,因此使用以下幫助器方法來檢索它。

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"); 
    ... 
} 
+0

@nueverest你能詳細解釋一下嗎? – Win

+2

@Win看來我錯了。我認爲在最糟糕的情況下,解決方案需要迭代頁面上的所有控件。現在我仔細看看,看起來是由我來選擇'root'/scope是爲了一個特定的id。 –

1

您需要先在UC本身上獲得UserControl,然後然後使用FindControl

1

首先嚐試加載佔位符,然後尋找佔位符的文本框:

事情是這樣的:

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

/僞