2012-01-07 15 views
1

私はメニューリストを作った。これは、2つのリピーターで構成されています。一方はproductType、もう一方はその製品タイプの内容です。 テキストボックスに必要なコンテンツの数を入力することができます。これで、テキストボックスとそのコンテンツを探したいと思っています。別のリピータの中のリピータ内のテキストボックスを見つける

これは私のASP.NETコードがどのように見えるかです:

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に入ると、そこにい行く:これは私がこれまでに行うことを試みたものです

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

chilrepeaters。 しかし、それは私のテキストボックスを見つけることができません。

体型とアイデアは何ですか?あなたはRepeaterItemでのTextBoxを検索する必要が

+0

あなたのViewStateに何を設定していますか?それは偽です。そうなら、ViewState = true;どのEventHandlerでこれをチェックしていますか..? – MethodMan

+0

TextBox txt = item2.FindControl( "TextBox1")でTextBoxをTextBoxとして検索しようとしているようです。 - 「MainContent_ParentRepeater_ChildRepeater_0_HB1_0」がどこから届いているのかわからない – TheGeekYouNeed

答えて

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

または

。だから、内側の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

私はそれを動作させることができません...私はcodebehindからtexztboxをfindcontrolしようとするとまだヌルです – Oedum

0

は、このクラスの2つの方法のいずれかを試してください(に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()を使用して特定のTextBoxを探し、ModifyControlを使用してTextBoxのすべてを変更/実行します。

関連する問題