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