如何验证asp.net页面 [英] how to validate asp.net page

查看:69
本文介绍了如何验证asp.net页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我未在dropdownlist1中选择区域时,我想验证页面.
我的asp.net页面是

 <%@    页面   语言  ="  C#"    ="  〜/masterPages/Attendence.Master"    AutoEventWireup   ="   CodeBehind    AttendencePage.aspx.cs" 继承  ="  %> 

<   asp:Content     ID   ="   ContentPlaceHolderID    ContentPlaceHolder1 "  Runat   服务器" <  脚本    ="  文本/javascript" 语言   javascript " 功能 Validation()
{
     var  zoneid = 文档 .getElementById(" <%= DropDownList1.ClientID%>");
     var  userid = 文档 .getElementById(" <%= TextBox1.ClientID%>");
     var  pwd = 文档 .getElementById(" <%= TextBox2.ClientID%>");

    如果(zoneid.value == "  || zoneid.value == " )
    {
        alert(" );
        zoneid.focus();
        返回 ;
    }
}
<  / 脚本 > 

<   div  > 
    <      ="   0" <%-   style ="width:60px; height:150px; background-color:#FFCCFF ;
           "border =" 2>  <   tr  > 
         <   td     ="  右" 样式  宽度:30px " <   asp:Label     ID   ="   runat   服务器" 文本  位置" > 
         <  /td  > 
         <   td     ="  左" 样式  宽度:30px " <   asp:DropDownList     ID   ="   ="  服务器"   
 
                       宽度  ="  / > 
        <  /td  > 
     <  /tr  > 
     <  > 
         <   td     ="  右" <   asp:Label     ID   ="   runat   服务器" 文本   EmployeeID" > 
         <  /td  > 
         <   td     ="  左" <   asp:TextBox     ID   ="   runat   服务器" 宽度   130px" > 
         <  /td  > 
     <  /tr  > 
     <   tr  > 
         <   td     ="  右" <   asp:Label     ID   ="   runat   服务器" 文本  密码" > 
         <  /td  > 
         <   td     ="  左" <   asp:TextBox     ID   ="   runat   服务器"  TextMode   密码" 
 
                     span>         宽度  ="  / > 
         <  /td  > 
     <  /tr  > 
     <   tr  > 
         <   td     ="  右" <   asp:Label     ID   ="   runat   服务器" 文本  备注" > 
         <  /td  > 
         <   td     ="  左" <   asp:TextBox     ID   ="   runat   服务器" 宽度   130像素" 
 
                        TextMode   ="  / > 
         <  /td  > 
     <  /tr  > 
     <   tr  > 
         <   td     ="  右" <   asp:Button     ID   ="   runat   服务器" 文本  提交" 
 
                     span>          OnClientClick   ="     onclick   ="    
 
                     span>          BackColor   ="     BorderColor   ="  / > 
         <  /td  > 
         <   td     ="  左" <   asp:Button     ID   ="   runat   服务器" 文本  清除" 
 
                     span>          onclick   ="    背景色  ="   BorderColor    #CC99FF" / > 
         <  /td  > 
     <  /tr  > 
    <  /table  > 

    <  中心 > 
        <   asp:Label     ID   ="   runat   服务器" 文本  标签" 可见  错误" 
 
                        = "     ForeColor   ="  / > 
     <  /center  > 
<  /div  >  




C#文件为:

 使用系统;
使用 System.Collections;
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用使用System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Data.SqlClient;

命名空间 DL_PROJECT1
{
    公共 部分  AttendencePage:System.Web .UI.页面
    {
        SqlConnection con =  SqlConnection(" );
        SqlDataReader dr;
        SqlDataAdapter da;
        DataSet ds =  DataSet();
    
        受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
        {
            如果(!IsPostBack)
            {
                DropDownList1.Focus();
                Populate();
            }
        }
        公共 无效 Populate()
        {
            con.Open();
            SqlCommand cmd =  SqlCommand(" ,骗子);
            dr = cmd.ExecuteReader();
            DropDownList1.DataSource = dr;
            DropDownList1.DataValueField = " ;
            DropDownList1.DataBind();
            DropDownList1.Items.Insert( 0  new  ListItem(" 选择区域" 默认值"));
            con.Close();
            dr.Close();
        }
        受保护的 无效 Button1_Click(对象发​​件人,EventArgs e)
        {
            尝试
            {
                con.Open();
                SqlCommand cmd1 =  SqlCommand("  + TextBox1.Text +  '和IncomingDate ='" + DateTime .今天+ " ,con);
                SqlDataReader dr = cmd1.ExecuteReader();
                如果(dr.Read())
                {
                    Label5.Visible =  true ;
                    Label5.Text = " ;
                    TextBox1.Text = " ;
                    TextBox2.Text = " ;
                    TextBox3.Text = " ;
                    DropDownList1.ClearSelection();
                    dr.Close();
                }
                其他
                {
                    dr.Close();
                    SqlCommand cmd2 = 新建 SqlCommand("  + TextBox1.Text +  '和Password ='" + TextBox2. Text + " ,等);
                    SqlDataReader dr1 = cmd2.ExecuteReader();
                    如果(dr1.Read())
                        {
                            字符串 code = dr1.GetValue( 0 ).ToString();
                            dr1.Close();
                            如果(TextBox3.Text!= " )
                            {
                                SqlCommand cmd =  SqlCommand("  + TextBox1.Text +  ',' " + DateTime.Now.ToString(  T")+  ','" + DateTime.Now.ToShortDateString()+  ','" + DropDownList1.SelectedItem.Text +   + TextBox3.Text + " ,con) ;
                                cmd.ExecuteNonQuery();
                            }
                            其他
                            {
                                SqlCommand cmd =  SqlCommand("  + TextBox1.Text +  ','" + DateTime.Now.ToString(" )+  ','" + DateTime.Now.ToShortDateString()+  ','" + DropDownList1.SelectedItem.Text +   ')",骗子);
                                cmd.ExecuteNonQuery();
                            }
                            
                            Label5.Visible =  true ;
                            Label5.Text = " ;
                            TextBox1.Text = " ;
                            TextBox2.Text = " ;
                            TextBox3.Text = " ;
                            DropDownList1.ClearSelection();
                        }
                    其他 如果(DropDownList1.SelectedItem.Text == " ")
                    {
                        dr1.Close();
                        Label5.Visible =  false ;
                        DropDownList1.Focus();
                    }
                    其他 如果(TextBox1.Text == " ")
                    {
                        dr1.Close();
                        Label5.Visible =  false ;
                        TextBox1.Focus();
                    }
                    其他 如果(TextBox2.Text == " ")
                    {
                        dr1.Close();
                        Label5.Visible =  false ;
                        TextBox2.Focus();
                    }
                    其他
                    {
                        dr1.Close();
                        Label5.Visible =  true ;
                        Label5.Text = " ;
                    }
                }
            }
            捕获(SqlException ex)
            {
            }
            最终
            {
                con.Close();
            }
        }
        
        受保护的 无效 Button2_Click(对象发​​件人,EventArgs e)
        {
            TextBox1.Text = " ;
            TextBox2.Text = " ;
            TextBox3.Text = " ;
            DropDownList1.ClearSelection();
            TextBox1.Focus();
            Label5.Visible =  false ;
        }
    }
} 



当我单击按钮时,它也会同时检查javascript和c#文件.但是我不想在javascript运行弹出菜单时运行c#文件.实际上,我想先通过浏览器然后通过服务器来解析内容.

使用JavaScript进行验证是个好主意,但是如果您急忙,我建议您使用


发出警告后,如果您将页面发回,保存空白条目.如果值为空,则返回false,这样就不会发回邮件.


如果javascript验证失败,则必须从Validation方法返回false,这样它就不会进入服务器端.这是您的代码中的一些修改.希望对您有用!

< asp:Button ID = "  runat = "  Text = " 提交"
             OnClientClick = "  onclick = " 
             BackColor = "  BorderColor = " #CC99FF"/> 




 <  脚本   类型  ="    语言  ="  javascript" %> ");
    var userid = document.getElementById("%> ");
    var pwd = document.getElementById("%> ");

    if(zoneid.value ==")
    {
        alert(请选择区域");
        zoneid.focus();
        返回false;
    }

    if(userid.value ==")
    {
        alert(请输入用户名");
        pwd.focus();
        返回false;
    }

    if(pwd.value ==")
    {
        alert(请输入密码");
        pwd.focus();
        返回false;
    }
返回true;
} 


I want to validate the page when I do not select zone in dropdownlist1.
My asp.net page is

<%@ Page Language="C#"  MasterPageFile="~/masterPages/Attendence.Master" AutoEventWireup="true" CodeBehind="AttendencePage.aspx.cs" Inherits="DL_PROJECT1.AttendencePage" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<script type="text/javascript" language="javascript">

 function Validation()
{
    var zoneid=document.getElementById("<%=DropDownList1.ClientID%>");
    var userid=document.getElementById("<%=TextBox1.ClientID%>");
    var pwd=document.getElementById("<%=TextBox2.ClientID%>");

    if(zoneid.value=="" || zoneid.value=="select zone")
    {
        alert("Please select Zone");
        zoneid.focus();
        return false;
    }
}
</script>

<div>
    <table cellpadding="0">
           <%-- style="width:60px; height:150px; background-color:#FFCCFF; 
           " border="2"> --%>
     <tr>
         <td align="right" style="width:30px">
             <asp:Label ID="Label3" runat="server" Text="Location"/>
         </td>
         <td align="left" style="width:30px">
             <asp:DropDownList ID="DropDownList1 "runat="server" 

             width="135px"/>       
        </td>
     </tr>
     <>
         <td align="right">
             <asp:Label ID="Label1" runat="server" Text="EmployeeID"/>
         </td>
         <td align="left">
             <asp:TextBox ID="TextBox1" runat="server" Width="130px"/>
         </td>
     </tr>  
     <tr>
         <td align="right">
             <asp:Label ID="Label2" runat="server" Text="Password"/>
         </td>
         <td align="left">
             <asp:TextBox ID="TextBox2" runat="server" TextMode="Password" 

             Width="130px"/>
         </td>
     </tr>  
     <tr>
         <td align="right">
             <asp:Label ID="Label4" runat="server" Text="Remark"/>
         </td>
         <td align="left">
             <asp:TextBox ID="TextBox3" runat="server" Width="130px" 

             TextMode="MultiLine"/>
         </td>
     </tr>    
     <tr>
         <td align="right">
             <asp:Button ID="Button1" runat="server" Text="Submit" 

             OnClientClick=" return Validation();" onclick="Button1_Click" 

             BackColor="#FFCCFF" BorderColor="#CC99FF"/>
         </td>
         <td align="left">
             <asp:Button ID="Button2" runat="server" Text="Clear" 

             onclick="Button2_Click" BackColor="#FFCCFF" BorderColor="#CC99FF" />
         </td>
     </tr>
    </table>

    <center>
        <asp:Label ID="Label5" runat="server" Text="Label" Visible="False" 

            Font-Bold="True" ForeColor="#000066"/>
     </center>
</div>




c# file is:

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;

namespace DL_PROJECT1
{
    public partial class AttendencePage : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=ESS-8\\SQLEXPRESS;Initial Catalog=DL_PROJECT1;Integrated Security=True");
        SqlDataReader dr;
        SqlDataAdapter da;
        DataSet ds = new DataSet();
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.Focus();
                Populate();
            }
        }
        public void Populate()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT ShortName FROM [ZoneTable]", con);
            dr = cmd.ExecuteReader();
            DropDownList1.DataSource = dr;
            DropDownList1.DataValueField = "ShortName";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem("Select Zone", "Default value"));
            con.Close();
            dr.Close();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                SqlCommand cmd1 = new SqlCommand("SELECT ECode from Attendence where ECode='" + TextBox1.Text + "' and IncomingDate='" + DateTime.Today + "'", con);
                SqlDataReader dr = cmd1.ExecuteReader();
                if (dr.Read())
                {
                    Label5.Visible = true;
                    Label5.Text = "User already taken attendence.";
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                    TextBox3.Text = "";
                    DropDownList1.ClearSelection();
                    dr.Close();
                }
                else
                {
                    dr.Close();
                    SqlCommand cmd2 = new SqlCommand("select ECode from AttendenceMaster where ECode='"+TextBox1.Text+"' and Password='"+TextBox2.Text+"'", con);
                    SqlDataReader dr1 = cmd2.ExecuteReader();
                    if(dr1.Read())
                        {
                            string code = dr1.GetValue(0).ToString();
                            dr1.Close();
                            if (TextBox3.Text != "")
                            {
                                SqlCommand cmd = new SqlCommand("INSERT INTO Attendence(ECode,IncomingTime,IncomingDate,ZoneName,Remark) VALUES('" + TextBox1.Text + "','" + DateTime.Now.ToString("T") + "','" + DateTime.Now.ToShortDateString() + "','" + DropDownList1.SelectedItem.Text + "','" + TextBox3.Text + "')", con);
                                cmd.ExecuteNonQuery();
                            }
                            else
                            {
                                SqlCommand cmd = new SqlCommand("INSERT INTO Attendence(ECode,IncomingTime,IncomingDate,ZoneName) VALUES('" + TextBox1.Text + "','" + DateTime.Now.ToString("T") + "','" + DateTime.Now.ToShortDateString() + "','" + DropDownList1.SelectedItem.Text + "')", con);
                                cmd.ExecuteNonQuery();
                            }
                            
                            Label5.Visible = true;
                            Label5.Text = "Attendence has been taken.";
                            TextBox1.Text = "";
                            TextBox2.Text = "";
                            TextBox3.Text = "";
                            DropDownList1.ClearSelection();
                        }
                    else if (DropDownList1.SelectedItem.Text == "")
                    {
                        dr1.Close(); 
                        Label5.Visible = false;
                        DropDownList1.Focus();
                    }
                    else if (TextBox1.Text == "")
                    {
                        dr1.Close();
                        Label5.Visible = false;
                        TextBox1.Focus();
                    }
                    else if (TextBox2.Text == "")
                    {
                        dr1.Close();
                        Label5.Visible = false;
                        TextBox2.Focus();
                    }
                    else
                    {
                        dr1.Close();
                        Label5.Visible = true;
                        Label5.Text = "You are not registered.";
                    }   
                }
            }
            catch (SqlException ex)
            {
            }
            finally
            {               
                con.Close();
            }
        }
        
        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            DropDownList1.ClearSelection();
            TextBox1.Focus();
            Label5.Visible = false;
        }             
    }
}



When I click the button it check javascript and c# file also.But i do not want to run c# file when javascript run popup menu.Actully I want to parse the content by browser then server.

解决方案

Validation with JavaScript is good concept, but if your are in the hurry I advise you to use validation controls.

They aren''t omnipotent, but they are easy and "cheap" solution if you don''t want to fiddle with JavaScript.


After giving alert, if you post back your page will save blank entries. Return false when the value is blank so that post back should not take place.


When validation fails by javascript then you have to return false from Validation method so that it does not go to server side. Here are some modifications in your code. Hope it works for you!

<asp:Button ID="Button1" runat="server" Text="Submit"
             OnClientClick="return Validation();" onclick="Button1_Click"
             BackColor="#FFCCFF" BorderColor="#CC99FF"/>




<script type="text/javascript" language="javascript">

void function Validation()
{
    var zoneid=document.getElementById("<%=DropDownList1.ClientID%>");
    var userid=document.getElementById("<%=TextBox1.ClientID%>");
    var pwd=document.getElementById("<%=TextBox2.ClientID%>");

    if(zoneid.value=="")
    {
        alert("Please select Zone");
        zoneid.focus();
        return false;
    }

    if(userid.value=="")
    {
        alert("Please Enter userid");
        pwd.focus();
        return false;
    }

    if(pwd.value=="")
    {
        alert("Please Enter Password");
        pwd.focus();
        return false;
    }
return true;
}


这篇关于如何验证asp.net页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