'return false' in linkbutton javascript stops the script from running

C

crferguson

Hello all! I'm thinking this one has probably been beaten to death in
the past, but I can't find an answer to this specific issue I'm having
with a javascript attached to a linkbutton.

What I'm doing is I have a page with a dozen or so linkbuttons on it.
The linkbuttons open a popup information window containing additional
details about some categories on the parent page.

Heres the problem: the parent page is pretty long and some of those
linkbuttons are near the bottom. So, when I click one of those
linkbuttons at the bottom, it does a postback, opens the popup window,
and -->makes the parent page shoot back up to the top due to the
reload<--. This means every time a user clicks one of those links they
have to scroll back to the bottom of the parent page to return to their
work.

I figured the best solution to this was to stop the postback by adding
"return false" to the script. But if I add "return false" to the java
script the popup window doesn't open. Does someone know why the
"return false" stops the popup from opening? Here's my code:

strScript = "<script language='javascript'>"
strScript += "window.open('" & Global.EmployeeInfoPage
& "',"
strScript += "'emp" & eEmp.PositionNumber &
"','toolbar=no,"
strScript += "addressbar=no,menubar=no,"
strScript += "left=40,top=40,"
strScript += "width=620,height=165');"
strScript += "</script>"

p = HttpContext.Current.Handler
p.RegisterClientScriptBlock("NewEntry", strScript)

That example works, but causes the postback on the requesting page.
Adding the "return false;" right before the last line "</script>" stops
the popup from opening...

Thank you all for any insights!

Cory
 
C

crferguson

Or, here's a second idea: how can I get the parent page from running
the Page_Load event upon clicking those linkbuttons? If I set a "If
not IsPostBack then" condition in the Page_Load event, won't that stop
the buttons from working altogether?

Thanks all!
 
M

mnichols

Hi,

IsPostBack is a boolean value that indicates whether the form has been
submitted back. IsPostBack will be false the first time the page is
loaded and true everytime an event triggers a PostBack ( i.e. the form
is submitted).

Regarding "returning false;".....if you return false from an onclick
event for a submit button in an html form then the submit action is
cancelled. Unfortunately that is quite different than what you are
doing. A Linkbutton hides the HTML markup from you so this approach will
not work at all.

The simplest way for you to make your code work would be this:
- For each LinkButton add an anchor just before the button:
<a id="uniqueId" />
- In your javascript make this change:
strScript += "left=40,top=40,"
strScript += "width=620,height=165');"
strScript += "location='#uniqueId';"
strScript += "</script>"
- I assume that you have a number of linkbuttons with uniqueIDs of some
sort. Use the same UniqueIds for the anchors.
- The result will be as before but your page will also scroll back down
to the button after the postback.

If you were to start fresh I would recommend this:
- forget the LinkButton
- use a regular button with an onclick event (return false is not required):
<input type="button" value="Click Me"
onclick="javascript:showDetails('uniqueId')">
- In the <head> section add the following:
<script type="text/javascript">
function showDetails(which)
{
window.open('page'+which,'detailwindow','toolbar=no,addressbar=no,menubar=no,left=40,top=40,width=620,height=165');
}
</script>

Obviously you must be starting off, and you haven't posted much code, so
I doubt I have given you a perfect solution but I hope I have given you
some better ideas.

good luck

mn
 
C

crferguson

Thank you for the new approach and great information. I'm going to
give that a try and see what happens. If you couldn't tell, I'm very
new to .NET and it's generous people like you who are making it
possible for me to learn at a rapid rate! Thanks!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top