Solved

Checking if Startup Script is already executed

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

757 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

22 Experts available now in Live!

Get 1:1 Help Now