473,395 Members | 1,790 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,395 software developers and data experts.

Gridview Commandfield imageurl

AG
I have a asp.net 2.0 gridview control with select, edit, delete buttons
enabled and set to image button type to display my images.
Is there any way to access the selectedimageurl property at run time so I
can supply a different image for the selected row?
I am assuming that I would do it in the RowDataBound event.
Or do I need to use a separate template column?

TIA

--

AG
Email: discuss at adhdata dot com


Sep 28 '06 #1
4 5629
Hi,

By default, the CommandField doesn't provide direct way to change the
selected row's SelectImageUrl. For such requirement, I recommend you to use
a TemplateField to create the select image manually:

<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgSelect" runat="server"
ImageUrl="~/Unselected.bmp" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>

We need to handle the GridView's two events: SelectedIndexChanging and
SelectedIndexChanged:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs
e)
{
SetSelectRowImage(GridView1.SelectedRow, true);
}
protected void GridView1_SelectedIndexChanging(object sender,
GridViewSelectEventArgs e)
{
SetSelectRowImage(GridView1.SelectedRow, false);
}

The function SetSelectRowImage() is used to change the image of the row
(NOTE: you need to change the cell index according to your field's position:

private void SetSelectRowImage(GridViewRow row, bool bSelect)
{
if (row != null)
{
ImageButton ib = row.Cells[0].FindControl("imgSelect") as
ImageButton;
if (bSelect)
{
ib.ImageUrl = "~/Selected.bmp";
}
else
{
ib.ImageUrl = "~/Unselected.bmp";
}
}
}

I hope this answers your question, please let me know whether or not you
need further information. Thanks.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 29 '06 #2
AG
Thanks Walter,

That worked well when only using the select button.
I also added template columns for edit and delete.
When edit is clicked I make the edit row the selected row also and in that
case need to reset the image also. Plus when the page is initially loaded, I
select the first row.

To cover all bases I moved the line
SetSelectRowImage(GridView1.SelectedRow, true);
to the gridview's PreRender event.
Seems to work fine.
Do you forsee any problem with that?

--

AG
Email: discuss at adhdata dot com

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:jv**************@TK2MSFTNGXA01.phx.gbl...
Hi,

By default, the CommandField doesn't provide direct way to change the
selected row's SelectImageUrl. For such requirement, I recommend you to
use
a TemplateField to create the select image manually:

<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgSelect" runat="server"
ImageUrl="~/Unselected.bmp" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>

We need to handle the GridView's two events: SelectedIndexChanging and
SelectedIndexChanged:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs
e)
{
SetSelectRowImage(GridView1.SelectedRow, true);
}
protected void GridView1_SelectedIndexChanging(object sender,
GridViewSelectEventArgs e)
{
SetSelectRowImage(GridView1.SelectedRow, false);
}

The function SetSelectRowImage() is used to change the image of the row
(NOTE: you need to change the cell index according to your field's
position:

private void SetSelectRowImage(GridViewRow row, bool bSelect)
{
if (row != null)
{
ImageButton ib = row.Cells[0].FindControl("imgSelect") as
ImageButton;
if (bSelect)
{
ib.ImageUrl = "~/Selected.bmp";
}
else
{
ib.ImageUrl = "~/Unselected.bmp";
}
}
}

I hope this answers your question, please let me know whether or not you
need further information. Thanks.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your
reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Sep 30 '06 #3
Hi AG,

From your description, I understand that:

1) You want the first row is selected when the page is initially loaded:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (GridView1.Rows.Count 0)
{
GridView1.SelectedIndex = 0;
}
}
}

You also made following changes:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs
e)
{
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
SetSelectRowImage(GridView1.SelectedRow, true);
}
2) You want the editing row also the selected row.

protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.SelectedIndex = e.NewEditIndex;
}

If my above understanding is correct, then I think this will do the job.
Please feel free to let me know if this doesn't work in other situations.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 2 '06 #4
AG
That does it, thanks Walter.

--

AG
Email: discuss at adhdata dot com

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:nU**************@TK2MSFTNGXA01.phx.gbl...
Hi AG,

From your description, I understand that:

1) You want the first row is selected when the page is initially loaded:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (GridView1.Rows.Count 0)
{
GridView1.SelectedIndex = 0;
}
}
}

You also made following changes:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs
e)
{
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
SetSelectRowImage(GridView1.SelectedRow, true);
}
2) You want the editing row also the selected row.

protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.SelectedIndex = e.NewEditIndex;
}

If my above understanding is correct, then I think this will do the job.
Please feel free to let me know if this doesn't work in other situations.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Oct 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
4
by: ber.janssens | last post by:
Hi, In ASP.NET 2.0, I have a GridView with 2 CommandFields, how can I determine in the SelectedIndexChanged event which CommandField is clicked? <asp:GridView ID="gvOrderList" runat="server"...
1
by: Burt | last post by:
I am trying to move all page attributes to a Theme skin file but can't set the Font-Size for a GridView Button. Anyone know how? <asp:GridView .... <asp:CommandField ButtonType="Button">...
5
by: graphicsxp | last post by:
Hi, I've added a linkbutton field to my gridview. Now I would like that when the user clicks on it for a particular row, a server-side function should be executed, which takes as parameters the id...
0
by: Mike P | last post by:
When you specify updateparameters or deleteparameters for your gridview, I was under the impression that whether or not you write some code in the rowcommand event to deal with this, the update or...
1
by: Evan M. | last post by:
Here's my GridView and my SqlDataSource <asp:GridView ID="ContactHistoryGrid" runat="server" AutoGenerateColumns="False" DataSourceID="ContactHistoryDS" DataKeyNames="JobHistoryID"...
0
by: haina | last post by:
the aspx file as blow <asp:DetailsView Width="100%" ID="dvOrg" runat="server" DataKeyNames="Id" AutoGenerateRows="False" OnModeChanging="dvOrg_ModeChanging" OnItemUpdating="dvOrg_OnItemUpdating" ...
3
by: COHENMARVIN | last post by:
I have a sqldatasource control that uses 3 parameters. I have a gridview that depends on the sqldatasource control The 3 parameters are session variables. So in my code, I set the values of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.