?
Solved

Checking if Startup Script is already executed

Posted on 2010-09-01
2
Medium Priority
?
277 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 1000 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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

764 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