从中继器中检索文本框值
在 Page_Load
上,我使用 EF4 绑定中继器。中继器包含一些在开始时填充的文本框,但用户可以编辑。我如何在稍后的回发中阅读这些文本框?
page.aspx(摘录):
<asp:Repeater ID="repOrders" runat="server">
<ItemTemplate>
<p>
<%# Eval("Id") %> -
<asp:TextBox ID="txtName" Text='<%# Eval("Name") %>' runat="server">
</asp:TextBox>
</p>
</ItemTemplate>
</asp:Repeater>
page.aspx.cs(摘录):
protected void Page_Load(object sender, EventArgs e) {
using (var ctx = new Ctx()) {
var ds = ctx.Orders;
repOrders.DataSource = ds;
repOrders.DataBind();
}
}
protected void lnkCheck_Click(object sender, EventArgs e) {
Response.Write(((TextBox)repOrders.Items[0].FindControl("txtName")).Text);
}
问题是,当我单击lnkCheck
时,中继器重新首先从数据库填充,因此用户对文本框的任何更改都会丢失。
如果我将 if (IsPostBack) return;
放入 Page_Load
中,则中继器为空。
我该如何解决这个问题?
On Page_Load
, I am binding a Repeater using EF4. The repeater contains some text boxes which are populated at the start but the user could edit. How can I read these text boxes on a later postback?
page.aspx (extract):
<asp:Repeater ID="repOrders" runat="server">
<ItemTemplate>
<p>
<%# Eval("Id") %> -
<asp:TextBox ID="txtName" Text='<%# Eval("Name") %>' runat="server">
</asp:TextBox>
</p>
</ItemTemplate>
</asp:Repeater>
page.aspx.cs (extract):
protected void Page_Load(object sender, EventArgs e) {
using (var ctx = new Ctx()) {
var ds = ctx.Orders;
repOrders.DataSource = ds;
repOrders.DataBind();
}
}
protected void lnkCheck_Click(object sender, EventArgs e) {
Response.Write(((TextBox)repOrders.Items[0].FindControl("txtName")).Text);
}
The problem is, when I click lnkCheck
, the Repeater re-populates from the database first so any user changes to the textboxes are lost.
And if I put if (IsPostBack) return;
in Page_Load
then the Repeater is empty.
How can I get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不是回发,则仅填充转发器。只要为转发器启用了视图状态,您的数据就会被保留。
您如何提出“稍后回发”以从中继器读取文本值?通常的偏好是从转发器发出 item 命令,然后使用查找控件来获取项目模板中文本框的值。
Only populate the repeater if not a postback. As long as viewstate is enabled for the repeater your data will be persisted.
How are you raising the 'later postback' to read the text values from the repeater? Usual preference would be to raise an the itemcommand from the repeater and then use find control to get the value of the textbox in the item template.