How to add attributes to GridView ButtonFields?

R

Rob Roberts

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
 
B

Bruno Piovan

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
 
R

Rob Roberts

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
 
B

Bruno Piovan

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 said:
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
 

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,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top