View Single Post
  #1 (permalink)  
Old August 8th, 2012, 03:24 AM
cms9651 cms9651 is offline
Authorized User
Points: 121, Level: 2
Points: 121, Level: 2 Points: 121, Level: 2 Points: 121, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default Aspx page implemented with javascript validator

Hello there.
First of all I must say that I am a newbie when it comes to net (C#).

My reference book is:
BEGINNING ASP.NET 4 IN C# AND VB
IN C# AND VB
Spaanjaars Imar.


Here is my problem with this aspx page implemented with javascript function validator:
  1. I need show in the javascript alert the ErrorMessage "The field is required", when validations is enabled;
  2. The aspx page and my GridView is not updated after inserting interval DateStart and DateEnd, why?
  3. The aspx page and my GridView is not updated after inserting interval DateStart1 and DateEnd1, why?

Can you help me?

If you have link for similar task, please give it me.
Can you explain any one or any sample code related this.

Your help would be very appreciated.
Thanks in advance for your time and hints.

Cheers.
Chevy Mark Sunderland (cms)

code-behind
Code:
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.Threading;
using System.Globalization;
using System.Text;

public partial class COV2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
   {
        
        if (!Page.IsPostBack)
        {
            // query ...

            GridViewBind();
            dadapter.Dispose();
            ConnSqlServer.Close();
            ConnSqlServer.Dispose();
        }
    }


    public void GridViewBind()
    {
        // query ...

        dadapter = new SqlDataAdapter(sql1, ConnSqlServer);
        dset = new DataSet();
        dset.Clear();
        dadapter.Fill(dset);
        GridView1.PageIndex = 0;
        GridView1.DataSource = dset.Tables[0];
        GridView1.DataBind();
        dadapter.Dispose();
        Response.Write(sql1);
    }


    protected void Button1_Click(Object sender, EventArgs e)
    {
        GridViewBind();
    }

    protected void Button2_Click(Object sender, EventArgs e)
    {
        GridViewBind();
    }


}
MyPage.aspx:
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="button.aspx.cs" Inherits="SqlServer_button" %>

<!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">
    <title></title>
</head>
<body onload="InitValidators()">
    <form id="form1" runat="server">
    <div>
        <asp:ValidationSummary ID="ValidationSummary1" ShowMessageBox="true" runat="server"
            ShowSummary="false" />
        <asp:TextBox ID="DateStart" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DateStart"
            ErrorMessage="RequiredFieldValidator" Display="None"></asp:RequiredFieldValidator>
        <asp:TextBox ID="DateEnd" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Not valid date!"
            ControlToValidate="DateEnd" ControlToCompare="DateStart" Type="Date" Operator="GreaterThanEqual"
            CssClass="redb" SetFocusOnError="false"></asp:CompareValidator>
        <asp:ImageButton ID="Button1" ImageUrl="images/cerca_icon.gif" runat="server" OnClick="Button1_Click"
            OnClientClick="javascript:InitValidators();return false;" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DateEnd"
            ErrorMessage="RequiredFieldValidator" Display="None"></asp:RequiredFieldValidator>
        <br />
        <br />
        <asp:TextBox ID="DateStart1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="DateStart1"
            ErrorMessage="RequiredFieldValidator" Display="None"></asp:RequiredFieldValidator>
        <asp:TextBox ID="DateEnd1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="DateEnd1"
            ErrorMessage="RequiredFieldValidator" Display="None"></asp:RequiredFieldValidator>
        <asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="Not valid date!"
            ControlToValidate="DateEnd1" ControlToCompare="DateStart1" Type="Date" Operator="GreaterThanEqual"
            CssClass="redb" SetFocusOnError="false"></asp:CompareValidator>
        <asp:ImageButton ID="Button2" ImageUrl="images/cerca_icon.gif" runat="server" OnClick="Button2_Click"
            OnClientClick="javascript:InitValidators();return false;" />
        <asp:Label ID="MessagesLabel" runat="server" Text=""></asp:Label>
        <br />
        <br />
    </div>
    </form>
    <script type="text/javascript" language="javascript">
        function InitValidators() {

            var DateStart = document.getElementById('<%=DateStart.ClientID%>');
            var DateEnd = document.getElementById('<%=DateEnd.ClientID%>')
            var DateStart1 = document.getElementById('<%=DateStart1.ClientID%>');
            var DateEnd1 = document.getElementById('<%=DateEnd1.ClientID%>')

            if (DateStart.value == '' || DateEnd.value == '') {
                ValidatorEnable(document.getElementById('<%= RequiredFieldValidator1.ClientID%>'), true);
                ValidatorEnable(document.getElementById('<%= RequiredFieldValidator2.ClientID%>'), true);
            }
            else {
                ValidatorEnable(document.getElementById('<%= RequiredFieldValidator1.ClientID%>'), false);
                ValidatorEnable(document.getElementById('<%= RequiredFieldValidator2.ClientID%>'), false);
            }

            if (DateStart1.value == '' || DateEnd1.value == '') {
                ValidatorEnable(document.getElementById('<%= RequiredFieldValidator3.ClientID%>'), true);
                ValidatorEnable(document.getElementById('<%= RequiredFieldValidator4.ClientID%>'), true);
            }
            else {
                ValidatorEnable(document.getElementById('<%= RequiredFieldValidator3.ClientID%>'), false);
                ValidatorEnable(document.getElementById('<%= RequiredFieldValidator4.ClientID%>'), false);
            }


        }
    </script>
</body>
</html>
Reply With Quote