Solved

Image Button Click ?

Posted on 2010-08-13
10
422 Views
Last Modified: 2012-05-10
I have the following code:

 <li><a>
<asp:ImageButton ID="imgLogin" ImageUrl='<%# Bind("Image") %>' runat="server" PostBackUrl='<%# Bind("Path") %>'
AlternateText='<%# Bind("AltText") %>' OnClick="ImageButton_Click" />
</a>
</li>

I use the <a></a> so that I can change the background image via CSS.

Basically all I want to do is have : OnClientClick call the code behind:

    Public Sub ImageButton_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
....
....
    End Sub

Any ideas?
0
Comment
Question by:directxBOB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 100 total points
ID: 33429486
Can you change the <a> to a <span>? That way you can still do the CSS change and it won't have an inherent onclick type event.
0
 
LVL 7

Expert Comment

by:urir10
ID: 33429499
You mean you want to run code behind and redirect the page to another url? if so you cannot do it that way but you can redirect the  page in Code Behind.
0
 

Author Comment

by:directxBOB
ID: 33429509
I can, even when I remove the <a></a> the event is not called. Is this as a result of the image buttons being within a repeater?
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:directxBOB
ID: 33429526
The above list is a menu system built from a asp:repeater

I want the OnClick Event to call the same method in the code behind so that I can capture the name of the button last clicked.

In the DataBound I will then check the session for the last clicked and change the background of the image to a differnt color.
0
 
LVL 7

Assisted Solution

by:urir10
urir10 earned 300 total points
ID: 33429564
if you want to capture the click event inside a repeater u will need to use the ItemCommand event of the repeater

protected sub Repeater_ItemCommand(sender as Object, e as RepeaterCommandEventArgs") handles repeater1.OnItemCommand

if e.CommandName = "ImageButtonClick" then
   

end if
0
 

Author Comment

by:directxBOB
ID: 33429869
                       <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="Repeater_ItemCommand">
                            <HeaderTemplate>
                                <ul id="navbar">
                            </HeaderTemplate>
                            <ItemTemplate>
                                <li><span>
                                    <asp:ImageButton ID="imgLogin" ImageUrl='<%# Bind("Image") %>' runat="server" PostBackUrl='<%# Bind("Path") %>'
                                        AlternateText='<%# Bind("AltText") %>' /></span> </li>
                            </ItemTemplate>
                            <FooterTemplate>
                                </ul>
                            </FooterTemplate>
                        </asp:Repeater>


codebehind:


    Protected Sub Repeater_ItemCommand(ByVal sender As Object, ByVal e As RepeaterCommandEventArgs)

        Try
            Dim Image As System.Web.UI.WebControls.ImageButton = TryCast(e.CommandSource, System.Web.UI.WebControls.ImageButton)
            Session.Add("LastLinkClicked", Image.ID)
        Catch ex As Exception

        End Try

    End Sub

For some reason the event doesn't kick off each every time you click a button.
0
 
LVL 3

Assisted Solution

by:revaluser
revaluser earned 100 total points
ID: 33429873
How Ever u are using anchor so make the anchor server side and use the below code

In Page Load
Use this

anchor.Attributes.Add("onclick","clickButton(event, imgLogin.ClientID, imgLogin.UniqueID)");


<script language="javascript" type="text/javascript">
//Just for Reference Change as per rquirement
    function clickButton(e, buttonid, btname)
    {
        var evt = e ? e : window.event;
       
        var bt = document.getElementById(buttonid);
       
        if (bt)
        {
            if(//Browser is IE)
            {
                __doPostBack(btname,'');
                return false;
            }
            else if (//Others Browsers)
            {
                bt.click();
                return false;
            }
        }
    }
</script>
0
 

Author Comment

by:directxBOB
ID: 33430162
@revaluser

Unfortunately I cannot use javascript. It's a development policy.... don't ask :(

Anyway to narrow this down a little further basically it appears the that ItemCommand isn't being called because the ImageButton has already navigated away from the page. So if I click the link that the page is already on then it gets called...

So any other alternatives?
0
 

Author Comment

by:directxBOB
ID: 33430402
Currently Trying to get this working with DataBound Event. Basically checking to see if the URL matches that as one of the imagebuttons, if so I save it in a session  and set the backgrouond image for that imagebutton in the session.
0
 
LVL 7

Accepted Solution

by:
urir10 earned 300 total points
ID: 33430429
"Anyway to narrow this down a little further basically it appears the that ItemCommand isn't being called because the ImageButton has already navigated away from the page. So if I click the link that the page is already on then it gets called...
"

This is why i said it cannot be dont , you cannot redirect the page and still expect it to excute the code on the same page. You can redirect the the page in code behind after you done storing it in session and what not.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need syntax help building hyperlink 2 41
How to use NFS (Network File System) in Asp.net mvc 5? 4 59
ASP.NET Calendar Control 5 26
Asp.Net Session Question 2 36
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question