Solved

Image Button Click ?

Posted on 2010-08-13
10
420 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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