Gary King

Gary King

  • NA
  • 83
  • 209.5k

Dynamically Setting a Form's Control Property (Textbox)

Jun 27 2011 7:50 AM
I have an aspx form with controls along the lines of:

<asp:TextBox ID="FirstName" runat="server" />

<asp:TextBox ID="LastName" runat="server" />
<asp:TextBox ID="Age" runat="server" />



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 
 1GaryKing  40
 2 BobSmith 36


What I want to do is read a row into a DataReader and then iterate through the columns in the DataReader and use the information in the code like this:

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!

 }


Answers (3)