Solved

Checking if Startup Script is already executed

Posted on 2010-09-01
2
275 Views
Last Modified: 2013-11-05

Experts,

I am new to ASP.NET and I am just looking to gain an understanding on something. I've searched around but I can't find anything that answers my question completely.

In my ASP.NET application I want to display an alert box to the user if there is an error. In my Catch Block I have the line of code below and it works.

Page.ClientScript.RegisterStartupScript(Me.GetType(), "error", "alert('" & ex.Message & "');", True)


From what I've read, it says you should call the IsStartupScriptRegistered method to determine whether a startup script with a given key and type pair is already registered and avoid unnecessarily attempting to add the script.


I've seen exampes where the IsStartupScriptRegistered method is used in an IF statement and if the script isn't registered then the line of code I'm already using will be executed. But what I cant find is how to handle situations where the script is already registered. (How do I get my alert box to display?...what would be the syntax I use then?)

What is the downside of RegisterStartupScript multiple times?

Maybe I'm overthinkng this. But any input is greatly appreciated.
0
Comment
Question by:jvoconnell
[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
2 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 250 total points
ID: 33615673
Well, you must differentiate between server and client load. When a page loads, first it loads on server, so it executes the code you placed on Page_Load server code event. Inside Page_Load event you can use Me.IsPostBack() (vb) or this.IsPostBack() (c#) to know when the page is loading at first time or when it has been reloaded by a PostBack.

Because to register client code using RegisterStartupScript is neccesary to give the script a key, you must use IsStartupScriptRegistered to check if that key is already added. But maybe you must consider another way of doing this...

It seems that you want to check any error condition each time your page loads, and if there is an error, show a message in client code to the user. If this is the scenario, maybe it would be better to do the following:

Put a Hidden server control (or a INPUT TYPE="hidden" runat="server" HTML tag) in your page. This control will not be visible to the user. In your Page_Load event you can check for your error condition and, if there is an error, load your hidden with the message you want to show to the user:

(Because I don't know which server language you're using I'll write VB):
If (error) Then
    Me.hidErrorMsg.Value = "your error message"
Else
    Me.hidErrorMsg.Value = String.Empty
End If

Then, in client code, write a function that runs on every page load at client side and checks if there is text inside your hidden control. If there is text, then show an alert with the text:

<script type="text/javascript">
    window.onload = function() {
        if(document.getElementById('<%=Me.hidErrorMsg.ClientID%>').value.length > 0) {
            alert(document.getElementById('<%=Me.hidErrorMsg.ClientID%>').value);
        }
    }
</script>

Hope that helps.
0
 
LVL 1

Author Comment

by:jvoconnell
ID: 33665316
Hi Roland,  I apologize for the late response. Something unexpectedly came up and I was away. I can use your answer to accomplish what I need to do.  Thank you very much and once again, I'm sorry for taking so long to respond.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

726 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