Ask the Expert

How do I make the ID of my textbox assign in the property of the usercontrol?

I am using a datagrid to display a set of records, and I am generating controls for each column dynamically in ASP.NET page. The first column of the datagrid contains a usercontrol and the second column contains a textbox. Both are generated dynamically. But I want the ID of the textbox to assign in the property of the usercontrol. How to do that? Please help me.
You could assign a fixed ID to the textbox, such as txtTextbox, and to the user control, such as usrControl. Later on, you can handle the ItemCreated event (or the ItemDataBound) and use the FindControl method to locate both your user control and the textbox and assign the property you need:
private void theGrid_ItemCreated(object sender, DataGridItemEventArgs e)
{
 Control usr = e.Item.FindControl("usrControl");
 Control txt = e.Item.FindControl("txtTextbox");
 //Assign the prop.
 ((MyUserControl)usr).TheTextBoxID = txt.ClientID;
}

This was first published in November 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: