Solved

Image Button Click ?

Posted on 2010-08-13
10
423 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

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 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