Linkbutton event(s) triggers JS popup and code behind sub

Posted on 2006-06-06
Last Modified: 2008-03-06
If anyone can offer a solution, I'd like to set up some code that consists of an asp:linkbutton with an onClick event that calls a javascript function (which will open a popup window) and also calls a sub in the .vb file or codebehind.

I've tried working with the onClientClick method of asp:linkbutton to call the javascript function but haven't had success.  

My first attempts at getting this to work look something like this, though I'm getting a compilation error.

<asp:linkbutton id="lbProfile" runat="server" onClick="codeBehindMethod" onClientClick="javascript:newWin('/newprofilephoto.aspx?id=<asp:literal id='ltlGUID2' runat='server' />', 'profilephoto', 500, 350, 0, 0, 1);" text="Upload a new photo" />

If anyone can point out something I'm doing wrong, if I'm going about this the wrong way entirely, or if this isn't possible, then I would appreciate your knowledge. Please let me know if you need anymore details to solve this problem.

Question by:sneidig
    LVL 7

    Accepted Solution


    I think the error may have something to do with the asp:literal you have in you javascript call.  You need to pass a string to the function so what you have is most likely being rendered to the screen as a string not the value of the literal.

    Run in the browser and go View->Source to see what you are passing to the js function.


    Author Comment

    That's an area worth looking into.

    I've found that by cutting and moving the asp:literal to another part of the page so now the line looks like this:

    <asp:linkbutton id="lbProfile" runat="server" onClick="updateProfile" onClientClick="javascript:newWin('/newprofilephoto.aspx?id=', 'profilephoto', 500, 350, 0, 0, 1);" text="Upload a new photo" />

    The page loads, the updateProfile method is called, though the javascript function newWin is not being called. Is there something else I've got to do to get onClientClick to work? Or is there another way to call a javascript function that will work?
    LVL 4

    Assisted Solution

    Replace 'ltlGUID2' literal control with simple protected variable that contains your id.
    <asp:linkbutton id="lbProfile" runat="server" onClick="codeBehindMethod"
    onClientClick="javascript:newWin('/newprofilephoto.aspx?id=<%#myGUID2%>', 'profilephoto', 500, 350, 0, 0, 1);" text="Upload a new photo" />

    Since lbProfile is a server control  you have to do this.DataBind() in code behid for this to work.

    Author Comment

    That's interesting svy, I haven't used this.DataBind() before though I see the what you're trying to say. I added this.DataBind() to my page_load event but upon compilation I get a "Name 'this' is not declared" message. I've done some research to better understand this.DataBind() but haven't figured it out yet.

    And still I can't figure out exactly why onClientClick is not triggering the javascript function which is a larger problem for me.

    Author Comment

    Thank you both for trying to answer my questions. I ended up using window.opener.document.forms[0].submit(); after an event in the popup window is triggered so it's a completely different approach than what I was asking about in the thread but you still gave me some new ideas to think and learn about, so thank you. I split the points.
    LVL 4

    Expert Comment

    this.DataBind() in C# is the same as Me.DataBind() in VB or Page.DataBind() or simply DataBind()

    Now when I put this on my form it invokes the javascript function and then submits the form:
    <asp:linkbutton id="lbProfile" runat="server" onClick="updateProfile" onClientClick="newWin('/newprofilephoto.aspx?id=', 'profilephoto', 500, 350, 0, 0, 1);" text="Upload a new photo" />

    Make sure newWin does not return false, does not set event.cancelBubble to true and does not set event.returnValue to false.

    Author Comment

    Good to know, thanks.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    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…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 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

    20 Experts available now in Live!

    Get 1:1 Help Now