Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to validate the page when I do not select zone in dropdownlist1.
My asp.net page is

ASP.NET
<%@ 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:
C#
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.
Posted
Updated 18-Mar-12 20:02pm
v4
Comments
Oshtri Deka 16-Mar-12 5:15am    
Edit: general formatting (code, empty spaces, identations etc.)
Oshtri Deka 16-Mar-12 5:19am    
Orginal code you copied here had several mistakes in markup. Did it work at all?
What is your problem? Which par doesn't work?

1. If you haven't install FireBug or press F12 on IE
2. Use console.info(value) to evaluate your variables
3. Try to get some conclusion from results and if they matter, post them here.
4. Good luck!

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.
 
Share this answer
 
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.
 
Share this answer
 
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!

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



XML
<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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900