从表单视图内的转发器内的文本字段中获取数据

时间:2011-06-23 12:36:42

标签: c# asp.net repeater objectdatasource formview

我想获取转发器内的文本框的值,该转发器位于窗体视图中,所有窗口视图都绑定到对象数据源。

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

但是我不能用它来在转发器中获取文本框它给我null 任何帮助??

3 个答案:

答案 0 :(得分:0)

您必须在转发器内找到文本框,就像您使用FormView1一样。

答案 1 :(得分: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;
}

答案 2 :(得分: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;
    }