动态地将文本框插入中继器并检索它们的值
我有一个从 SQL 绑定的中继器,包含可编辑和只读元素的混合。当您单击一行上的“编辑”按钮时,可编辑部分应转换为文本框,当您单击“更新”时,它应保存您的更改。
这是编辑和更新按钮的 OnClick 代码的(非常)简化版本:
switch(commandName)
{
case "Edit":
Label1.Visible = false; //hide read-only version
PlaceHolder1.Visible = true; //show editing version
//Dict1 is Dictionary<string, string> in this example.
foreach (var key in Dict1)
{
//insert a TextBox dynamically into the PlaceHolder
PlaceHolder1.Controls.Add(new TextBox
{
ID = "txt" + key,
Text = Dict1[key]
});
}
break;
case "Update":
//retrieve user input from dynamically-added TextBoxes
foreach (var TextBox1 in PlaceHolder1.Controls.Where(c => c.ID.StartsWith("txt")))
{
doStuff(TextBox1);
}
Label1.Visible = true; //show read-only version
PlaceHolder1.Visible = false; //hide editing version
break;
}
问题是,当页面回发时,我动态添加的文本框不存在。我在调试器中检查了 PlaceHolder1.Controls
,其中没有文本框。 PlaceHolder1
本身位于中继器内,但我不会在回发时重新绑定中继器。
我曾考虑过使用原始 HTML 来代替 TextBox 控件,并从 Request.Form 中提取值,但这对我来说感觉有点骇人听闻。如何使动态添加的文本框在回发过程中保持不变?
编辑:
这里有一些复杂的情况,如果没有大量的示例代码就很难展示。以下是重要的内容:
- 转发器中的每个单元格都可以混合只读和可编辑文本(即动态插入的标签和文本框)
- 我不知道每个单元格中有多少个可编辑区域。原始文本可能看起来像
blah blah @A1@ blah blah @A2@ blah...
,我必须插入替换文本来代替 @A1@、@A2@ 等。编辑模式,只有替换文本是可编辑的。
I have a Repeater bound from SQL, containing a mixture of editable and read-only elements. When you click the "Edit" button on a row, the editable portions should convert to textboxes, and when you click "Update", it should save your changes.
Here's a (very) simplified version of the Edit and Update buttons' OnClick code:
switch(commandName)
{
case "Edit":
Label1.Visible = false; //hide read-only version
PlaceHolder1.Visible = true; //show editing version
//Dict1 is Dictionary<string, string> in this example.
foreach (var key in Dict1)
{
//insert a TextBox dynamically into the PlaceHolder
PlaceHolder1.Controls.Add(new TextBox
{
ID = "txt" + key,
Text = Dict1[key]
});
}
break;
case "Update":
//retrieve user input from dynamically-added TextBoxes
foreach (var TextBox1 in PlaceHolder1.Controls.Where(c => c.ID.StartsWith("txt")))
{
doStuff(TextBox1);
}
Label1.Visible = true; //show read-only version
PlaceHolder1.Visible = false; //hide editing version
break;
}
The problem is that my dynamically-added TextBoxes aren't there when the page posts back. I've examined PlaceHolder1.Controls
in the debugger, and there are no TextBoxes in it. PlaceHolder1
itself is inside a Repeater, but I'm not rebinding the Repeater on PostBack.
I've considered using raw HTML in place of TextBox controls and pulling the values out of Request.Form, but that feels hackish to me. How can I make the dynamically-added TextBoxes persistent across postbacks?
EDIT:
There are some complications here that it's hard to show without a ton of sample code. Here are the big ones:
- Each cell in the repeater can have a mixture of read-only and editable text (i.e. dynamically-inserted Labels and TextBoxes)
- I don't know how many editable regions are going to be in each cell. The original text might look like
blah blah @A1@ blah blah @A2@ blah...
, and I would have to insert replacement text in place of @A1@, @A2@, etc. In edit mode, only that replacement text is editable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您动态添加控件时,它们不会在回发期间保留。回发后您必须再次将它们添加到页面中。我通常在 Page_Load 中执行此操作。然后,一旦添加它们,它们的回发状态将在稍后的 ASP.NET 页面生命周期中正确恢复。
因此,
希望这有帮助!
When you add controls dynamically, they aren't preserved across postbacks. You have to add them to the page again after postback. I usually do it in Page_Load. Then, once they are added, their postback states will be restored correctly later in the ASP.NET page life cycle.
So,
Hope this helps!
一种可能的答案是在项目模板中预定义文本框,然后只需在单击事件上为给定行设置其可见性。
第二种可能性是重构中继器以使用 GRIDVIEW 控件。它支持开箱即用的可编辑模板。
无论如何,发生的情况是,由于这些控件是动态添加的,因此它们不会在下一次回发时重新创建。在回发时,如果您想访问控件,则需要重新创建它们。 但是,不要忘记该值存储在表单集合中:
One possible answer would be to have the TEXTBOX pre-defined in the ITEM TEMPLATE, and then just set its' visiblity for the given row on the click event.
A second possibility would be to refactor the repeater to utilize the GRIDVIEW control instead. It supports editable templates out of the box.
Anyways, what's happening, is that since those controls are added dynamically, they're not getting recreated on the next postback. On the postback, you'll need to re-create the controls if you want to access them. BUT, don't forget that the value IS stored in the form collection: