How can you add Attributes to controls in template

A

ad

If TextBox1 is a control on a WebForm, we can add Javascript function by
adding attribute in the Page_Load event like:

protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onclick", "return MyFunction('Tiger')");
}

But if the TextBox is in a template, How can we add the attributes with
codes?
 
G

Guest

You can add it in the ItemCreated/ItemDataBound event of the data-bound control

protected void My_ItemCreated(Object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
TextBox TextBox1 = (TextBox).e.Item.FindControl("TextBox1");
TextBox1.Attributes.Add("onclick", "return MyFunction('Tiger')");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top