Reputation: 6543
In c#, how do I access asp:textbox elements with dynamic variable names? The code I provide dont work for some reason. The method createContact is called on a button click. Im doing som refactoring and perhaps I also should add the textfields dynamicly?
Some sample code:
front:
<asp:TextBox runat="server" ID="OwnerFirstName" class="handleLongField"></asp:TextBox>
<asp:TextBox runat="server" ID="OwnerLastName" class="handleLongField"></asp:TextBox>
<asp:TextBox runat="server" ID="AdminFirstName" class="handleLongField"></asp:TextBox>
<asp:TextBox runat="server" ID="AdminLastName" class="handleLongField"></asp:TextBox>
and more of this kind
code behind :
public bool createContact(CONTACT_ROLES role)
{
String prefix = "";
switch(role)
{
case CONTACT_ROLES.ADMIN :
prefix = "Admin";
break;
case CONTACT_ROLES.OWNER :
prefix = "Owner";
break;
case CONTACT_ROLES.BILLING :
prefix = "Billing";
break;
case CONTACT_ROLES.TECH :
prefix = "Tech";
break;
}
Contact contact = new Contact();
contact.firstName = ((TextBox)this.FindControl(prefix + "FirstName")).Text;
contact.lastName = ((TextBox)this.FindControl(prefix + "LastName").Text;
}
Upvotes: 0
Views: 4502
Reputation: 6543
In my specific case all I hade to do was to add an placeholder around all the textboxes in the front. Then instead of calling
contact.firstName = ((TextBox)this.FindControl(prefix + "FirstName")).Text;
I used
contact.firstName = ((TextBox)myPlaceHolder.FindControl(prefix + "FirstName")).Text;
So for some reason "this" didnt point to the correct place. Anyone got an idé why that is? This is a page within a masterpage.
Upvotes: 0
Reputation: 18797
Other folks have mentioned that you don't need to use dynamic names. But in case you're interested, Here's how it goes:
If you create controls in your load event, and add them to your page, you won't have intellisense in your methods to access them. e.g. you create controls like:
var txtFirstName = new TextBox();
txtFirstName.ID = prefix + "FirstName";
this.Page.Form.Controls.Add(txtFirstName);
If you want to have access to this control after postback, You have to create it again in your page_load event (meaning you should not limit it to if (!IsPostback)
condition. Then you can access the control in any method of you page as follows:
TextBox txtFirstName = this.Page.Form.FindControl(prefix + "FirstName") as TextBox;
if(txtFirstName != null)
...
Upvotes: 2
Reputation: 218961
Honestly, in the provided example it looks like the need for the dynamic name is a result of over-engineering. If the controls themselves are static, then the code which accesses them should really also be static. The switch
construct, the calls to FindControl
, etc. all seem to be an unnecessarily obtuse substitute for just building the objects from the known controls:
var adminContact = new Contact();
adminContact.firstName = AdminFirstName.Text;
adminContact.lastName = AdminLastName.Text;
var ownerContact = new Contact();
ownerContact.firstName = OwnerFirstName.Text;
ownerContact.lastName = OwnerLastName.Text;
etc...
If you really truly need to create only one Contact
at any given time, then you can wrap these groups in a switch
for that purpose. But trying to fit dynamic names into the code as well just seems like an attempt to be more clever than necessary, which often leads to strange bugs and support problems.
Upvotes: 1
Reputation: 3885
I don't think you would require FindControl here, since these textboxes are present on the page itself and not inside a control on the page.
Try the following:
private Control FindControlByName(string name)
{
foreach (Control c in this.Controls) //assuming this is a Form
{
if (c.Name == name)
return c; //found
}
return null; //not found
}
Upvotes: 1
Reputation: 85106
Can you do this instead:
public bool createContact(CONTACT_ROLES role)
{
Contact contact = new Contact();
String prefix = "";
string firstName;
string lastName;
switch(role)
{
case CONTACT_ROLES.ADMIN :
firstName = AdminFirstName.Text;
lastName= AdminLastName.Text;
break;
case CONTACT_ROLES.OWNER :
firstName = OwnerFirstName.Text;
lastName= OwnerLastName.Text;
break;
//and so on....
}
contact.firstName = firstName ;
contact.lastName = lastName;
}
It doesn't seem like you really have dynamic names for your textBoxes, you just want to get the values from specific textboxes based on other criteria
Upvotes: 2