2012-01-07 50 views
1

我做了一個菜單列表。它由兩個中繼器組成,一箇中繼器和productType,另一箇中間器件包含該產品類型的內容。 可以在文本框中輸入您想要的內容數量,現在我想查找文本框及其內容。查找另一箇中繼器內的中繼器內的文本框

這是我的ASP.NET代碼看起來像:

<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_ItemDataBound"> 
     <ItemTemplate> 
      <h2> 
       <%#DataBinder.Eval(Container.DataItem, "typenavn") %></h2> 
      <asp:HiddenField ID="HiddenField1" Value='<%# Eval("id") %>' runat="server" /> 
      <asp:Repeater ID="ChildRepeater" runat="server"> 
       <ItemTemplate> 
        <table> 
         <tr> 
          <td style="width: 400px"> 
           <%#DataBinder.Eval(Container.DataItem, "productName") %> 
          </td> 
          <td style="width: 400px"> 
           <%#DataBinder.Eval(Container.DataItem, "pris") %> 
          </td> 
          <td> 
           <asp:HiddenField ID="HiddenField2" runat="server" /> 
           <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> 
          </td> 
         </tr> 
        </table> 
       </ItemTemplate> 
      </asp:Repeater> 
     </ItemTemplate> 
    </asp:Repeater> 

這是我試圖到目前爲止做:

Repeater ChildRepeater; 

      foreach (RepeaterItem item1 in ParentRepeater.Items) 
      { 
       if (item1.ItemType == ListItemType.Item || item1.ItemType == ListItemType.AlternatingItem) 
       { 
        ChildRepeater = (Repeater)item1.FindControl("ChildRepeater"); 

        foreach (RepeaterItem item2 in ChildRepeater.Items) 
        { 
         if (item2.ItemType == ListItemType.Item || item2.ItemType == ListItemType.AlternatingItem) 
         { 

          TextBox txt = (TextBox)item2.FindControl(("MainContent_ParentRepeater_ChildRepeater_0_HB1_0")) as TextBox; // MainContent_ParentRepeater_ChildRepeater_0_HB 

         } 
        } 
       } 
       break; 
      } 

首先進入的parentrepeater和進入它的chilrepeaters。 但它不能找到我的文本框。

任何機構都有什麼想法?

+0

你有什麼你的ViewState設置爲..它是假的..如果是這樣嘗試使它ViewState = true;還有什麼EventHandler你檢查這.. ..? – MethodMan 2012-01-07 19:31:55

+0

看起來你應該試圖找到與TextBox TextBox txt = item2.FindControl(「TextBox1」)的文本框; - 不知道你從哪裏獲得'MainContent_ParentRepeater_ChildRepeater_0_HB1_0'? – TheGeekYouNeed 2012-01-08 12:36:45

回答

1
foreach (RepeaterItem item1 in Repeater.Items) 
{ 
    if (item.ItemType == ListItemType.Item) 
    { 
    TextBox txt = (TextBox)item.FindControl(("MainContent_ParentRepeater_ChildRepeater_0_HB1_0")) as TextBox; 
    // do something with "myTextBox.Text" 
    break; 
    } 
} 

你必須尋找在的RepeaterItem文本框。讓你無論是處理內部Repeater的ItemDataBound事件,或者你只是遍歷所有RepeaterItems:

foreach(RepeaterItem item in ChildRepeater.Items){ 
    if(item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem){ 
    var txt = (TextBox)item.FindControl("MainContent_ParentRepeater_ChildRepeater_0_HB1_0"); 
    } 
} 
+0

我不能讓它工作...當我嘗試從代碼隱藏findcontrolcontrol texztbox它仍然爲空 – Oedum 2012-01-07 23:25:20

0

嘗試在這個類中的兩種方法之一:(將這個類在App_Code文件)

using System.Web; 
using System; 
using System.Web.UI; 
using System.Web.UI.WebControls; 


/// <summary> 
/// Summary description for ControlHelper 
/// </summary> 
public static class ControlHelper 
{ 
    // Example: HtmlForm form = ControlHelper.FindControlRecursive(this.Master, "form1") as HtmlForm; 
    /// <summary> 
    /// Finds a Control recursively. Note finds the first match and exits 
    /// </summary> 
    /// <param name="ContainerCtl"></param> 
    /// <param name="IdToFind"></param> 
    /// <returns></returns> 
    public static Control FindControlRecursive(this Control Root, string Id) 
    { 
     if (Root.ID == Id) 
      return Root; 

     foreach (Control Ctl in Root.Controls) 
     { 
      Control FoundCtl = FindControlRecursive(Ctl, Id); 
      if (FoundCtl != null) 
       return FoundCtl; 
     } 

     return null; 
    } 

    //ModifyControl<TextBox>(this, tb => tb.Text = "test"); 
    public static void ModifyControl<T>(this Control root, Action<T> action) where T : Control 
    { 
     if (root is T) 
      action((T)root); 
     foreach (Control control in root.Controls) 
      ModifyControl<T>(control, action); 
    } 
} 

你使用FindControlRecursive()來查找特定的文本框,並且您將使用ModifyControl修改/對所有文本框執行某些操作。

相關問題