Sponsored Ad

Thursday, June 24, 2010

Number Range Validator/Check in ASP.NET | RangeValidator

ASP.NET RangeValidator is very helpful to validate integer and double values range. You need not to write a javaScript code to check for number range niether write a C# code to validate at server side afterpost back.

So using this validation control you can avoid round trip to server.

 

Number Range Validator/Check in ASP.NET | RangeValidator

Number Range Validator/Check in ASP.NET | RangeValidator

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Check for Number Range</h4>
        <br />
        Enter a Number:
        <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />
        <asp:RangeValidator ID="IntRangeValidator1" ControlToValidate="txtNumber" Type="Integer"
            MaximumValue="1000" MinimumValue="100" SetFocusOnError="true" Text="The Number entered is not in given range, please enter a number between 100 to 1000."
            runat="server" />
    </form>
</body>
</html>

Tuesday, June 22, 2010

How to Validate/Check for Date in DateRange in ASP.NET

You can verify that given date in daterange or not just by using asp.net RangeValidator. You need not use javascript code niether validate date at server side.

Just use RangeValidator and give minimum and maximum date and all is done. Whereever user will enter a date out of this range it will prompt a error message and stop processing.

 

How to Validate/Check for Date in DateRange in ASP.NET

How to Validate/Check for Date in DateRange in ASP.NET

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Check for Date Range</h4>
        <br />
        Enter Date:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />
        <asp:RangeValidator ID="RangeValidator1" ControlToValidate="TextBox1" Type="Date"
            MaximumValue="10/10/2010" MinimumValue="01/01/2010" SetFocusOnError="true" Text="The Date entered is not in given range."
            runat="server" />
    </form>
</body>
</html>

Sunday, June 20, 2010

How to use CustomValidator to write oun validation script in ASP.NET

We can use CustomValidator to write our own complex validation at server side. Suppose we have to validate a phone number should be of 10 digit only so for that we can write a C# code to check lenth of phone number and if its not 10 digit just assign false to IsValid property.

args.IsValid = false;

 

How to use CustomValidator to write oun validation script in ASP.NET

How to use CustomValidator to write oun validation script in ASP.NET

 

How to use CustomValidator to write oun validation script in ASP.NET Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >

<script runat="server">

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.ToString().Length != 10)
        {
            args.IsValid = false;
        }
    }
</script>

<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Check for Phone Number Length Validation</h4>
        <br />
        <br />
        Phone Number:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />
        <asp:CustomValidator ID="CustomValidator1" ControlToValidate="TextBox1" Text="Phone Number of 10 digits are allowed only."
            runat="server" OnServerValidate="CustomValidator1_ServerValidate" />
    </form>
</body>
</html>

Saturday, June 19, 2010

How to Compaire/Validate TextBox and DropDownList Values at client side in ASP.NET | CompareValidator

You can compaire a textbox and a Dropdownlist select value using asp.net comparevalidotor. Just give the name of both controls as given below.

 

How to Compaire/Validate TextBox and DropDownList Values at client side in ASP.NET | CompareValidator

How to Compaire/Validate TextBox and DropDownList Values at client side in ASP.NET | CompareValidator

 

How to Compaire/Validate TextBox and DropDownList Values at client side in ASP.NET | CompareValidator Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Compare Two Password Values</h4>
        Textbox Value :
        <asp:TextBox ID="txtValues" runat="server" />
        <br />
        <br />
        Select Dropdown Value:
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>IndiHub.com</asp:ListItem>
            <asp:ListItem>SoftwareTestingNet.com</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />
        <asp:CompareValidator ID="compair_values" ControlToValidate="txtValues"
            ControlToCompare="DropDownList1" ForeColor="red" Type="string" Text="Please enter same value in textbox as selected in dropdown list."
            Operator="equal" runat="server" />
    </form>
</body>
</html>

Thursday, June 17, 2010

How to Compaire/Validate Two Password Values in ASP.NET | CompareValidator

Suppose you have two password fields and you want to copaire these fields that original password and reentered passwords are same or not?

Here is a simple solution to do client side validation. Use ASP.NET comparevalidator and assign these properties.

ControlToValidate="txtPassword1" ControlToCompare="txtPassword2"

Set type =”string”

And its all done. Now if you will enter wrong repassword then it will validate client side and prompt you with error.

How to Compaire/Validate Two Password Values in ASP.NET | CompareValidator

