the datagrid control has an OnItemDataBound event, it fires for each row as it is bound to the grid. Create an event handler for this event, in the handler get a reference to the textbox control and add the attribute.
private void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox textBox = (TextBox)e.Item.FindControl("txtDate");
textBox.Attributes.Add("onclick","myJavascriptFunc();");
}
}
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
Is this right? I'm not sure it's good to have two text boxes with the same id in the template column of your datagrid.
<asp:TextBox id="txtDate" runat="server" /><asp:TextBox id="txtDate" runat="server" />
EDIT -> Oh hangon I've just realised the textbox is in the edititem template, I've never used that myself before I need some time to experiment. The If statement in the handler is checking for Item and alternating item. Perhaps this will work:
private void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton lButton = (LinkButton)e.Item.FindControl("Delete");
lButton.Attributes.Add("OnClick", "javascript:return confirm('Are you sure, would you like to delete?')");
}
else if (e.ItemType == ListItem.EditItem)
{
TextBox textBox = (TextBox)e.Item.FindControl("txtDate");
textBox.Attributes.Add("onclick","myJavascriptFunc();");
}
}
I will try it myself too.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
Yeah this works for me:
if (e.ItemType == ListItem.EditItem)
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68