当前位置: 代码迷 >> ASP.NET >> 接收Request.QueryString["username"]的有关问题,更新不了数据~
  详细解决方案

接收Request.QueryString["username"]的有关问题,更新不了数据~

热度:9305   发布时间:2013-02-25 00:00:00.0
接收Request.QueryString["username"]的问题,更新不了数据~~
页面的代码是  
  protected void Page_Load(object sender, EventArgs e)
  {
  Label1.Text = Request.QueryString["username"];
  string strSql = "select * from tb_User where username='" + Label1.Text + "'";
  SqlDataReader olereaduser;
  olereaduser = connect.GetInfos(strSql);
  if (!olereaduser.Read())
  {
  Response.Write("您还没有登录!");
  }
  else
  {
  mqq.Text = Convert.ToString(olereaduser["qq"]) == null ? "" : Convert.ToString(olereaduser["qq"]);
  mmobile.Text = Convert.ToString(olereaduser["mobile"]) == null ? "" : Convert.ToString(olereaduser["mobile"]);
  mtel.Text = Convert.ToString(olereaduser["tel"]) == null ? "" : Convert.ToString(olereaduser["tel"]);
  maddress.Text = Convert.ToString(olereaduser["address"]) == null ? "" : Convert.ToString(olereaduser["address"]);
  }
  }
  protected void Button1_Click(object sender, EventArgs e)//更新按钮点击事击
  {
  try
  {
  string strSql = "update tb_User set qq='" + mqq.Text + "',mobile='" + mmobile.Text + "',tel='" + mtel.Text + "',address='" + maddress.Text + "' where username='" + Label1.Text + "'";
  connect.SetInfos(strSql);
  Response.Write("<script language='javascript'>alert('更新成功!');window.location.href('U_index.aspx');</script>");
  }
  catch (Exception Err)
  {
  Response.Write(Err.Message);
  }
  }

connect类
public class connect
{
public connect()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
  public static SqlConnection getConnection()
  {
  return new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  }
  public static void SetInfos(string SQLstr)
  {
  SqlConnection conn = getConnection();
  conn.Open();
  SqlCommand cmd = new SqlCommand();
  string strSql = SQLstr;
  cmd.Connection = conn;
  cmd.CommandText = strSql;
  cmd.ExecuteNonQuery();
  cmd.Dispose();
  conn.Close();  
  }
  public static SqlDataReader GetInfos(string SQLstr)
  {
  SqlConnection conn = getConnection();
  conn.Open();
  SqlCommand cmd = new SqlCommand();
  string strSql = SQLstr;
  cmd.Connection = conn;
  cmd.CommandText = strSql;
  SqlDataReader myReader = cmd.ExecuteReader();
  return (myReader);
  cmd.Dispose();
  conn.Close();
  }
}


Label.Text的值是由另外一个页面传过来的,现在遇到的问题是老更新不了数据,好像Label.Text的值在更新按钮事件中没了,不知咋回事,急着做作业,请各位大哥大姐帮帮忙~~~谢谢~~~

------解决方案--------------------------------------------------------
可以这样修改

C# code
protected void Page_Load(object sender, EventArgs e){    if (!IsPostBack)    {     //你的代码    }}
------解决方案--------------------------------------------------------
将page_load里的代码放到
if(!ispostback)
{
...
}
的判断里面去
  相关解决方案