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

发布于 2024-11-16 23:21:11 字数 1640 浏览 4 评论 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;

,但我不能用它来获取 文本框 内的转发器它给我 null 有什么帮助吗?

i want to get the value of the textboxes inside the repeater which is inside a form view all binded to object datasource .

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

i get txtProdName as this

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

but i cannot use it to get textboxes inside the repeater its giving me null
any help ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瑶 2024-11-23 23:21:11

您必须找到中继器本身内部的文本框,就像您使用 FormView1 所做的那样。

You'll have to find the textbox inside of the repeater itself, just how you're doing it with the FormView1.

极致的悲 2024-11-23 23:21:11

使用递归搜索来查找控件,无论它有多深:

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

Use a recursive search to find the control no matter how deep it goes:

    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;
    }
羁拥 2024-11-23 23:21:11

尝试

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

try

  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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文