How to Compaire/Validate Two Password Values in ASP.NET | CompareValidator

 

How to Compaire/Validate Two Password Values in ASP.NET | CompareValidator Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Compare Two Password Values</h4>
       Password Value 1:
        <asp:TextBox ID="txtPassword1" TextMode="Password" runat="server" />
        <br />
        <br />
        Password Value 2:
        <asp:TextBox ID="txtPassword2" TextMode="Password" runat="server" />
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />   
        <asp:CompareValidator ID="compair_Password_values"  ControlToValidate="txtPassword1" ControlToCompare="txtPassword2"
            ForeColor="red" Type="string"  Text="Please enter same Password values in both textboxes." Operator="equal"
            runat="server" />
    </form>
</body>
</html>

Wednesday, June 16, 2010

How to Compaire/Validate Two Double Values in ASP.NET | CompareValidator

Now in ASP.NET 2.0 and later version you can compaire double values of two textboxes on client side without using javascript code and going to server.

As you an check

12.0 and 12.00 are same double values and 12.0 and 12.001 are different values.

Please note that you can not compaire string values with double compairvalidator. It not not show any validation message.

 

How to Compaire/Validate Two Double Values in ASP.NET | CompareValidator

How to Compaire/Validate Two Double Values in ASP.NET | CompareValidator

 

How to Compaire/Validate Two Double Values in ASP.NET | CompareValidator Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Compare Two Double Values</h4>
       Double Value 1:
        <asp:TextBox ID="txtDouble1" runat="server" />
        <br />
        <br />
        Double Value 2:
        <asp:TextBox ID="txtDouble2" runat="server" />
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />   
        <asp:CompareValidator ID="compair_double_values"  ControlToValidate="txtDouble1" ControlToCompare="txtDouble2"
            ForeColor="red" Type="Double"  Text="Please enter same double values in both textboxes."
            runat="server" />
    </form>
</body>
</html>

Tuesday, June 15, 2010

How to Compaire Two Integer Values in ASP.NET | CompareValidator

Compairing two integer values in asp.net 2.0 and above versions are very easy now. You can just use the CompareValidator control and given the first textbox name and second textbox name as given in below example.

012 and 12 integer values are same, so it is not giving any error.

While 12 and 120 are different integer values, so there is error message.

 

How to Compaire Two Integer Values in ASP.NET | CompareValidator

How to Compaire Two Integer Values in ASP.NET | CompareValidator1

 

How to Compaire Two Integer Values in ASP.NET | CompareValidator Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Compare two Integer values</h4>
       Integer Value 1:
        <asp:TextBox ID="txtNumber1" runat="server" />
        <br />
        <br />
        Integer Value 2:
        <asp:TextBox ID="txtNumber2" runat="server" />
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />   
        <asp:CompareValidator ID="compair_values"  ControlToValidate="txtNumber1" ControlToCompare="txtNumber2"
            ForeColor="red" Type="integer"  Text="Please enter same integer values in both textboxes."
            runat="server" />
    </form>
</body>
</html>

Monday, June 14, 2010

CompareValidator in ASP.NET | How to Compaire Two String Values in ASP.NET

CompareValidator in ASP.NET | How to Compaire Two String Values in ASP.NET

ASP.NET 2.0 provide number of client side validation controls. One control is CompareValidator, which help to compare two values at client side without writing a javascript code. This example helps you to compare two string values without writing extra code.

Just assign ControlToValidate="txtName1" ControlToCompare="txtName2" the name of two textboxes need to compare and all is done.

 

CompareValidator in ASP.NET | How to Compaire Two String Values in ASP.NET Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Compare two values</h4>
        Value 1:
        <asp:TextBox ID="txtName1" runat="server" />
        <br />
        <br />
        Value 2:
        <asp:TextBox ID="txtName2" runat="server" />
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />
        <br />
        <asp:CompareValidator ID="compair_values" Display="dynamic" ControlToValidate="txtName1" ControlToCompare="txtName2"
            ForeColor="red" Type="String" Text="Validation Failed!"
            runat="server" />
    </form>
</body>
</html>

Sunday, June 6, 2010

How to Generate Random Links in ASP-C#

 

How to Generate Random Links in ASP-C#

Sometimes we need to display random links while designing a web application. Here is a solution to implement this using ASP.NET-C# coding.

This is implemented using random number generator and then checking these number and display the url on the basis of generated numbers.

