In the .cs I can set the .Text property as follows:
FirstName.Text = "Gary";
LastName.Text = "King";
Age.Text = "40";
Simple! BUT.....
The values are being retrieved from a SQL database table that looks like this:
| ID | FirstName | LastName | Age |
| 1 | Gary | King | 40 |
| 2 | Bob | Smith | 36 |
for (int i = 1; i < dr.FieldCount; i++)
{
// use the column name to determine which form control (textbox) .Text property to set
dr.GetName(i).Text = dr.GetValue(i); // -- This is the bit that I am struggling with!
}
VulpesPosted Jun 27, 2011, 9:33 AM
for (int i = 0; i < dr.FieldCount; i++)
{
Control c = this.FindControl(dr.GetName(i));
if (c != null)
{
string value = dr.GetValue(i).ToString();
if (c is TextBox)
{
TextBox tb = (TextBox)c;
tb.Text = value;
}
else if (c is Label)
{
Label lbl = (Label)c;
lbl.Text = value;
}
else if (c is RadioButton)
{
RadioButton rb = (RadioButton)c;
rb.Checked = (value.ToLower() == "true");
}
}
}
Gary KingPosted Jun 27, 2011, 9:12 AM
However, it is a little more complex than that....
The form control is not necessarily a TextBox - some of them are Labels or RadioButtons.
Obviously RadioButtons need to be handled different (ie, .Checked instead of .Text)
What about the Labels? Is the a way that they can be factored into the code that you kindly provided?
Thanks
Gary
Suthish NairPosted Jun 27, 2011, 8:15 AM
TextBox tmptxt;
for (int i = 0; i < dr.FieldCount; i++)
{
tmptxt = new TextBox();
tmptxt = (TextBox)this.FindControl(dr.GetName(i));
if(tmptxt != null)
{
tmptxt.Text = dr.GetValue(i).ToString();
}
tmptxt = null;
}