Page

How to use RangeValidator in asp.net

//RangeValidator.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RangeValidator.aspx.cs" Inherits="RangeValidator" %>  
  
<!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 runat="server">  
    <title>asp.net RangeValidator: how to use</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:Navy">RangeValidator</h2>  
        <asp:Label ID="Label1" runat="server">  
        </asp:Label>  
        <br /><br />  
        <b>Select a number between 4 to 7</b>  
        <br />  
        <asp:DropDownList   
            ID="DropDownList1"  
            runat="server"  
            >  
            <asp:ListItem>1</asp:ListItem>  
            <asp:ListItem>2</asp:ListItem>  
            <asp:ListItem>3</asp:ListItem>  
            <asp:ListItem>4</asp:ListItem>  
            <asp:ListItem>5</asp:ListItem>  
            <asp:ListItem>6</asp:ListItem>  
            <asp:ListItem>7</asp:ListItem>  
            <asp:ListItem>8</asp:ListItem>  
            <asp:ListItem>9</asp:ListItem>  
            <asp:ListItem>10</asp:ListItem>  
        </asp:DropDownList>  
        <asp:RangeValidator   
            ID="RangeValidator1"  
            runat="server"  
            >  
        </asp:RangeValidator>  
        <br /><br />  
        <asp:Button ID="Button1" runat="server" Text="Submit Number" OnClick="Button1_Click" />  
    </div>  
    </form>  
</body>  
</html>  


//RangeValidator.aspx.cs


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;  
  
public partial class RangeValidator : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if(!this.IsPostBack)  
        {  
            Label1.ForeColor = System.Drawing.Color.HotPink;  
            Label1.Font.Italic = true;  
            DropDownList1.BackColor = System.Drawing.Color.SaddleBrown;  
            DropDownList1.ForeColor = System.Drawing.Color.AliceBlue;  
            RangeValidator1.ControlToValidate = "DropDownList1";  
            RangeValidator1.Type = ValidationDataType.Integer;  
            RangeValidator1.MinimumValue = "4";  
            RangeValidator1.MaximumValue = "7";  
            RangeValidator1.ErrorMessage = "Select between 4 to 7";  
        }  
    }  
  
    protected void Button1_Click(object sender, EventArgs e) {  
        Label1.Text = "Number is valid: " +  
            DropDownList1.SelectedItem.Text.ToString();  
    }  
}  

No comments:

Post a Comment