473,670 Members | 2,228 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 selectedimageur l 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 5645
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:TemplateFi eld>
<ItemTemplate >
<asp:ImageButto n ID="imgSelect" runat="server"
ImageUrl="~/Unselected.bmp" CommandName="Se lect" />
</ItemTemplate>
</asp:TemplateFie ld>

We need to handle the GridView's two events: SelectedIndexCh anging and
SelectedIndexCh anged:

protected void GridView1_Selec tedIndexChanged (object sender, EventArgs
e)
{
SetSelectRowIma ge(GridView1.Se lectedRow, true);
}
protected void GridView1_Selec tedIndexChangin g(object sender,
GridViewSelectE ventArgs e)
{
SetSelectRowIma ge(GridView1.Se lectedRow, false);
}

The function SetSelectRowIma ge() 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 SetSelectRowIma ge(GridViewRow row, bool bSelect)
{
if (row != null)
{
ImageButton ib = row.Cells[0].FindControl("i mgSelect") 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
SetSelectRowIma ge(GridView1.Se lectedRow, 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.comwr ote in message
news:jv******** ******@TK2MSFTN GXA01.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:TemplateFi eld>
<ItemTemplate >
<asp:ImageButto n ID="imgSelect" runat="server"
ImageUrl="~/Unselected.bmp" CommandName="Se lect" />
</ItemTemplate>
</asp:TemplateFie ld>

We need to handle the GridView's two events: SelectedIndexCh anging and
SelectedIndexCh anged:

protected void GridView1_Selec tedIndexChanged (object sender, EventArgs
e)
{
SetSelectRowIma ge(GridView1.Se lectedRow, true);
}
protected void GridView1_Selec tedIndexChangin g(object sender,
GridViewSelectE ventArgs e)
{
SetSelectRowIma ge(GridView1.Se lectedRow, false);
}

The function SetSelectRowIma ge() 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 SetSelectRowIma ge(GridViewRow row, bool bSelect)
{
if (row != null)
{
ImageButton ib = row.Cells[0].FindControl("i mgSelect") 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(objec t sender, EventArgs e)
{
if (!IsPostBack)
{
if (GridView1.Rows .Count 0)
{
GridView1.Selec tedIndex = 0;
}
}
}

You also made following changes:

protected void GridView1_Selec tedIndexChanged (object sender, EventArgs
e)
{
}
protected void GridView1_PreRe nder(object sender, EventArgs e)
{
SetSelectRowIma ge(GridView1.Se lectedRow, true);
}
2) You want the editing row also the selected row.

protected void GridView1_RowEd iting(object sender,
GridViewEditEve ntArgs e)
{
GridView1.Selec tedIndex = 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.comwr ote in message
news:nU******** ******@TK2MSFTN GXA01.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(objec t sender, EventArgs e)
{
if (!IsPostBack)
{
if (GridView1.Rows .Count 0)
{
GridView1.Selec tedIndex = 0;
}
}
}

You also made following changes:

protected void GridView1_Selec tedIndexChanged (object sender, EventArgs
e)
{
}
protected void GridView1_PreRe nder(object sender, EventArgs e)
{
SetSelectRowIma ge(GridView1.Se lectedRow, true);
}
2) You want the editing row also the selected row.

protected void GridView1_RowEd iting(object sender,
GridViewEditEve ntArgs e)
{
GridView1.Selec tedIndex = 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
13741
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. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
4
9790
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" AutoGenerateColumns="False" OnSelectedIndexChanged="gvOrderList_SelectedIndexChanged"> <Columns>
1
3546
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"> <ControlStyle Font-Size="X-Small" /> </asp:CommandField> </asp:GridView>
5
4931
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 of the selected row. How can I do that ? Thanks
0
1955
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 delete happens anyway. For example, I have a gridview like this : <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="GetOppsTypeList"...
1
10396
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" OnRowCreated="ContactHistoryGrid_RowCreated" CssClass="GridViewTable" GridLines="None" CellSpacing="1" CellPadding="3" AllowSorting="True" AllowPaging="True"> <EmptyDataTemplate>
0
928
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" CellPadding="4" GridLines="None" > <Fields> <asp:TemplateField SortExpression="Logo" HeaderText="Logo:"> <ItemTemplate> <asp:Image ID="imgLogo" runat="Server" Width="100px" Height="100px" ...
3
1566
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 those 3 session variables, and then do a 'gridview.databind' and hope that something happens. But nothing happens - the gridview does not appear. Then I noticed that if I click on a dropdownlist that has 'autopostback' enabled, that the page...
0
8469
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8661
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7419
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2800
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.