C# 从表单视图中的repeater内的textfield获取数据
我想获得repeater中文本框的值,repeater位于绑定到对象数据源的表单视图中C# 从表单视图中的repeater内的textfield获取数据,c#,asp.net,repeater,objectdatasource,formview,C#,Asp.net,Repeater,Objectdatasource,Formview,我想获得repeater中文本框的值,repeater位于绑定到对象数据源的表单视图中 <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="Id" EnableViewState="False" OnPageIndexChanging="FormView1_PageIndexChanging" onitemupdated="FormView1_ItemUpdated"
<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>
但是我不能用它来获取中继器中的文本框,因为它给了我空值
有什么帮助吗?您必须找到转发器内部的文本框,看看您是如何使用
FormView1
试试看的
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;
}
使用递归搜索查找控件,无论其深度如何:
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;
}
在哪种情况下,您会使用此代码?第一条语句必须在
FormView
绑定事件中,第二条语句必须在repeater
绑定事件中。请检查我的更新并将AutoPostBack=“true”
添加到文本框中。在回发页面时,请确保不要重新绑定FormView
。问题是,此转发器绑定到一组对象,我不知道它将生成多少个文本框,因此,在文本更改中,我可以看到文本正在更改,但如何更新我的对象您必须通过将其ID设置为唯一值来识别文本框
,该值应为对象的属性,然后使用Linq
您可以获得要更新的对象。检查我的回答nx我尝试了这个,我找到了控制文本框,但它给我的是旧值而不是编辑的值。。。
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;
}