读书人

ASP.net关于确认、取消提示的有关问题

发布时间: 2012-09-23 10:28:11 作者: rapoo

ASP.net关于确认、取消提示的问题,请求帮助!
我想写一段代码,后台的,部分代码如下,我想在result不为0时,弹出confirm提示后点击确认的话,可以继续执行else中的数据库操作语句,点取消就只是return,我不知道应该怎么实现啊??

if (result !=0)
{
Response.Write("<script>confirm('该学生此学科成绩已存在,是否覆盖此成绩?');</script");
}
else
{
mySQL = "update UTB_SCHOOL_SCORE set LessonID=@LessonID,StudentID=@StudentID,Score=@Score where ID=@ID";
con = new SqlConnection(@"server=localhost;uid=sa;pwd=;database=schooldemo");
com = new SqlCommand(mySQL, con);
。。。。。。。。。。。。

[解决办法]
你的问题,就是不知道如何写出脚本,此脚本让asp.net回发。许多人只是会拖个Button控件之类的进行asp.net回发,不会自己写控件的回发代码。

其实也很简单,只要要处理回发的控件具有 IPostbakcEventHandler 接口,就可以处理回发。这里我给你写一个例子,假设confirm的结果是要回发给整个页面,而不是回发给页面上的某个控件(发给任意一个子控件都显得画蛇添足),那么可以这样写一个demo页面

HTML code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />    </form></body></html>
[解决办法]
if (result !=0)
{
Response.Write("<script>location.href='nextstep.aspx?type='+confirm('该学生此学科成绩已存在,是否覆盖此成绩?');</script");
}
'nextstep.aspx页面里面
Page_Load:

if(Request.QueryString["type"]=="true")
{
mySQL = "update UTB_SCHOOL_SCORE set LessonID=@LessonID,StudentID=@StudentID,Score=@Score where ID=@ID";
con = new SqlConnection(@"server=localhost;uid=sa;pwd=;database=schooldemo");
com = new SqlCommand(mySQL, con);
}

大致的意思是这样的,你也可以封装在一个页面中

读书人网 >asp.net

热点推荐