Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Image Button Click ?

Posted on 2010-08-13
10
Medium Priority
?
424 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1200 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 400 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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

704 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