插入文本框到一个中继动态和检索其价值 [英] Insert TextBoxes into a Repeater dynamically and retrieve their value
问题描述
我从SQL绑定一个中继器,包含可编辑和只读元素的混合物。当你点击某行的编辑按钮,可编辑的部分应转换为文本框,当您单击更新,它应该保存更改。
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.
下面是一个(非常)简化了编辑和更新按钮的onClick code版本:
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;
}
问题是,我的动态添加文本框是不是有当页面回。我已经研究在调试器 PlaceHolder1.Controls
,有它无文本框。 PLACEHOLDER1
本身是一个中继器里面,但我不会重新绑定在回发的中继器。
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.
我在地方TextBox控件的使用原始的HTML和拉动值出的Request.Form的考虑,但那种感觉的hackish给我。我怎样才能使动态添加文本框在回传执着?
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?
编辑:
有一些并发症这里,很难展现一个没有样品每吨code的。这里是大的:
There are some complications here that it's hard to show without a ton of sample code. Here are the big ones:
- 在中继器的每个单元格可以有只读和可编辑的文本的混合物(即动态插入标签和文本框)
- 我不知道有多少可编辑区域将如何在每个单元格。原文可能类似于
等等等等@ A1 @等等等等@ A2 @等等...
,我将不得不插入替换文本到位@ A1 @,@的A2 @等。在编辑模式下,只有替换文本可编辑。
- 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.
推荐答案
当您添加控件动态,他们不是在回传pserved $ P $。你必须给他们回传后再次添加到页面。我通常做在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.
因此,
- 动态控制必须在每次页面加载时间增加,
- 但是一旦你添加它们,ASP.NET正确地恢复自己的状态。
希望这有助于!
这篇关于插入文本框到一个中继动态和检索其价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!