• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 724
  • Last Modified:

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

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.

Thanks,
Scott
0
sneidig
Asked:
sneidig
  • 4
  • 2
2 Solutions
 
osiris247Commented:
HI

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.

Steve
0
 
sneidigAuthor Commented:
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?
0
 
svyCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sneidigAuthor Commented:
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.
0
 
sneidigAuthor Commented:
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.
0
 
svyCommented:
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.
0
 
sneidigAuthor Commented:
Good to know, thanks.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now