How to Generate Random Links in ASP-C# Example:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<html>
<body>

<%
Random r = new Random();

double dd = r.NextDouble() ;
if (dd > 0.7)
    Response.Write("<a href='#>C# Tutorials</a>");
else if (dd > .5)
    Response.Write("<a href='#'>Software Testing Tutorials</a>");
else
    Response.Write("<a href='#>SharePoint Tutorials</a>");

%>
<p>
Example of random link generator.
</p>

</body>
</html>

Friday, June 4, 2010

How to Redirect User to Different URL – Radio Buttons - using ASP-C#

 

How to Redirect User to Different URL – Radio Buttons - using ASP-C#

This example help you to code a asp.net C# coding to redirect the user to specific url on selecting a radio button.

 

How to Redirect User to Different URL – Radio Buttons - using ASP-C#

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<%
    if (Request.Form["select"] != null && Request.Form["select"] != "")
       Response.Redirect(Request.Form["select"]);

%>  

<html>
<body>
<form action="Default3.aspx" method="post">
<input type="radio" name="select"
value="#">
C# Tutorials<br>
<input type="radio" name="select"
value="#">
Software Testing Tutorials<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Thursday, June 3, 2010

How to set Priority of a Thread in C# | Example

How to set Priority of a Thread in C# | Example

While running the application in multithreaded application, a priority task need to finish first. C# allowes to set the priority of a thread by assigning the thread priority.

These are different types of thread priorities available in C#:

Zero

BelowNormal

Normal

AboveNormal

Highest

you can check the output of this program. the thread2 is started first but thread1 executed first and thread2 executed second. this is because thread1 have hieghest priority and thread2 have lowest priority.

sometimes the output of given program can differ from current one, because thread based program dipends on system configuration and running programs also.

 

How to set Priority of a Thread in C# | Example

using System;
using System.Text;
using System.Threading;

namespace Console_App
{

    public class Class1
    {
        public void fun1()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(" Thread-1 #" + i  );            
            }
        }

        public void fun2()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(" Thread-2 #" + i);
            }
        }
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 a = new Class1();
            Thread thread1 = new Thread(new ThreadStart(a.fun1));
            Thread thread2 = new Thread(new ThreadStart(a.fun2));
            thread1.Priority = ThreadPriority.Highest;
            thread2.Priority = ThreadPriority.Lowest;
            thread2.Start();
            thread1.Start();
            Console.ReadLine();
        }
    }
}

Wednesday, June 2, 2010

How to run a thread in BackGround in C#

How to run a thread in BackGround in C#

Threads either run in background or foreground. There is a property by which one can know thred running status.

Thread.CurrentThread.IsBackground

One can force thread to run in background or forground by setting the property

thread1.IsBackground = true; //thread will run in background

thread1.IsBackground = false; //thread will run in foreground

like in current example the new thread is running in background and the parent thread is running in foreground.

 

How to run a thread in BackGround in C# Example:

using System;
using System.Text;
using System.Threading;

namespace Console_App
{

    public class Class1
    {
        public void fun1()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(" #" + i + "Running in Background: " + Thread.CurrentThread.IsBackground );            
            }
        }
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 a = new Class1();
            Thread thread1 = new Thread(new ThreadStart(a.fun1));
            Console.WriteLine("Before Starting Thread - Running in Background: " + Thread.CurrentThread.IsBackground);
            thread1.Start();
            thread1.IsBackground = true;
            Console.WriteLine("After Staring Thread  - Running in Background: " + Thread.CurrentThread.IsBackground);           
            Console.ReadLine();
        }
    }
}

Tuesday, June 1, 2010

Thread Execution With Join() in C#

Thread Execution With Join() in C#

Thread Execution with join() is different then normal execution. Join() blocks the execution of current thread untill called thread finish the execution. Jion(2) weights for called thread to finish execution in next 2 milliseconds and if not complete it continew the execution process.

Thread Execution With Join() in C# Example:

using System;
using System.Text;
using System.Threading;

namespace Console_App
{

    public class Class1
    {
        public void fun1()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(" #" + i );            
            }
        }
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 a = new Class1();
            Thread thread1 = new Thread(new ThreadStart(a.fun1));
            Console.WriteLine("Before Starting Thread " );
            thread1.Start();
            thread1.Join(2);
            Console.WriteLine("After Staring Thread " );           
            Console.ReadLine();
        }
    }
}

Sponsored Ad

Development Updates