Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically setting textbox values 1

Status
Not open for further replies.

pd123

Programmer
May 22, 2001
48
GB
Hi,

I have an array which stores the ID of the textbox control, and the value it has.

Eg. txtSurname,Smith|txtForename,John|txtAge,22|txtCity,London

What I am currently doing is looping around this array and extracting the values. However, how do I programmatically set the values?

eg. txtSurname.Text = "Smith"

The string holds "txtSurname" - how do I target this control and access it's properties (ie. setting the text).

Any help appreciated.

Thanks,
pd123
 
if the control is created at runtime you would have to loop through the controls in the page, check to see if they are textboxes and if the id matches, set their value

if the controls are not created dynamically and have a runat='server' attribute, you can simply access them from code by their id.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Thanks.

The controls are not created dynamically. The array stores this information so when the page is re-visited later I can extract the values and populate the fields with values.

I know the control is called txtSurname - this is stored in the array. The question is how do I get a handle onto it's properties when the name of the control ID is stored in a variable?

Is the only way the method you suggested? ie. by looping around the controls on the page and comparing the ID?
 
Code:
Control ctrl = Page.FindControl("txtUsername");
if (ctrl != null && ctrl is TextBox)
{
     ((TextBox)ctrl).Text = "whatever";
}

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Thanks DaZZleD - that's the code I was after.

Cheers!
pd123
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top