
Originally Posted by
cms9651
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;
}
}
}
}
Bookmarks