How can you add Attributes to controls in template

  • Thread starter Thread starter ad
  • Start date Start date
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?
 
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')");
}
}
 
Back
Top