Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2004, 06:11 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

you dont need to put any tag,we have a string variable('s')that keeps JavaScript codes,
Code:
//JavaScript Syntax
var txtbox1=document.getElementById('TextBox1');//finds first textbox
var txtbox2=document.getElementById('TextBox2');//finds second textbox
I supposed the ID's of your textboxes were TextBox1 and TextBox2(in code-behind) then we test
value property(JavaScript property) of every textbox according to your condition,
Code:
//JavaScript Syntax
if (txtbox1.value=='' && txtbox2.value=='' || txtbox1.value!='' && txtbox2.value!='')
    {window.alert('Validation not acceptable.');return false;}
I return false because I dont want Button_Click event to be fired off,
now I should add this javascript code to onclick attribute somewhere like Page_Load,
Code:
//C# Syntax
//s keeps that javascript code I mentioned
Button.Attributes.Add("onclick",s);
There are two points here,
1-Button_Click is a server-side event for the Button,
2-onclick attribute is a client-side event for the same Button,
when an user clicks the Button first client-side event check the condition,
if condition is true(invalid inputs)the onclick attribute shows a po pup window,
and return false so Button_Click is not fired off any more,
but if condition is false(valid inputs),Button_Click is fired,
(put codes come after a successful validation in your Button_Click event)
Code:
//Final Code
private void Page_Load(object sender, System.EventArgs e)
{
    if(!Page.IsPostBack)
    {
           string s="";
           s+="var txtbox1=document.getElementById('TextBox1');var txtbox2=document.getElementById('TextBox2');";
           s+="if (txtbox1.value=='' && txtbox2.value=='' || txtbox1.value!='' && txtbox2.value!=''){window.alert('Validation not acceptable.');return false;}";
           Button.Attributes.Add("onclick",s);
    }
}
private void Button_Click(object sender, System.EventArgs e)
{
      //codes are executed on the server-side after a successful validation
}
this method works just like this page,look at the bottom of this page,
if you try to send an empty message a po pup window notices you,
and stops sending your request to server.
_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
 
Old December 20th, 2004, 05:31 PM
Authorized User
 
Join Date: Aug 2004
Location: , , .
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Mehdi-
this code works just Great! Thanks a lot. It is so simple and does the needful. Again, thanks for taking all the time to find the solution.
Renu

 
Old December 21st, 2004, 06:28 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to nashnash
Default

--save this as 'te.aspx'-----

<%@ Page language="c#" Codebehind="te.aspx.cs" AutoEventWireup="false" Inherits="Insight.te" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<script language="javascript">
    function validate()
    {
        var strText1;
        var strText2;
        strText1=document.getElementById("TextBox1").value ;
        strText2=document.getElementById("TextBox2").value ;
        if(strText1=="" && strText2 =="")
        {
            alert("Please enter any textboxes");
            document.getElementById("lblMsg").innerHTML ="One of the textbox must be filled";
            return false;
        }
    }
</script>
<form id="Form1" runat="server">
    <table>
        <tr>
            <td>
                <div id="lblMsg"></div>
            </td>
        </tr>
        <tr>
            <td>
                <asp:textbox id="TextBox1" runat="server"
        Width="76px"></asp:textbox>
        <asp:textbox id="TextBox2" runat="server"
        Width="76px"></asp:textbox>
        <asp:button id="Button1" runat="server"
        Text="Button"></asp:button>
            </td>
        </tr>
    </table>

    </p>
    <p>

        </p>
        </form>
-----save this as 'te.aspx.cs'-------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Insight
{
    /// <summary>
    /// Summary description for te.
    /// </summary>
    public class te : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.TextBox TextBox2;
        protected System.Web.UI.WebControls.Button Button1;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            Button1.Attributes.Add("onclick","javascript:retur n validate();");
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}


Thanks
Nash


 
Old December 21st, 2004, 06:30 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to nashnash
Default

please copy and pase and u will get the result.
--save this as 'te.aspx'-------------
<%@ Page language="c#" Codebehind="te.aspx.cs" AutoEventWireup="false" Inherits="Insight.te" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<script language="javascript">
    function validate()
    {
        var strText1;
        var strText2;
        strText1=document.getElementById("TextBox1").value ;
        strText2=document.getElementById("TextBox2").value ;
        if(strText1=="" && strText2 =="")
        {
            alert("Please enter any textboxes");
            document.getElementById("lblMsg").innerHTML ="One of the textbox must be filled";
            return false;
        }
    }
</script>
<form id="Form1" runat="server">
    <table>
        <tr>
            <td>
                <div id="lblMsg"></div>
            </td>
        </tr>
        <tr>
            <td>
                <asp:textbox id="TextBox1" runat="server"
        Width="76px"></asp:textbox>
        <asp:textbox id="TextBox2" runat="server"
        Width="76px"></asp:textbox>
        <asp:button id="Button1" runat="server"
        Text="Button"></asp:button>
            </td>
        </tr>
    </table>

    </p>
    <p>

        </p>
        </form>

-------------save this as 'te .aspx.cs'------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Insight
{
    /// <summary>
    /// Summary description for te.
    /// </summary>
    public class te : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.TextBox TextBox2;
        protected System.Web.UI.WebControls.Button Button1;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            Button1.Attributes.Add("onclick","javascript:retur n validate();");
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}


 
Old December 21st, 2004, 06:33 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to nashnash
Default

----save this as 'te.aspx'--------
<%@ Page language="c#" Codebehind="te.aspx.cs" AutoEventWireup="false" Inherits="Insight.te" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<script language="javascript">
    function validate()
    {
        var strText1;
        var strText2;
        strText1=document.getElementById("TextBox1").value ;
        strText2=document.getElementById("TextBox2").value ;
        if(strText1=="" && strText2 =="")
        {
            alert("Please enter any textboxes");
            document.getElementById("lblMsg").innerHTML ="One of the textbox must be filled";
            return false;
        }
    }
</script>
<form id="Form1" runat="server">
    <table>
        <tr>
            <td>
                <div id="lblMsg"></div>
            </td>
        </tr>
        <tr>
            <td>
                <asp:textbox id="TextBox1" runat="server"
        Width="76px"></asp:textbox>
        <asp:textbox id="TextBox2" runat="server"
        Width="76px"></asp:textbox>
        <asp:button id="Button1" runat="server"
        Text="Button"></asp:button>
            </td>
        </tr>
    </table>

    </p>
    <p>

        </p>
        </form>

----save this as 'te.aspx.cs' ------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Insight
{
    /// <summary>
    /// Summary description for te.
    /// </summary>
    public class te : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox TextBox1;
        protected System.Web.UI.WebControls.TextBox TextBox2;
        protected System.Web.UI.WebControls.Button Button1;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            Button1.Attributes.Add("onclick","javascript:retur n validate();");
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}






Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation controls and postback sdi126 ASP.NET 2.0 Basics 5 November 11th, 2007 12:08 PM
User controls and validation relaytest49 ASP.NET 2.0 Professional 1 October 11th, 2006 01:25 PM
Validation Controls andyj00 ASP.NET 1.0 and 1.1 Professional 3 June 9th, 2005 09:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.