当前位置:编程学习 > C#/ASP.NET >>

一个简单的asp按钮问题。

我的疑问是:登录页面(用来母版页)的重置按钮和linkbutton为什么会引发RequiredFieldValidator验证??而我用相同方法做的default.aspx页面(不用母版页)则不会引发验证?是不是和母版页有关?

如图所示,登录页面按了重置按钮或者“看不清,换一个之后”,引发验证


而default.aspx页面能把text清空,不会引发验证



母版页代码MasterPage.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>欢迎你</title>
<script language="javascript" type="text/javascript">
// <!CDATA[

function Text1_onclick() {

}

function Button1_onclick() {

}

function Password1_onclick() {

}

function Select1_onclick() {

}

function Select1_onclick() {

}

// ]]>

function MM_popupMsg(msg) { //v1.0
  alert(msg);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
</script>
        <style type="text/css">
            #Text1
            {
                width: 173px;
                height: 25px;
            }
            #Text2
            {
                width: 173px;
                height: 25px;
            }
            #Text3
            {
                width: 173px;
                height: 25px;
            }
            #Text4
            {
                width: 173px;
                height: 25px;
            }
            #Password1
            {
                width: 173px;
                height: 26px;
            }
            #Button1
            {
                width: 81px;
                height: 30px;
            }
            #Button2
            {
                width: 81px;
                height: 30px;
            }
            #Select1
            {
                width: 174px;
                height: 25px;
            }
            .style1
            {
                height: 38px;
            }
            .style2
            {
                width: 249px;
            }
            .style3
            {
                height: 38px;
                width: 249px;
            }
            .style4
            {
                width: 32%;
            }
            .style5
            {
                height: 38px;
                width: 32%;
            }
        </style>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body onload="MM_preloadImages('首页1.jpg','申请表单1.jpg','查询表单1.jpg','个人资料1.jpg','信息公告1.jpg','退出系统1.jpg')">
<table width="981" height="668" border="0" align="center" cellspacing="0" cellpadding="0">
  <tr>
    <td height="141" colspan="6"><img src="标题.jpg" width="1001" height="141" /></td>
  </tr>
  <tr>
    <td width="167" height="40"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image8','','首页1.jpg',1)"><img src="首页.jpg" name="Image8" width="167" height="40" border="0" id="Image8" /></a></td>
    <td width="167"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image9','','申请表单1.jpg',1)"><img src="申请表单.jpg" name="Image9" width="167" height="40" border="0" id="Image9" /></a></td>
    <td width="167"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image10','','查询表单1.jpg',1)"><img src="查询表单.jpg" name="Image10" width="167" height="40" border="0" id="Image10" /></a></td>
    <td width="167"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image11','','个人资料1.jpg',1)"><img src="个人资料.jpg" name="Image11" width="167" height="40" border="0" id="Image11" /></a></td>
    <td width="167"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image12','','信息公告1.jpg',1)"><img src="信息公告.jpg" name="Image12" width="167" height="40" border="0" id="Image12" /></a></td>
    <td width="166"><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image13','','退出系统1.jpg',1)"><img src="退出系统.jpg" name="Image13" width="167" height="40" border="0" id="Image13" /></a></td>
  </tr>
  <tr>
    <td height="431" colspan="6" valign="top" background="背景.jpg"><p> </p>
    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
      </td>
  </tr>
  <tr>
    <td height="36" colspan="6"><img src="底标.jpg" width="1001" height="36" /></td>
  </tr>
  <tr>
    <td height="19" colspan="3"> </td>
    <td> </td>
    <td>  </td>
    <td> </td>
  </tr>
</table>
</body>
</html>

asp 按钮 验证 --------------------编程问答-------------------- 登录页面前台,index.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" Title="无标题页" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <form id="form1" runat="server">
    <table width="547" border="0" align="center" style="height: 310px">
      <tr>
        <td height="45" colspan="3"><img src="用户登录.jpg" alt="登录" width="520" height="45" /></td>
      </tr>
      <tr>
        <td height="40" class="style6"><img src="登陆账号.jpg"  alt="帐号" width="168" height="40" /></td>
        <td width="279" class="style2">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ControlToValidate="TextBox1" ErrorMessage="必填"></asp:RequiredFieldValidator>
          </td>
       
      </tr>
      <tr>
        <td height="40" class="style6"><img src="登录密码.jpg" width="168"  alt="密码" height="40" /></td>
        <td class="style2">
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                ControlToValidate="TextBox2" ErrorMessage="必填"></asp:RequiredFieldValidator>
          </td>
        <td> </td>
      </tr>
      <tr>
        <td height="42" class="style6"><img src="验证码.jpg" width="168"  alt="验证码" height="40" /></td>
        <td class="style2">
            
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                ControlToValidate="TextBox3" ErrorMessage="必填"></asp:RequiredFieldValidator>
            
          </td>
        <td> </td>
      </tr>
      
      <tr>
        <td class="style6"> </td>
        <td class="style2">
            <asp:Image ID="Image14" runat="server" ImageUrl="ValidateCode.aspx" />
            <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">看不清,换一个</asp:LinkButton>
          </td>
        <td> </td>
      </tr>
      <tr>
        <td class="style6">
                          
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="登录" />
          </td>
        <td class="style2">           
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="重置" />
          </td>
        <td> </td>
      </tr>
    </table>
    </form>
</asp:Content>

<asp:Content ID="Content3" runat="server" contentplaceholderid="head">
    <style type="text/css">
    .style6
    {
        width: 139px;
    }
        #zhanghao
        {
            width: 171px;
        }
    </style>



</asp:Content>



登录页面后台index.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["zhuangtai"] = "";
       // Image14.ImageUrl = "ValidateCode.aspx";
    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        if (Session["CheckCode"].ToString().Equals(TextBox3.Text.ToString()))
        {
            int id = Convert.ToInt32(TextBox1.Text);
            String pw = TextBox2.Text;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=.;Initial Catalog=caigou;Integrated Security=True";
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from yonghu where id='" + id + "'", con);
            SqlDataReader data = cmd.ExecuteReader();

            if (data.Read() != false)
            {

                if (pw.Equals(data.GetValue(1).ToString()))
                {
                    Session["yonghuming"] = data.GetValue(2).ToString();
                    Session["zhuangtai"] = "true";
                    Session["role"] = data.GetValue(3).ToString();
                    data.Close();
                    con.Close();
                    con.Dispose();


                    Response.Redirect("Default.aspx");

                }
                else
                {
                    data.Close();
                    con.Close();
                    con.Dispose();
                    Response.Write("<script>alert('错误!')</script>");


                }
            }
            else
            {
                Response.Write("<script>alert('ID不存在!')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('验证码错误!')</script>");
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        this.Image14.ImageUrl = "ValidateCode.aspx";
    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    }
}


--------------------编程问答-------------------- 你的重置不要用Button 服务器控件,用<input type=reset />就可以了。 服务器控件会回发服务器,会进行验证的。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,