protected void Button1_Click(object sender, EventArgs e)
{
string js = "<script>alert('您未登陆,无法操作');</script>";
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 = "<script>alert('您未登陆,无法操作');</script>";
if(Session["name"] == null)
{
ScriptManager.RegisterStartupScript(Updatepanel1, Updatepanel1.GetType(), "Startup", js, false);
}
else
{
//功能代码
}
}
是不是Response.End真的无法与RegisterStartupScript同时使用呢?
这样试试呢?或者用这个
Response.Write("<script language=javascript>alert('您未登陆,无法操作')<script>");
Response.Write(" <script language=javascript>alert('您未登陆,无法操作') <script>");方法在平时是可以使用的
但在UpdatePanel中,Response.write方法会出错
直接转到登录页面去!
可是我想直接在界面上显示出登陆的DIV (WEB2.0常用的这种效果)
这个代码我最终会写在一个类中,然后在每个需要测试的地方进行调用,如果写return的话,那只是从类中return回主程序
是这样的
我想要做一个发贴的程序.
当用户没有登陆时,点击发新贴按钮时,用JS显示出一个隐藏的DIV,DIV中的内容是用户登录框.
与此同时,自动中止,原来按钮的功能
App_Code/lib.cs类文件中:
static public void CheckLogin()
{
string js = "<script>document.all.logindiv.style.display = 'block';</script>";
if(Session["name"] == null)
{
ScriptManager.RegisterStartupScript(Updatepanel1, Updatepanel1.GetType(), "Startup", js, false);
Response.End();//如果这里用Return
}
}主程序中
protected void Button1_Click(object sender, EventArgs e)
{
CheckLogin(); string sqlcmd = "insert into message (title) values (@title)";
//后面就是接下去的往数据库里写记录的代码了
}
App_Code/lib.cs类文件中:
static public void CheckLogin(Updatepanel1)
{
string js = "<script>document.all.logindiv.style.display = 'block';</script>";
if(Session["name"] == null)
{
ScriptManager.RegisterStartupScript(Updatepanel1, Updatepanel1.GetType(), "Startup", js, false);
Response.End();//如果这里用Return的话,只是返回到主程序中,并没有中断主程序以后的执行
}
}主程序中
protected void Button1_Click(object sender, EventArgs e)
{
CheckLogin(UpdatePanel1); string sqlcmd = "insert into message (title) values (@title)";
//后面就是接下去的往数据库里写记录的代码了
}
刚刚上面代码有点问题,我也是临时写的,没有测试过,大概就是这个意思,您能看明白不?
static public bool CheckLogin(Updatepanel1)
{
string js = "<script>document.all.logindiv.style.display = 'block';</script>";
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)";
//后面就是接下去的往数据库里写记录的代码了
}
}
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Label ID="lblmsg" runat="server" Text=""></asp:Label>
<asp:Label ID="lblmsg2" runat="server" Text="Error Message" Visible="false"></asp:Label> 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写的,感觉更有条理些.. 嘿嘿. 学习了.