PDA

View Full Version : Adding imagbutton event handler programaticly


lee2006
04-04-2007, 02:04 PM
Hi there I am having problems with adding an click event handler to some image buttons that are created programmticly using asp.net and vb.

I create the images from an xml file depending on the image tags contaned within, this works fine and dispplays the images on the screen.

But i want the images to be clickable to view a larger image, so i need to be able to point to the clickevent handler on the fly.

I have read loads of posts and tutorilas and they all say to point to a handler in vb just add the following line

AddHandler himage.Click, AddressOf btn_click

So i addid this to the creation of the image buttons, it dosent throw any errors but all that happens is the page is reloaded, the event handler is not fired, i have tested it, the code snippet is bellow:-

'loop for all image found in xml list
For a = 0 To himgcount - 1
' create new image instance and set variables
Dim himage As New ImageButton
himage.Width = 70
himage.Height = 70
himage.CssClass = "cimage"
himage.ID = "test" + a.ToString
'set images location from xml
himage.ImageUrl = imagepath & histimagexmn(a).Attributes("ipath").Value.Trim
'set image tool tip from xml atribute and trim white space
himage.ToolTip = histimagexmn(a).Attributes("toolt").Value.Trim
'set image alternate text from xml atribute and trim white space
himage.AlternateText = histimagexmn(a).Attributes("toolt").Value.Trim
AddHandler himage.Click, AddressOf btn_click
'add image to panel
hist_pnl.Controls.Add(himage)
Next a

and the event handler is as follows

Private Sub btn_click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
//code to run in here
End Sub

Anyone have any ideas as I am fresh out and cant fined anythig else, this apparently should work, but as stated the event handler does not fire and the imagebuttons are linked back to the same page so it relods, even though i havent specified that.
Please help


Thanks
Lee

Ben Collier
04-04-2007, 02:30 PM
But i want the images to be clickable to view a larger image, so i need to be able to point to the clickevent handler on the fly.


Could you not just add a link around each image that take the user to the image? Or a link to a page that displays larger images?

For example:
The user click on an image (which links to /images/yourimage.jpg etc)
or the user clicks on an image (which links to showimage.aspx?image=123)

any use?

Ben

lee2006
04-04-2007, 02:51 PM
hi there thanks for the swift reply, thats souns alot esier than the idea i had
Only question is how do u add a link to an asp:image, there is no navigation url or anyhting like that
and when the url is passed to the page e.g
pview.aspx?image=~/Art.gif
how do i get that url to the image pn the page
i am using vb
thank you for your help
here from you soon
lee

lee2006
04-04-2007, 02:53 PM
I have check the imagebutton too and the only option is postbackurl as is is a button not a link
any ideas
lee

Ben Collier
04-04-2007, 03:17 PM
Wrap an image in a hyperlink-
<asp:HyperLinkID="HyperLink1"runat="server">
<asp:ImageID="Image1"runat="server"/></asp:HyperLink>


hyperlink1.NavigateUrl = url from xml
image1.ImageUrl = url from xml


Now to display an image in a page from a query string:

Dim Image As String = Request.QueryString("image")
Image1.ImageUrl = Image

Now accessing yourfile.aspx?image=123.jpg will show image 123.jpg in the control image1.

Hope this helps!

Also looking into the gridview control may be of use! You can select xml as a datasource.

Good luck!
Ben

lee2006
04-04-2007, 03:25 PM
hi there thanks so much that works great, and dont mean to be anyoying but i want the image page to open as a popup
I have a popu method decalred on my page, but how do i call a vb method when the button is clicked, the method is

Public Shared Sub OpenPopUp(ByVal opener As System.Web.UI.WebControls.WebControl, ByVal PagePath As String, ByVal windowName As String, ByVal width As Integer, ByVal height As Integer)
'create sub variables
Dim clientScript As String
Dim windowAttribs As String
'Building Client side window attributes with width and height.
'Also the the window will be positioned to the middle of the screen
windowAttribs = "width=" & width & "px," & _
"height=" & height & "px," & _
"left='+((screen.width -" & width & ") / 2)+'," & _
"top='+ (screen.height - " & height & ") / 2+'"
'Building the client script - window.open, with additional parameters
clientScript = "window.open('" & PagePath & "','" & windowName & "','" & windowAttribs & "');return false;"
'register the script to the clientside click event of 'opener' control
opener.Attributes.Add("onClick", clientScript)
End Sub

got this from a tutorial, or do you know an esier way to do what you just described but in a popup window rather than the same window that i can se the size for

hope you can help and thanks so much for you input

lee

Ben Collier
04-04-2007, 03:49 PM
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language=javascript>
var newwindow;
function popup(url)
{
newwindow=window.open(url,'Image','height=400,widt h=200');
if (window.focus) {newwindow.focus()}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:hyperlink ID="hyperlink1" runat="server" NavigateUrl="javascript:popup('123.jpg')">LinkButton</asp:hyperlink>

</form>
</body>
</html>

Try working from this! Just dynamically set the navigate url to "javascript:popup('" & image url & "')"

Goodluck

lee2006
04-04-2007, 06:32 PM
Hi there
thanks alot for all your help worked like a charm
So glad somone knows how to explane things in english
Thanks again
Lee

Ben Collier
04-04-2007, 09:25 PM
No problem! Glad to help!