Image with link in a gridview?

C

Carlos Albert

Hi everyone,

Is there a way to make a column that has an image with a hyperlink on it?
Cause I know how to make a hyperlink column, and a image column, but not how
to mix it both...

Tnx!
 
R

Remy

There are actually a bunch of different ways you can do that.
Do you want this to be a link that leads to another page, or more
something like a button that causes a callback?

For a callback I use a ButtonColumn, set following things:
Text:
<img src="Resources/images/Delete.gif?" width="18" height="18"
border="0" Alt="Delete this Trade">
Command:
cmdDelete
Button Type:
Link Button

For a link (it's actually with javascript, but you get the idea) I use
a plain BoundColumn:

Data formating expression:
<A HREF=javascript:eek:nClick=OpenNTMWindow({0});><img
src="Resources/images/XmlSign.gif?" width="18" height="18" border="0"
Alt="Show NTM Message"></a>
Data Field:
Whatever column from the db you wanna have in the {0} placeholder
above.

Hope that helps. You can build some pretty powerfull stuff this way.

Cheers

Remy Blaettler
www.collaboral.com
 
C

Carlos Albert

Hello again Remy,

I used the javascript just fine. Now I was trying to use the "real" button,
but I came across a problem. The button is in a gridview inside another
gridview. How can I now wich record the user "selected" pressing the button?
 
R

Remy

Yeah, that is a bit tricky I think, but below is a way to do it. (I'm
sure there are better ways):

private void dgErrorMessages_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
OdbcConnection execConn = null;
int rowID;

if (e.CommandName == "cmdResend")
{
//ItemIndex is only available if it is a command from a
row, but we also get the Page
//command in here
rowID = Convert.ToInt32(e.Item.Cells[3].Text);
log.Info("cmdResend - RowID = " + rowID);

try
{
execConn =
DatabaseUtils.OpenDatabaseConnection("Executive");

lbResult.Text = coreLogic.ResendNtmMessage(rowID,
coreLogic.GetStartDate(cldrStart), coreLogic.GetEndDate(cldrEnd),
execConn);
FillAndBindGrid(execConn);
}
finally
{
if(execConn != null) execConn.Close();
}
}
else if (e.CommandName == "cmdDelete")
{
//ItemIndex is only available if it is a command from a
row, but we also get the Page
//command in here
rowID = Convert.ToInt32(e.Item.Cells[3].Text);
log.Info("cmdDelete - RowID = " + rowID);

try
{
execConn =
DatabaseUtils.OpenDatabaseConnection("Executive");

lbResult.Text = coreLogic.DeleteNtmMessage(rowID,
execConn);
FillAndBindGrid(execConn);
}
finally
{
if(execConn != null) execConn.Close();
}
}
}

dgErrorMessages is the name of the grid. ItemCommand is the event
handler for all the commands that are sent via the columns. The command
name is put into the CommandName field in the ButtonColumn
configuration.
But the core is
e.Item.Cells[3]
which is how I get the ID for this column. The ID is in my 3rd column
(or in the 4th, not sure if it zero based anymore).
It's not very nice to hardcode the 3 into the code, but...

Hope that gives you a hint

Cheers

Remy Blaettler
www.collaboral.com
 
Joined
May 9, 2007
Messages
1
Reaction score
0
you can also use javascript:
<head runat="server">
<title>Untitled Page</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">

function loadIt(){

gv=document.getElementById("Gridview1");//this is our gridview rendered into a table
var len = document.images.length;
tbl=document.getElementById("Gridview1");
for(i=0;i<len;i++){
//our image column index is 6
link=document.images.src;
tbl.rows[i+1].cells[6].innerHTML="<a href='"+link+"'>"+tbl.rows[i+1].cells[6].innerHTML+"</a>";
} //end for
}//end function
</script>

</head>

then call it after loading your page. you can also change the link, here it links to image itself.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top