Page.ClientScript.RegisterStartupScript method

Hello guys!
I need your help.
This is code behind in my net page (C#).

I use the Page.ClientScript.RegisterStartupScript method, but I need print in alert the resume numbers of strDAA variable.
If I have only one variable strDAA the script working, but if I have more numbers of variable strDAA the script print only last value.

I need resume all operation execute in my script in the alert window.

Could you please help?
Thanks you very much for your help

 protected void btnUpdate_Click(object sender, EventArgs e)
    {
        OdbcCommand cmd = new OdbcCommand();
        myConnectionString.Open();
        cmd.Connection = myConnectionString;
        int myValue = 0;

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkUpdate = (CheckBox)
               GridView1.Rows[i].Cells[0].FindControl("chkSelect");

            if (chkUpdate != null)
            {
                if (chkUpdate.Checked)
                {
                    ....

                    strDAA = ((TextBox)
                        GridView1.Rows[i].FindControl("DAA")).Text;
                    ....

                        string myStringVariable = string.Empty;
                        myStringVariable = "OK for DAA " + strDAA + " ";
                    }
                    else
                    {
                        string myStringVariable = string.Empty;
                        myStringVariable = "Error for DAA " + strDAA + " ";
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
                        myValue = 1;
                    }
                }
            }

        }

        if (myValue == 0)
        {
            GridViewBind();
        }

        Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);

        myConnectionString.Close();
        myConnectionString.Dispose();
    }

And it would because you are using the same name for the RegisterStartupScript for each iteration. So you have two choices.

One, build your JavaScript alerts in a StringBuilder and then register a single startup script after the for loop, or Two, give a unique startup name to each iteration.

Example of Idea One:


        StringBuilder sb  = new StringBuilder();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkUpdate = (CheckBox)
               GridView1.Rows[i].Cells[0].FindControl("chkSelect");

            if (chkUpdate != null)
            {
                if (chkUpdate.Checked)
                {
                    ....

                    strDAA = ((TextBox)
                        GridView1.Rows[i].FindControl("DAA")).Text;
                    ....

                        string myStringVariable = string.Empty;
                        myStringVariable = "OK for DAA " + strDAA + " ";
                    }
                    else
                    {
                        string myStringVariable = string.Empty;
                        myStringVariable = "Error for DAA " + strDAA + " ";
                        sb.AppendFormat("alert('{0}');", myStringVariable);
                        //ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
                        myValue = 1;
                    }
                }
            }

        }
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", sb.ToString(), true);

Example of Idea Two:

for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkUpdate = (CheckBox)
               GridView1.Rows[i].Cells[0].FindControl("chkSelect");

            if (chkUpdate != null)
            {
                if (chkUpdate.Checked)
                {
                    ....

                    strDAA = ((TextBox)
                        GridView1.Rows[i].FindControl("DAA")).Text;
                    ....

                        string myStringVariable = string.Empty;
                        myStringVariable = "OK for DAA " + strDAA + " ";
                    }
                    else
                    {
                        string myStringVariable = string.Empty;
                        myStringVariable = "Error for DAA " + strDAA + " ";
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert" + i.ToString(), "alert('" + myStringVariable + "');", true);
                        myValue = 1;
                    }
                }
            }

        }

thank you for help.

This is new version of my code behind, but does not open window alert… :frowning:

 protected void btnUpdate_Click(object sender, EventArgs e)
    {
        OdbcCommand cmd = new OdbcCommand();
        myConnectionString.Open();
        cmd.Connection = myConnectionString;
        int myValue = 0;


        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkUpdate = (CheckBox)
               GridView1.Rows[i].Cells[0].FindControl("chkSelect");

            if (chkUpdate != null)
            {
                if (chkUpdate.Checked)
                {
                    ....

                    strDAA = ((TextBox)
                        GridView1.Rows[i].FindControl("DAA")).Text;
                    ....

                        string myStringVariable = string.Empty;
                        myStringVariable = "OK for DAA " + strDAA + " ";
                    }
                    else
                    {
                        string myStringVariable = string.Empty;
                        myStringVariable = "Error for DAA " + strDAA + " ";
                        sb.AppendFormat("alert('{0}');", myStringVariable);
                        myValue = 1;
                    }
                }
            }

        }

        if (myValue == 0)
        {
            GridViewBind();
        }

        ClientScript.RegisterStartupScript(this.GetType(), "myalert", sb.ToString(), true);

        myConnectionString.Close();
        myConnectionString.Dispose();
    }

Check you page output and see what it wrote to the page.

