• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Checking if Startup Script is already executed


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
jvoconnell
Asked:
jvoconnell
1 Solution
 
Luis PérezSoftware Architect in .NetCommented:
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
 
jvoconnellAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now