2011-06-23 100 views
0

我想要獲取表單視圖中的所有綁定到對象數據源的中繼器內的文本框的值。從表單視圖中的中繼器中獲取數據

<asp:FormView ID="FormView1" runat="server" AllowPaging="True" 
    DataKeyNames="Id" EnableViewState="False" 
    OnPageIndexChanging="FormView1_PageIndexChanging" 
    onitemupdated="FormView1_ItemUpdated" 
    OnItemUpdating="FormView1_ItemUpdating" ondatabound="FormView1_DataBound"> 
    <ItemTemplate> 
     <asp:TextBox ID="txtProdName" runat="server" Text='<%#Eval("ManufacturerProductName") %>'></asp:TextBox> 
     <asp:Repeater ID="Repeater1" runat="server" DataSource='<%#DataBinder.Eval(Container.DataItem,"Distributors") %>'> 
      <ItemTemplate> 
       <asp:TextBox ID="TextBox1" runat="server" ontextchanged="onTextChanged" Text='<%# DataBinder.Eval(Container.DataItem, "FobCost")%>'></asp:TextBox> 
       <asp:Repeater ID="Repeater2" runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem,"PricingsheetWarehouses") %>'> 
        <ItemTemplate> 
         <asp:TextBox ID="TextBox2" runat="server" ontextchanged="onTextChanged" Text='<%# DataBinder.Eval(Container.DataItem, "DeliveredCost")%>'></asp:TextBox> 
        </ItemTemplate> 
       </asp:Repeater> 
      </ItemTemplate> 
     </asp:Repeater> 
    </ItemTemplate> 
</asp:FormView> 

我得到txtProdName因爲這

TextBox t=FormView1.FindControl("txtProdname")as textBox; 

,但我不能用它來得到它給我空 任何幫助中繼器內文本框

回答

0

你必須找到中繼器本身的文本框,就是你如何使用FormView1

0

嘗試

Repeater repeater1=FormView1.FindControl("Repeater1")as Repeater; 

protected void RptrSupplier_ItemDataBound(Objectsender,System.Web.UI.WebControls.RepeaterItemEventArgs e) 
{ 
    // Only process items (not footers or headers) 
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
      TextBox t = e.Item.FindControl("TextBox1") as TextBox; 
      t.ID = ((MyType)e.Item.DataItem).ID.ToString(); //you should cast to the type of your object 
      t.TextChanged += txt1_TextChanged; 
    } 
} 

protected void txt1_TextChanged(object sender, EventArgs e) 
{ 
    TextBox t = sender as TextBox; 
    var tempobject = MyCollection.Where(C => C.ID == t.ID).Single(); 
    tempobject.Prop = t.Text; 
} 
+0

是啊,這一個返回T作爲空... – user780975

+0

在此情況下不把這個碼?第一條語句必須在'FormView'綁定事件中,第二條語句在'repeater'綁定事件中。 –

+0

我編輯我的答案檢查它。 –

0

使用遞歸搜索來查找控制,不管它有多深:

public static Control FindControlRecursive(Control control, string id) 
    { 
     if (control == null) return null; 
     Control ctrl = control.FindControl(id); 
     if (ctrl == null) 
     { 
      foreach (Control child in control.Controls) 
      { 
       ctrl = FindControlRecursive(child, id); 
       if (ctrl != null) break; 
      } 
     } 
     return ctrl; 
    } 
+0

thnx我試試這個我找到控制文本框,但它給我的舊值不編輯一個... – user780975