Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Client Script
Message
General information
Forum:
ASP.NET
Category:
Other
Title:
Environment versions
OS:
Windows XP
Database:
MS SQL Server
Miscellaneous
Thread ID:
01317676
Message ID:
01319515
Views:
15
>>Hi everybody,
>>
>>I need to be able to dynamically show the message. The code I'm trying to use is
>
>Javascript code starts executing as soon as it's loaded (remember, the page isn't loaded all at once, it's streamed), so you've got a few problems with the code you've shown. First, your code is referencing controls before the controls are even available to the page, ex. "var dv = document.getElementById" will fail because the controls haven't been processed yet. So that code needs to be moved below the elements it references (or, just move the var statements to within the function). Also, the RegisterClientScriptBlock injects the Javascript right after the < form element of the page, so it's still going to run your code too soon. Use RegisterStartupScript instead, which places the code right before the < /form ending tag. That (should) fix the problem.

I tried the following:

Moved the JavaScript at the bottom of the ASPX, e.g.
  <script type="text/javascript">
    var dv = document.getElementById("spnStatFrame");
    var Spn = document.getElementById("spnStat");
    
    function DisplayMessage(Message)
    {            
        dv.style.display = "inline";        
        Spn.innerHTML = Message;
        setTimeout("dv.style.display = 'none'", 300000);
    }
</script>
    </form>
</body>
</html>
and in the code changed it to
protected void Form_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        if (e.Exception == null)
        {
            string Text = string.Format("You just inserted {0} {1}", e.Values["FirstName"], e.Values["LastName"]);
            string Script = string.Format("document.DisplayMessage(\"{0}\");", Text);
            this.ClientScript.RegisterStartupScript(this.GetType(), "Message", Script); 
        }
        else
        {
            string Text = "The data was entered in an incorrect format, please check it over.";
            string Script = string.Format("DisplayMessage(\"{0}\");", Text + " " + e.Exception.ToString().Replace(Environment.NewLine, "\\n").Replace("\"", "\\\""));
            this.ClientScript.RegisterStartupScript(this.GetType(), "Message", Script); 
            //this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Message", Script, true);
            e.ExceptionHandled = true;
            e.KeepInInsertMode = true;
        }
    }
Now I see the whole messages displayed at the bottom including DisplayMessage(

Do you see what is wrong here?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform