ASP.NET里弹出窗口的问题,单引号所引发的错误.在线等!
代码:
private void btnSumbit_Click(object sender, System.EventArgs e)
{
if(lbDircetList.Items.Count == 0)
{
lblMessage.Text = "error! ";
}
else
{
string str= " ";
for (int i=0;i <this.lbDircetList.Items.Count;i++)
{
str = str+ " ' "+this.lbDircetList.Items[i].Value+ " ' "+ ", ";
}
str = str.Remove(str.Length - 1,1);
Page.RegisterStartupScript( " ", " <script> window.open( 'rp_Total.aspx?departmentcode= "+str.ToString()+ " '); </script> ");
}
}
--------------------------
出错:
网页左下角出现黄色感叹号,里面提示:
必须要有 ') '
--------------------------
分析:
因为在str = str+ " ' "+this.lbDircetList.Items[i].Value+ " ' "+ ", ";
加了 " ' "而引起的.但我确实要连单引号都传过去,怎么做呢?即是这样:
例如:
string str = '000001 ', '000002 '
但我不用窗口弹出方式,而用Session传递参数却正常.怎样才能在弹出窗口里打开页面没有出错呢?应该修改什么地方?谢谢!
------解决方案--------------------作为url传值的话,str.UrlEncode()一下
------解决方案--------------------Page.RegisterStartupScript( " ", " <script> window.open( 'rp_Total.aspx?departmentcode= "+Server.UrlEncode(str.ToString())+ " '); </script> ");
------解决方案--------------------要单引号?
str = HttpUtility.UrlEncode(str);
------解决方案--------------------str = str .replace( " ' ", "\\ ' ");
------解决方案--------------------string 换成stringBuilder试试