Solved

Image Button Click ?

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

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now