0

I have some dynamic controls in ASP.NET. I need to retrieve a value on postback from a control that no longer exists.

The following code works perfectly:

value = Request.Form["ctl00$ContentPlaceHolder1$text_name"];

However, I'm wondering if there's an elegant way to dynamically generate the "ctl00$ContentPlaceHolder$" part or to get the value of textbox_value.UniqueID when the control no longer exists.

Apologies if this question's come up a million times before - just looking for a more elegant solution that doesn't involve changing any settings on the server.

Edit:

As requested, the code for adding controls is like this:

foreach (NameValuePair entry in database_table){

    Label label = new Label();
    label.ID = "label_" + entry.Name;
    label.Value = entry.Name;
    label.Attributes.Add("runat", "server");

    TextBox textBox = new TextBox();
    textBox.ID = "text_" + entry.Name;
    textBox.Text = entry.Value;
    textBox.Attributes.Add("runat", "server");
}

On a button click, it reads name-value pairs from a database table (the number of entries is not static), allows them to be modified and saves them when a "save" button is pressed. It functions like a pop-up, so the fields are gone on postback. (I would prefer not to change this part of the code)

Thanks~

lem
  • 155
  • 1
  • 11
  • You need to display the actual code of how your are adding the dynamic controls. – Cherian M Paul Aug 21 '13 at 14:40
  • Can't you store the UniqueID somewhere between round trips? – Andyz Smith Aug 21 '13 at 14:45
  • I would prefer not to since I would need something like a Session variable to store every dynamic ID, and the users need to be able to edit in multiple tabs (which greatly restricts what I can do with the Session). – lem Aug 21 '13 at 16:19
  • What if you use an unlikely string for the Label.ID. Then you could iterate the request collection for anything that has the unlikely string in it, regardless of ctl100, ctl101, etc. – Andyz Smith Aug 21 '13 at 21:45
  • That is kinda what I ended up doing. :) – lem Aug 22 '13 at 14:46
  • This is kindof what Static ClientIdMode does. Gives you a more solid handle on something dynamically generated. ..... My other suggestion woud be to store the UniqueId in a hidden field in the markup. – Andyz Smith Aug 22 '13 at 15:01

2 Answers2

2

I ended up writing my own function to parse the Request.Form data. It searches for the control named attname and returns the value of the control.

private string findControlValue(string attname)
{
    string[] request = Request.Form.ToString().Split('&');
    string searchkey = "txt_" + attname;

    foreach (string seg in request)
    {
        if (seg.Contains(searchkey))
        {
            string ctrlName = seg.Split('=')[0];
            string ctrlValue = seg.Split('=')[1];
            string value = Server.UrlDecode(ctrlValue);
            return value;
        }
    }
    return null;
}
Joshua Dwire
  • 5,415
  • 5
  • 29
  • 50
lem
  • 155
  • 1
  • 11
1

If you are using ASP.net 4.0 or later, you can set the ClientIdMode of the control to be static, and then the ID that you use for the control will be the one that is used client-side (though this wont work for controls that are in repeater-like controls - a static ID has to be unique to the page). If you can do this, then it will allow you to set "friendlier" ID values for the controls (which will be more predictable for your data retrieval as shown in your question).

Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
  • For some reason I couldn't get this to work. I'm not sure if I was doing something wrong, but I tried adding it to the parent control and also dynamically adding it to the controls themselves with no luck. – lem Aug 22 '13 at 14:50