我是学ASPNET的新手,我写了个简单的登录程序,不知是myButton的Click事件触发不了,还是数据库连不上,在登录无论输入什么,反应都是一样的(点登录后,没有反应).这个问题我找了好几天都没找出来,谁能帮帮我?非常感谢!! 
下面是我写的程序: 
//////WebForm1.aspx 
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="MyTest.WebForm1" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
<HTML> 
  <HEAD> 
      <title>WebForm1</title> 
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR> 
<meta content=C# name=CODE_LANGUAGE> 
<meta content=JavaScript name=vs_defaultClientScript> 
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema> 
  </HEAD> 
<body MS_POSITIONING="GridLayout"> 
<form id=Form1 method=post runat="server"><FONT face=宋体> 
<TABLE id=Table1  
style="Z-INDEX: 101; LEFT: 352px; POSITION: absolute; TOP: 168px" cellSpacing=1  
cellPadding=1 width=300 border=1> 
  <TR> 
    <td> </TD> 
    <TD style="WIDTH: 55px" align=center>登录</TD></TR> 
  <TR> 
    <TD style="WIDTH: 55px">姓名:</TD> 
    <TD><asp:textbox id=MyName runat="server"></asp:TextBox></TD></TR> 
  <TR> 
    <TD style="WIDTH: 55px"></TD> 
    <TD><asp:requiredfieldvalidator id=RequiredFieldValidator1 runat="server" Display="Dynamic" ControlToValidate="MyName" ErrorMessage="请输入姓名!"></asp:RequiredFieldValidator></TD></TR> 
  <TR> 
    <TD style="WIDTH: 55px">密码:</TD> 
    <TD><asp:textbox id=MyPwd runat="server" TextMode="Password"></asp:TextBox></TD></TR> 
  <TR> 
    <TD style="WIDTH: 55px"></TD> 
    <TD><asp:requiredfieldvalidator id=RequiredFieldValidator2 runat="server" Display="Dynamic" ControlToValidate="MyPwd" ErrorMessage="输入密码!"></asp:RequiredFieldValidator></TD></TR> 
  <TR> 
    <TD style="WIDTH: 55px" align=center height=20><asp:button id=myButton runat="server" Text="登录" BorderStyle="Double"></asp:Button></TD> 
    <td><asp:button id=MyCancel runat="server" Text="取消" CausesValidation="False"></asp:Button> </TD></TR></TABLE><asp:label id=Label1 style="Z-INDEX: 102; LEFT: 432px; POSITION: absolute; TOP: 416px" runat="server"></asp:Label></FONT></FORM> 
   </body> 
</HTML> 
//////////////////////////////////////////////////// ////Webform1.aspx.cs 
using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Web; 
using System.Web.SessionState; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 
using System.Configuration; namespace MyTest 

   /// <summary> 
   /// WebForm1 的摘要说明。 
   /// </summary> 
   public class WebForm1 : System.Web.UI.Page 
   { 
      protected System.Web.UI.WebControls.TextBox MyPwd; 
      protected System.Web.UI.WebControls.Button MyCancel; 
      protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1; 
      protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2; 
      protected System.Web.UI.WebControls.Label Label1; 
      protected System.Web.UI.WebControls.Button myButton; 
      protected System.Web.UI.WebControls.TextBox MyName; 
    
      private void Page_Load(object sender, System.EventArgs e) 
      { 
         // 在此处放置用户代码以初始化页面 
      }       #region Web 窗体设计器生成的代码 
      override protected void OnInit(EventArgs e) 
      { 
         // 
         // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 
         // 
         InitializeComponent(); 
         base.OnInit(e); 
      } 
       
      /// <summary> 
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
      /// 此方法的内容。 
      /// </summary> 
      private void InitializeComponent() 
      {     
         this.Load += new System.EventHandler(this.Page_Load);       } 
      #endregion 
      private void myButton_Click(object sender, System.EventArgs e ) 
      { 
         SqlConnection myConnection=new SqlConnection(ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"]); 
         SqlCommand myCommand=new SqlCommand("select * from UserLogin where UserName='"+ MyName.Text +"' and UserPwd='"+ MyPwd.Text +"'",myConnection); 
         myConnection.Open(); 
         SqlDataReader myReader=myCommand.ExecuteReader(); 
          
         if (myReader.Read()) 
         { 
            Label1.Text="你登录成功了!"; 
         } 
         else 
         { 
            Label1.Text="登录失败!"; 
         } 
         myCommand.Connection.Close(); 
      }       private void MyCancel_Click(object sender,System.EventArgs e) 
      { 
         MyName.Text=""; 
         MyPwd.Text="";       
      }    } } 
//////////////////////////////////////////////////////////////// ////下面是Web.config被我修改的部分: 
<appSettings> 
    <add key="SQLCONNECTIONSTRING" value="data source=lxh;uid=sa;database=MyTest;pool=true;password=sa"></add> 
    </appSettings> 能告诉我是什么原因吗?谢谢!! 

解决方案 »

  1.   

    private void InitializeComponent() 
          {     
             this.Load += new System.EventHandler(this.Page_Load);       } 里面加一句
    this.myButton.Click +=new System.EventHandler(this.myButton_Click);
      或且删了myButton,重新生成一个,生成后,应该会自动生成上面的那句。
      

  2.   


    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);//加上这一句 this.Load += new System.EventHandler(this.Page_Load); }
      

  3.   

    谢谢zedan(kk)、iceiceberg() 两位,这个问题终于解决了,谢谢你们!