在aspx页面中正常,于是我把代码分离到ascx里。
代码如下:
UserLogin.ascx<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserLogin.ascx.cs" Inherits="UserLogin" %>
<form id="form1" name="form1" runat="server" method="post" >
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
  <td width="177"><img src="img/yhdl.jpg" width="177" height="47" /></td>
</tr>
<tr>
  <td width="177" align="right" background="img/yhdlbg.jpg" style="height: 25px"><table width="90%" height="25" border="0" cellpadding="0" cellspacing="0" class="zt">
      <tr>
        <td align="center" style="height: 37px">用户名:</td>
        <td style="height: 37px; text-align: left"><label>
            &nbsp;<asp:TextBox ID="UserName" runat="server" Height="13px" Width="78px"  ></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName"
                ErrorMessage="*"></asp:RequiredFieldValidator></label></td>
      </tr>
  </table></td>
</tr>
<tr>
  <td width="177" height="25" align="right" background="img/yhdlbg.jpg"><table width="90%" height="25" border="0" cellpadding="0" cellspacing="0" class="zt">
      <tr>
        <td align="center" style="height: 25px">密&nbsp; 码:</td>
        <td style="height: 25px; text-align: left"><label>
            &nbsp;<asp:TextBox ID="Password" runat="server" Height="13px" TextMode="Password"
                Width="78px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Password"
                ErrorMessage="*"></asp:RequiredFieldValidator></label></td>
      </tr>
  </table></td>
</tr>
<tr>
  <td width="177" height="35" align="center" background="img/yhdlbg.jpg"><table width="85%" border="0" cellspacing="0" cellpadding="0">
      <tr>
          <td align="center" colspan="2" style="height: 20px">
              <label>
            &nbsp;<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/img/dl.jpg" OnClick="ImageButton1_Click" /></label><label>&nbsp;</label></td>
      </tr>
  </table></td>
</tr>
</table>
</form>UserLogin.ascx.cs    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string UserName = Request.Form["UserName"];
        string Password = Request.Form["Password"];
        //Response.Write(UserName+"asdf");
        //Response.End();
        //ACCESS数据库的连接字符串
        string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=" + Server.MapPath(".\\DataBase\\#wulong.mdb");
        //生成一个新的连接
        OleDbConnection myConn = new OleDbConnection(strConn);
        myConn.Open();
        string sql = "select * from users where username='" + UserName + "' and Password='" + Password + "'";
        //Response.Write(sql);
        //Response.End();
        OleDbCommand myComm = new OleDbCommand(sql, myConn);
        OleDbDataReader dr = myComm.ExecuteReader();
        if (dr.HasRows)
        {
            Session["UserName"] = UserName;
            Response.Redirect("~\\admin\\Admin_Index.aspx");
        }
        else
        {
            Response.Write("<script>alert('登录失败!请检查您输入了正确的用户名和密码!');window.history.back();</script>");
        }
    }代码很简单,就是用户登录。现在的问题是取值取不到Request.Form["UserName"]取到的是空值。