Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Problem to show the message

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?
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
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
Avatar of Peter Chan

ASKER

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?
What do you mean by "can't be executed"? Do you mean it just doesn't do anything, or does it raise an exception?
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.
When you "view source" on your outputted HTML do you see the line of javascript?
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.
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?
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.
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.
To provide both code-behind and markup page to you here?
I just wonder why one message can be shown there but one other message cannot.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.