[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem to show the message

Posted on 2014-08-06
15
Medium Priority
?
149 Views
Last Modified: 2014-08-12
Hi,
I don't know why the prompt message is sometimes shown but sometimes not. For instance, 1st message below can be shown but 3rd one cannot be shown.

                        Page.ClientScript.RegisterStartupScript(this.GetType(), "Information", "javascript:alert('Invalid number');", true);
                        ...
                        catch (Exception ex2)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "Information", "javascript:alert('exception got');", true);
                            return;
                        }
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "Information", "javascript:alert('Have applied the change fine');", true);
                        ...

Open in new window

if I put something like

lb_msg.Text=...;

Open in new window

instead of 3rd message line above, then that line can be executed. why?
0
Comment
Question by:HuaMinChen
  • 7
  • 7
15 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40243107
The keys for the script have to be unique (i.e. you can't register two different scripts with "Information" as the key). Try changing "Information" in the 3rd one to something else and seeing if that makes a difference.
0
 
LVL 84

Expert Comment

by:David Johnson, CD, MVP
ID: 40243116
You hit the nail on the head, Carl
A startup script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.
http://msdn.microsoft.com/en-us/library/z9h4dk8y%28v=vs.110%29.aspx
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40243170
Thanks all.
I've tried to instead put these
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoA", "javascript:alert('Invalid number');", true);
                        ...
                        catch (Exception ex2)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoB", "javascript:alert('exception got');", true);
                            return;
                        }
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoC", "javascript:alert('Have applied the change fine');", true);
                        ...

Open in new window

but the 3rd message still cannot be displayed. But if I put something like

lb_msg.Text=...;

Open in new window


instead of 3rd message line above, then that line can be executed. why?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40243240
What do you mean by "can't be executed"? Do you mean it just doesn't do anything, or does it raise an exception?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40243252
I mean 3rd message line above does not show one message as expected. But if I put something like

lb_msg.Text=...;

instead of 3rd message line above, then that line can be executed.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40243255
When you "view source" on your outputted HTML do you see the line of javascript?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40243262
I choose "View source" on the web page, and then try to find the message

Have applied the change fine

Open in new window


but I cannot find out such message within that.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40243284
Well, what you have should work fine; even having two startup scripts should work ok.
Is there something else in your code that could be causing it not to reach that line?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40243291
That line can be reached. If not, why this line below

lb_msg.Text=...;

can be executed, if I put it instead of the above 3rd message line.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40243298
Can you post your code as you currently have it? That's everything from the first "RegisterStartupScript" line, up to and including the third one.
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40243307
To provide both code-behind and markup page to you here?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40243309
I just wonder why one message can be shown there but one other message cannot.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 645 total points
ID: 40243316
Just the code-behind will do. There is not reason why what you are trying to do shouldn't be working - that's why I wanted to see the whole of your code to see if there is something odd that is preventing the line from executing.

Maybe that line is throwing and exception and you are suppressing it somewhere with and empty catch block.

If I run this:
        Try
            ClientScript.RegisterStartupScript(Me.GetType(), "Information", "alert('Hello world');", True)
        Catch ex As Exception
            ClientScript.RegisterStartupScript(Me.GetType(), "Information", "alert('Something went wrong');", True)
        End Try

        ClientScript.RegisterStartupScript(Me.GetType(), "Information", "alert('Hello another world');", True)

Open in new window

I only get one message, as expected, because of the duplicate key name.
However, this:
        Try
            ClientScript.RegisterStartupScript(Me.GetType(), "Information", "alert('Hello world');", True)
        Catch ex As Exception
            ClientScript.RegisterStartupScript(Me.GetType(), "Information", "alert('Something went wrong');", True)
        End Try

        ClientScript.RegisterStartupScript(Me.GetType(), "Information2", "alert('Hello another world');", True)

Open in new window

Works as expected.

So I can only think that you have something somewhere in your code that is preventing it from behaving as it should.

By the way, what version of the .Net framework are you using?
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 40245263
Many thanks Carl. Here is code-behind.

using System;
using System.IO;
using System.IO.Compression;
using System.Text; // This was needed to make ASCIIEncoding function
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.SqlTypes;
using System.Data;
using System.Data.Common;
using System.Xml;
using System.Windows;
using System.Data.OleDb;
using System.Security.Cryptography; // This was needed to make everything related to MD5 function

namespace Verify_ite
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void lb_reg_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://192.168.168.1/Add_ite2_dev/Default.aspx");
        }
        protected void ValButton_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Mssqlconn2"].ConnectionString);
            //OleDbConnection conn = null;
            //conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:Database Password=?????;Data Source=C:\inetpub\VS2012\DB1.accdb");
            int ret_val = 0;
            if (tb_ver_code.Text == "")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoA", "javascript:alert('testing1');", true);
                return;
            }
            else
            {
                conn.Open();
                try
                {
                    SqlCommand cmd = new SqlCommand("select COUNT(*) from ite_master where verify_code=@ver_cd and isnull(verified,0)=1", conn);
                    cmd.Parameters.Add("ver_cd", SqlDbType.Decimal).Value = Convert.ToDecimal(tb_ver_code.Text);
                    ret_val = Convert.ToInt32(cmd.ExecuteScalar());
                    if (ret_val > 0)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoB", "javascript:alert('testing2');", true);
                        //lb_msg.Text = "testing2";
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoC", "javascript:alert('testing3');", true);
                    return;
                }
                finally
                {
                    conn.Close();
                }
                conn.Open();
                try
                {
                    SqlCommand cmd = new SqlCommand("select COUNT(*) from ite_master where verify_code=@ver_cd and isnull(verified,0)=0", conn);

                    cmd.Parameters.Add("ver_cd", SqlDbType.Int).Value = Convert.ToInt32(tb_ver_code.Text);
                    //cmd.Parameters.Add("ver_cd",OleDbType.Decimal).Value = Convert.ToDecimal(tb_ver_code.Text);
                    ret_val = Convert.ToInt32(cmd.ExecuteScalar());
                    if (ret_val == 0)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoD", "javascript:alert('Invalid number');", true);
                        //lb_msg.Text = "Invalid number";
                        return;
                    }
                    else
                    {
                        try
                        {
                            SqlCommand cmd2 = new SqlCommand("update ite_master set verified=1 where verify_code=@ver_cd", conn);
                            cmd2.Parameters.Add("ver_cd", SqlDbType.Int).Value = Convert.ToInt32(tb_ver_code.Text);
                            cmd2.ExecuteNonQuery();
                        }
                        catch (Exception ex2)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoE", "javascript:alert('exception got');", true);
                            return;
                        }
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoF", "javascript:alert('Have applied the change fine');", true);
                        //lb_msg.Text = "Have applied the change fine";
                    }
                }
                catch (Exception ex)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoG", "javascript:alert('testing4');", true);
                    return;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }
}

Open in new window


In above, I don't know why the message with key InfoF, is not shown as expected. But if I instead put "lb_msg.Text" line that is right after it, then such line can be called, as I can see the value of such label.

I am using .Net framework v4.0.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40255216
I've tried your code in a test environment and I can hit all of the javascript messages fine. The only route through your code I can see that wouldn't hit a message is if one of the conn.Open() lines threw an exception - seeing as they sit outside of your try...catch blocks.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month20 days, 1 hour left to enroll

872 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