Ok, thank you.
now with last modifies open window alert.
but I have one window alert for each strDAA variable…
the idea was to summarize in a single window alert all variables strDAA affected by changes in gridview…

 protected void btnUpdate_Click(object sender, EventArgs e)
    {
        OdbcCommand cmd = new OdbcCommand();
        myConnectionString.Open();
        cmd.Connection = myConnectionString;
        int myValue = 0;


        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkUpdate = (CheckBox)
               GridView1.Rows[i].Cells[0].FindControl("chkSelect");

            if (chkUpdate != null)
            {
                if (chkUpdate.Checked)
                {
                    ....

                    strDAA = ((TextBox)
                        GridView1.Rows[i].FindControl("DAA")).Text;
                    ....

                        string myStringVariable = string.Empty;
                        myStringVariable = "OK for DAA " + strDAA + " ";
                        sb.AppendFormat("alert('{0}');", myStringVariable);
                    }
                    else
                    {
                        string myStringVariable = string.Empty;
                        myStringVariable = "Error for DAA " + strDAA + " ";
                        sb.AppendFormat("alert('{0}');", myStringVariable);
                        myValue = 1;
                    }
                }
            }

        }

        if (myValue == 0)
        {
            GridViewBind();
        }

        ClientScript.RegisterStartupScript(this.GetType(), "myalert", sb.ToString(), true);

        myConnectionString.Close();
        myConnectionString.Dispose();
    }

In that case, remove the alert(’ and '); from the string builder and instead just write the text and a newline character. Then place the alert() in the RegisterStartupScript as shown below.

 protected void btnUpdate_Click(object sender, EventArgs e)
    {
        OdbcCommand cmd = new OdbcCommand();
        myConnectionString.Open();
        cmd.Connection = myConnectionString;
        int myValue = 0;


        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkUpdate = (CheckBox)
               GridView1.Rows[i].Cells[0].FindControl("chkSelect");

            if (chkUpdate != null)
            {
                if (chkUpdate.Checked)
                {
                    ....

                    strDAA = ((TextBox)
                        GridView1.Rows[i].FindControl("DAA")).Text;
                    ....

                        string myStringVariable = string.Empty;
                        myStringVariable = "OK for DAA " + strDAA + " ";
                        sb.AppendFormat("{0}{1}", myStringVariable, Environment.NewLine);
                    }
                    else
                    {
                        string myStringVariable = string.Empty;
                        myStringVariable = "Error for DAA " + strDAA + " ";
                        sb.AppendFormat("{0}{1}", myStringVariable, Environment.NewLine);
                        myValue = 1;
                    }
                }
            }

        }

        if (myValue == 0)
        {
            GridViewBind();
        }

        ClientScript.RegisterStartupScript(this.GetType(), "myalert", string.Format("alert('{0}');", sb.ToString()), true);

        myConnectionString.Close();
        myConnectionString.Dispose();
    }

thank you, but now does not open window alert… :confused:

 protected void btnUpdate_Click(object sender, EventArgs e)
    {
        OdbcCommand cmd = new OdbcCommand();
        myConnectionString.Open();
        cmd.Connection = myConnectionString;
        int myValue = 0;


        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkUpdate = (CheckBox)
               GridView1.Rows[i].Cells[0].FindControl("chkSelect");

            if (chkUpdate != null)
            {
                if (chkUpdate.Checked)
                {
                    ....

                    strDAA = ((TextBox)
                        GridView1.Rows[i].FindControl("DAA")).Text;
                    ....

                        string myStringVariable = string.Empty;
                        myStringVariable = "OK for DAA " + strDAA + " ";
                        sb.AppendFormat("{0}{1}", myStringVariable, Environment.NewLine);
                    }
                    else
                    {
                        string myStringVariable = string.Empty;
                        myStringVariable = "Error for DAA " + strDAA + " ";
                        sb.AppendFormat("{0}{1}", myStringVariable, Environment.NewLine);
                        myValue = 1;
                    }
                }
            }

        }

        if (myValue == 0)
        {
            GridViewBind();
        }

        ClientScript.RegisterStartupScript(this.GetType(), "myalert", string.Format("alert('{0}');", sb.ToString()), true);

        myConnectionString.Close();
        myConnectionString.Dispose();
    }

Check your page output again, check for any JavaScript syntax errors, etc.

the output is correct and summarize all variables strDAA affected by changes in gridview.
but now does not open in a single window alert

Can you paste the output here? I’d like to see what it wrote out.
Any JavaScript errors on the page?

thank you.
this is the output and I don’t have JavaScript error in net page.

OK for DAA 30459. OK for DAA 16179;

Ok, solved.
The problem is

Environment.NewLine

I changed this line:

sb.AppendFormat("{0}{1}", myStringVariable, Environment.NewLine);

with:

sb.AppendFormat("{0}{1}", myStringVariable, "\\\
"); 

and now I have in a single window alert summarize all variables strDAA affected by changes in gridview.

thank you