473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to add attributes to GridView ButtonFields?

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
3 14287
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rob Roberts | last post by:
I am trying to figure out a way to add onmouseover and onmouseout events to ButtonFields in a GridView in order to give them a rollover effect, but so far I haven't been able to figure out a way to...
2
by: MarkAurit | last post by:
I have a gridview with some boundfields and buttonfields, and based on the user's role I want to modify their attributes: for example, if the person's role doesnt allow updates, I want to set a...
0
by: MarkAurit | last post by:
I have a gridview that has some ButtonFields for delete, edit, etc. When the user clicks on delete the row is marked for deletion in the database and the gridview is refreshed, showing the row is...
13
by: AG | last post by:
I have a gridview that I bind to a List(of Type) at runtime. Not using a datasource control. The gridview has a template column with an imagebutton whose commandname is set to 'Delete'. The...
5
by: Kimmo Laine | last post by:
Hi is there a way to change propertys attribute from the code? Let´s say that i have the following property in my class: public int Count } Is there a way to change the displayname, from...
3
by: jobs | last post by:
Say I have Gridview with some Buttonfields I want to enable or disable based on the values of other bound data? Here I want I only want to enable button Rate only if field lastAction = "Export...
5
by: Yossi | last post by:
I have a grid view with several text columns and 2 button columns. How do I tell which button on which row was clicked?
7
Bob Ross
by: Bob Ross | last post by:
I have a GridView with some ButtonFields in it. I want to be able to export this grid into excel but I need to remove the the buttons from the buttonfields. Is there any way to easily...
1
by: COHENMARVIN | last post by:
I have a gridview with a button that should only appear if the database routine it is based on returns a value of 'True' for the database field "ReservationsExist". So I tried putting in the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.