开发者

Insert TextBoxes into a Repeater dynamically and retrieve their value

开发者 https://www.devze.com 2023-02-26 04:36 出处:网络
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

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.Contr开发者_StackOverflow社区ols 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.


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!


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消