Insert TextBoxes into a Repeater dynamically and retrieve their value

六月ゝ 毕业季﹏ 提交于 2019-12-19 11:46:35

问题


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.

回答1:


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,

  • Dynamic controls must be added each time the page is loaded,
  • but once you add them, ASP.NET restores their state correctly.

Hope this helps!




回答2:


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:

myval =  request.form["txt" + key];


来源:https://stackoverflow.com/questions/5666031/insert-textboxes-into-a-repeater-dynamically-and-retrieve-their-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!