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

x
?
Solved

RegisterStartupScript

Posted on 2004-10-12
6
Medium Priority
?
1,327 Views
Last Modified: 2012-06-27
Hi Experts

I have injected some startup javascript from my ASP.NET/VB.NET code, and this javascript runs successfully when the page loads. I've used Page.RegisterStartupScript(<name>, <script>) to do this.

In certain circumstances I want to unregister this startup code so it doesn't execute - any ideas how to do this?

Thanks
0
Comment
Question by:dalsandhu
[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
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:onskee
ID: 12294294
Unless I'm misunderstanding your question, you can prevent it from loading by placing it in a conditional -
If blnMyCondition = True Then
    ' do something here
Else
   Page.RegisterStartupScript("blnMyCondition", "<script language......>")
End IF

If you can provide a code sample or some more details, we might be able to give you a more direct answer.
0
 

Author Comment

by:dalsandhu
ID: 12298154
I use this method in the Page_Load sub to set the focus to the first field on my form.

The user can update values in certain fields which cause a trip to the server and the page to reload. After the user has updated these fields I want the focus to move to the next field rather than back to the first field. The problem is that the function fired by the field change event, fires after the Page_Load sub. So, I can't put a conditional in the Page_Load code as at that point there is no knowledge of the field change event.

If this isn't clear I'll provide the code.

Thanks
0
 
LVL 3

Expert Comment

by:onskee
ID: 12298215
I think you may be able to use the textchanged event to call your startup script.

Private Sub txtBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBox.TextChanged
    Page.RegisterStartupScript("MoveFocus", "<script language>document.forms(0)." & txtNewBox.ClientID & ".focus();</script>")
End Sub


If I'm still off, please let me know.

Thx.
0
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 

Author Comment

by:dalsandhu
ID: 12299293
That's exactly what I've done, so you are not 'off' at all.

The focus does change, which is good, but the Page_Load script also fires. This has the effect that both the field I want, and the first field, are highlighted. Perhaps I'm being pernickety, but I don't like this, so I want to turn the first script off.

I've tried 'overwriting' the script, i.e. calling the method with the same ID, but this doesn't work - the original script fires.

I've just thought, I could have the movefocus script retrieve the name of the field to move to based on the value in a hidden field. The page_load and onchange procs could then set the value of this hidden field. Hopefully the hidden field value will be set before the movefocus script fires.

I'd still like to know how to unregister the script though, as this would be easier.

Thanks
0
 
LVL 3

Accepted Solution

by:
onskee earned 2000 total points
ID: 12299487
Place the code you don't want to re-run (in Page_Load or anywhere else) within
If Not IsPostBack
  Page.RegisterStartupScript(...)
End IF
This will only fire on the first load.

AFAIK, a startupscript can't be 'unregistered', since it's simply emitted to the browser when the page has loaded.

Also, you can use IsStartupScriptRegistered("KeyName") (returns true/false) to see if a script is already registered.

HTH
0
 

Author Comment

by:dalsandhu
ID: 12299634
I'll go with the 'not ispostback' - thanks onskee.

I wanted to have my startup script outside the ispostback conditional, as there are cancel, save & navigation buttons that reload the page. I can repeat the startup script register in the onclick event procs for these buttons easily enough though.

Cheers,
Dal
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

721 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