By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

How to add attributes to GridView ButtonFields?

P: n/a
Is there any way to add attributes to GridView ButtonFields? I'm looking
for a way to add event handlers to ButtonFields. With regular ASP.NET
buttons, I can add event handlers by using the Attributes collection, like
this:

MyButton.Attributes.Add("onmouseover", "this.className='actionH'");
MyButton.Attributes.Add("onmouseout", "this.className='actionN'");

But the GridView ButtonField doesn't seem to have an Attributes property.
Is there any way that event handlers can be added to ButtonFields?

Thanks in advance,
--Rob Roberts
Apr 11 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rob,

you could to this in RowCreated event of the GridView, if you have a button
called btnButton in the column index 4, the code would be like this:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnButton =
(Button)e.Row.Cells[4].FindControl("btnButton");
btnButton.Attributes.Add("onmouseover",
"this.className='actionH'");
btnButton.Attributes.Add("onmouseout",
"this.className='actionN'");
}
}

also, you could do this after calling DataBind() method of the GridView,
this code would be almost the same...

foreach (GridViewRow row in GridView1.Rows)
{
Button btnButton =
(Button)row.Cells[4].FindControl("btnButton");
btnButton.Attributes.Add("onmouseover",
"this.className='actionH'");
btnButton.Attributes.Add("onmouseout",
"this.className='actionN'");
}

Bruno

"Rob Roberts" <ro************@AndThisToo.pcisys.net> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Is there any way to add attributes to GridView ButtonFields? I'm looking
for a way to add event handlers to ButtonFields. With regular ASP.NET
buttons, I can add event handlers by using the Attributes collection, like
this:

MyButton.Attributes.Add("onmouseover", "this.className='actionH'");
MyButton.Attributes.Add("onmouseout", "this.className='actionN'");

But the GridView ButtonField doesn't seem to have an Attributes property.
Is there any way that event handlers can be added to ButtonFields?

Thanks in advance,
--Rob Roberts

Apr 11 '06 #2

P: n/a
Bruno,
you could to this in RowCreated event of the GridView, if you have a
button called btnButton in the column index 4, the code would be like
this:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnButton =
(Button)e.Row.Cells[4].FindControl("btnButton");
btnButton.Attributes.Add("onmouseover",
"this.className='actionH'");
btnButton.Attributes.Add("onmouseout",
"this.className='actionN'");
}
}


Thank you for your reply. In your example above, how do you know what name
to pass to FindControl? I don't see any way to assign an Id value to a
GridView's fields. Without an Id value, what do you pass to FindControl?

I did get your example to work by using Controls[0] instead of
FindControl(), like this:

Button btnButton = (Button)e.Row.Cells[4].Controls[0];

Thanks!,
--Rob Roberts

Apr 11 '06 #3

P: n/a
Hi Rob,
sorry, I forgot to mention that the column 4 was a template column....
but you can get the button even if the column is not a template, the way you
did, using Controls[0] works ok as well, if you had a template column, you
can add a button and set its Id property to a name that you can use with
FindControl, FindControl is better to work if you have more than one control
in a template column so the Index might change if you add more controls.

After getting a reference to the Button, check the Id property, that is the
Id you can use with FindControl :)

Bruno

"Rob Roberts" <ro************@AndThisToo.pcisys.net> wrote in message
news:O%***************@TK2MSFTNGP02.phx.gbl...
Bruno,
you could to this in RowCreated event of the GridView, if you have a
button called btnButton in the column index 4, the code would be like
this:

protected void GridView1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnButton =
(Button)e.Row.Cells[4].FindControl("btnButton");
btnButton.Attributes.Add("onmouseover",
"this.className='actionH'");
btnButton.Attributes.Add("onmouseout",
"this.className='actionN'");
}
}


Thank you for your reply. In your example above, how do you know what
name to pass to FindControl? I don't see any way to assign an Id value to
a GridView's fields. Without an Id value, what do you pass to
FindControl?

I did get your example to work by using Controls[0] instead of
FindControl(), like this:

Button btnButton = (Button)e.Row.Cells[4].Controls[0];

Thanks!,
--Rob Roberts

Apr 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.