Solved

Checking if Startup Script is already executed

Posted on 2010-09-01
2
274 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

815 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now