日期:2014-05-18  浏览次数:20835 次

vs中c#问题
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ligon : System.Web.UI.Page
{
  string userid;
  protected void Page_Init(object sender, EventArgs e)
  {
  //this.tbName.Text = "";//设为空白
  //this.tbPsw.Text = "";

  }
   
  protected void Button1_Click(object sender, EventArgs e)
  {
  if (Page.IsValid == true)
  {
  //连接数据库
  Boolean test = true;
  SqlConnection conn = new SqlConnection();
  conn.ConnectionString = "server=.;database=test;Integrated Security=true";
  conn.Open();  
  string cmdtext = "Select count(*) from customer where name='" + tbName.Text + "' and password='"+tbPsw.Text+"'";
  SqlCommand cmd = new SqlCommand(cmdtext, conn);
  int count=Convert.ToInt32(cmd.ExecuteScalar());  
  try
  {
  if (tbName.Text == "")
  {
  this.Button1.Attributes.Add("onclick", "javascript:showmessagebox1();");
  test = false;
  }
  else
  {
  if (tbPsw.Text== "")
  {
  this.Button1.Attributes.Add("onclick", "javascript:showmessagebox2();");
  test = false;
  }
  }
  if (test)
  {
  if (count > 0)
  {
  Response.Redirect("guanli.aspx");
  Session["userid"] = userid;
  Response.Write("asdsad");
  }
  else
  {
  this.Button1.Attributes.Add("onclick", "javascript:showmessagebox();");

  }
  }
   
  }
  finally
  {  
  conn.Close();
  }  
  }
  }
  protected void Button2_Click(object sender, EventArgs e)
  {
  Response.Redirect("http://www.google.com");
  }
  public override void VerifyRenderingInServerForm(Control control)
  {

  }
}
这是一个登入页面的代码;
非语法错误,而可能是逻辑错误,每次对于输入用户名,密码的响应,要按两次提交才有结果;就是所有响应都往后一步。而第一次按是不会有任何结果的;
我看了好久都没看出问题啊。谁能帮我看看啊,谢谢

------解决方案--------------------
if (Page.IsValid == true) 

-->page.ispostback
------解决方案--------------------
this.Button1.Attributes.Add("onclick", "javascript:showmessagebox1();"); 
像这样的验证都写在一个JS方法里,在JS方法里再判断处理。。
Button1再指定OnClientClick="JS方法"