SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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

    Code:
     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();
        }

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,215
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cms9651 View Post
    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.
    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:
    Code:
            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:
    Code:
    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;
                        }
                    }
                }
    
            }

  3. #3
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thank you for help.

    This is new version of my code behind, but does not open window alert...
    Code:
     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();
        }

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,215
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Check you page output and see what it wrote to the page.

  5. #5
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    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...
    Code:
     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();
        }
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,215
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code:
     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();
        }

  7. #7
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thank you, but now does not open window alert...
    Code:
     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();
        }
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,215
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Check your page output again, check for any JavaScript syntax errors, etc.

  9. #9
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    the output is correct and summarize all variables strDAA affected by changes in gridview.
    but now does not open in a single window alert
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,215
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Can you paste the output here? I'd like to see what it wrote out.
    Any JavaScript errors on the page?

  11. #11
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    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.
    Code:
    OK for DAA 30459. OK for DAA 16179;
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  12. #12
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, solved.
    The problem is
    Code:
    Environment.NewLine
    I changed this line:
    Code:
    sb.AppendFormat("{0}{1}", myStringVariable, Environment.NewLine);
    with:
    Code:
    sb.AppendFormat("{0}{1}", myStringVariable, "\\n");
    and now I have in a single window alert summarize all variables strDAA affected by changes in gridview.

    thank you
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •