下面是我写的一段简单的注册表单只有用户名和密码;当然了是为了测试特意写的,先前就遇到这个问题,到网上查了N久都没有找到答案,希望大侠们帮我改正一下代码。
问题很明显,这段代码经过刷新之后会重新向数据库添加数据,意思就是说:点完提交不关闭页面,只要一刷新就导致了页面提交
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string strid = txtID.Text;
string strpwd = txtPWD.Text;
string strpwdAn = txtPWDAgain.Text;
string strCon="Data Source=bk60538\\sqlexpress;Initial Catalog=userinfo;Integrated Security=True";
SqlConnection con = new SqlConnection(strCon);
string strCmd;
SqlCommand cmd;
try
{
con.Open();
strCmd = "select count(*) from t_user where userid=@uid";
cmd = new SqlCommand(strCmd, con);
cmd.Parameters.AddWithValue("@uid", strid);
int count =Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
Response.Write("用户名已经存在");
}
else
{
strCmd = "insert into t_user values(@userid,@userpwd)";
cmd = new SqlCommand(strCmd, con);
cmd.Parameters.AddWithValue("@userid", strid);
cmd.Parameters.AddWithValue("@userpwd", strpwd);
cmd.ExecuteNonQuery();
Response.Write("写入成功");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
cmd = null;
con.Close();
con = null;
}
}
}
问题很明显,这段代码经过刷新之后会重新向数据库添加数据,意思就是说:点完提交不关闭页面,只要一刷新就导致了页面提交
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string strid = txtID.Text;
string strpwd = txtPWD.Text;
string strpwdAn = txtPWDAgain.Text;
string strCon="Data Source=bk60538\\sqlexpress;Initial Catalog=userinfo;Integrated Security=True";
SqlConnection con = new SqlConnection(strCon);
string strCmd;
SqlCommand cmd;
try
{
con.Open();
strCmd = "select count(*) from t_user where userid=@uid";
cmd = new SqlCommand(strCmd, con);
cmd.Parameters.AddWithValue("@uid", strid);
int count =Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
Response.Write("用户名已经存在");
}
else
{
strCmd = "insert into t_user values(@userid,@userpwd)";
cmd = new SqlCommand(strCmd, con);
cmd.Parameters.AddWithValue("@userid", strid);
cmd.Parameters.AddWithValue("@userpwd", strpwd);
cmd.ExecuteNonQuery();
Response.Write("写入成功");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
cmd = null;
con.Close();
con = null;
}
}
}
解决方案 »
- 如何在ashx文件中定义一个全局变量.我想在mymethod()中用.我试着在类下面定义了一下.不行.高手指点
- c#向数据库存Money类型和int类型的代码区别
- asp.net网站如何放在互联网上?
- gridview中dropdownlist无法转换类型!?
- 关于gridview中嵌套gridview的问题??急~~~~~~~~最后十分了
- 急用!请教一个奇怪的现象 (在线等!解决后马上结帖)
- 各位大牛,你们是怎样写asp.net的程序啊
- 为啥在类里不能用Response.write....
- 有难度哟!upload file using http and asp.net????
- ASP。NET调试问题,很郁闷解决不了,高手帮忙
- asp.net 中执行<script>代码后页面布局变不正常了
- 问个简单的数据绑定问题。
Response.redirect("当前页面地址");
最简单的方法是操作成功后Resposne.ReDirect(Request.RawUrl);