关于网友提出的“得得撸改名得得爱 ResponseEnd使得RegisterStartupScript失效的问题”问题疑问,本网通过在网上对“得得撸改名得得爱 ResponseEnd使得RegisterStartupScript失效的问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:得得撸改名得得爱 ResponseEnd使得RegisterStartupScript失效的问题
描述:
protected void Button1_Click(object sender, EventArgs e)
{
string js = "";
if(Session["name"] == null)
{
ScriptManager.RegisterStartupScript(Updatepanel1, Updatepanel1.GetType(), "Startup", js, false);
Response.End();//这句如果不加的话,是可以有提示框的
}
}
我在网上找了一下了,网上的解决方法是用Response.Write(js);来替代Page.ClientScript.RegisterStartupScript
但这样会有两个问题:
1.由于Response.Write是把JS代码打印在HTML文档的顶部,会使CSS失效
2.如果Button1的事件是在UpdatePanel中触发的,由于UpdatePanel触发的代码中有Response.Write是无法通过的
(而我在其实应用中正是通过UpdatePanel触发的)
我不想使用if进行功能代码的判断,因为这样我就无法把这个功能写成一个类了,例如这种:
protected void Button1_Click(object sender, EventArgs e)
{
string js = "";
if(Session["name"] == null)
{
ScriptManager.RegisterStartupScript(Updatepanel1, Updatepanel1.GetType(), "Startup", js, false);
}
else
{
//功能代码
}
}
是不是Response.End真的无法与RegisterStartupScript同时使用呢?
解决方案1: yeagen好! 我是这么理解做的:
protected void Button1_Click(object sender, EventArgs e)
{
Session["msg"] = txtTest.Text.ToString();
//如果为空,则将原隐藏的Label显示出来.
if(string.IsNullOrEmpty((string) Session["msg"]))
{
lblmsg2.Attributes["Style"] = "display:block";
//为空时的处理代码
// ...........
}
else
{
lblmsg.Text = "Hoho Succeed";
//满足条件的处理代码
// ..........
}
}
不过,看到楼上ojlovecd写的,感觉更有条理些.. 嘿嘿. 学习了.
解决方案2: 看明白了,那你可不可以另CheckLogin返回一个bool值?根据这个值判断代码是否接着往下走:
//App_Code/lib.cs类文件中:
static public bool CheckLogin(Updatepanel1)
{
string js = "";
if(Session["name"] == null)
{
ScriptManager.RegisterStartupScript(Updatepanel1, Updatepanel1.GetType(), "Startup", js, false);
return false;
}
return true;
}
主程序中
protected void Button1_Click(object sender, EventArgs e)
{
if(CheckLogin(UpdatePanel1))
{
string sqlcmd = "insert into message (title) values (@title)";
//后面就是接下去的往数据库里写记录的代码了
}
}
以上介绍了“得得撸改名得得爱 ResponseEnd使得RegisterStartupScript失效的问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2923801.html