cms9651
November 27, 2012, 5:43pm
1
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();
}
cpradio
November 27, 2012, 5:59pm
2
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:
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;
}
}
}
}
cms9651
November 27, 2012, 6:25pm
3
thank you for help.
This is new version of my code behind, but does not open window alert…
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();
}
cpradio
November 27, 2012, 6:41pm
4
Check you page output and see what it wrote to the page.
cms9651
November 27, 2012, 6:47pm
5
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();
}
cpradio
November 27, 2012, 7:20pm
6
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();
}
cms9651
November 27, 2012, 8:18pm
7
thank you, but now does not open window alert…
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();
}
cpradio
November 27, 2012, 8:20pm
8
Check your page output again, check for any JavaScript syntax errors, etc.
cms9651
November 27, 2012, 8:25pm
9
the output is correct and summarize all variables strDAA affected by changes in gridview.
but now does not open in a single window alert
cpradio
November 27, 2012, 10:01pm
10
Can you paste the output here? I’d like to see what it wrote out.
Any JavaScript errors on the page?
cms9651
November 28, 2012, 8:14am
11
thank you.
this is the output and I don’t have JavaScript error in net page.
OK for DAA 30459. OK for DAA 16179;
cms9651
November 28, 2012, 9:08am
12
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