1

I have a C# project which has the textbox dynamically created and when the form is submitted the code behind needs to retrieve the value of the textbox.

The id of the textbox contains a $ dollar sign and when retrieving the value it says empty.

Wondering if the $ is considered a special character for the ID or if there is a way to handle this issue?

// Here is my sample code

//Create Form
foreach (DataRow row in setOfColumns.Rows)
{
    TextBox txtBox = new TextBox();
    txtBox.ID = row["NameOfColumn"] + " textbox"; //Where row["NameOfColumn"] = "Money $"
}

//Display form on browser values
name="ctl00$ContentPlaceHolder1$ctl00$Money $"
id="ContentPlaceHolder1_ctl00_Money $"
value="$3,392"

//Submit Form
DataTable dt = new DataTable();
foreach (Control c in controls)
{
    if (c is TextBox)
    {
        DataRow dr = dt.NewRow();
        String txtValueCtrl = ((TextBox)c).Text.ToString(); //txtValueCtrl = "", it returns empty value for the textbox
        dr["Value"] = txtValueCtrl;

        dt.Rows.Add(dr);
    }
}

//Once I remove the '$' from the txtBox.ID it is able to retrieve the value from the textbox
  • Please show some code giving us at least a hint of how you are doing this. For instance, is it an ASP.NET TextBox control? – John Saunders Feb 18 '14 at 23:40
  • 1
    When the form is submitted it's not the `id` attribute that is used as key in the form data, but the `name` attribute. What is the name of the textbox? – Guffa Feb 18 '14 at 23:41
  • possible duplicate of [Accessing control Client Name and not Id in ASP.NET](http://stackoverflow.com/questions/5763557/accessing-control-client-name-and-not-id-in-asp-net) – Aristos Feb 19 '14 at 00:13
  • You have add a space on the id and on name. You have probably wrong there. – Aristos Feb 19 '14 at 00:15
  • Why are you putting a space $ as part of the Id and name? – Rick S Feb 19 '14 at 00:19
  • It is not the space because I have used the ID as 'Money Amount' and it has worked. The reason I am using the $ and space as ID and Name is that the Label text is 'Money $' and I maintain the label text, name, id the same for the textbox id and name. Trying to have the name of the value be similar. In the ID and name of each element I do append the element type. Ex. label.ID = "Money $ label" txtBox.ID = "Money $ textbox" I recently upgraded to .NET 4.5 and it started to give this as a problem. Previously on .Net 2.0 – user3325717 Feb 19 '14 at 01:05

1 Answers1

0

If you are using .NET 4.0 or above, you can prevent this behavior by adding an attribute "ClientIDMode" to "Static" which will no longer cause the client ID to be modified the way you are seeing.

Regarding your inability to access the value: Are you sure you have the viewstate turned on for the control?

Ananthan Unni
  • 1,304
  • 9
  • 23
  • I do not have an issue with the control ID being modified the way it is being modified since I am not looking for the control based on the ID itself, the code looks at all the elements on the page and select the specific one for the value. I tried setting the viewstate for the control to true but it still did not return the value of the textbox. – user3325717 Feb 19 '14 at 16:22