Wednesday, 11 May 2011

IMP LINK FOR ASP.NET ARTICALS

http://www.devcurry.com/

http://www.dotnetcurry.com

CLIENT SIDE VALIDATION IN USER CONTROL

//THIS IS MY .ASCX SOURCE FILE  PAGE
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ValidateLoginControl.ascx.cs" Inherits="ValidateLoginControl" %>
<style type="text/css">
    .style1
    {
        width: 100%;
    }
    .style2
    {
        width: 73px;
    }
    .style3
    {
        width: 350px;
    }
</style>
<script language="JavaScript" type="text/JavaScript">
    function validate() {
        if (document.getElementById("<%=txtUserName.ClientID%>").value == "") {
            alert("Enter Ur UserName");
            document.getElementById("<%=txtUserName.ClientID%>").focus();
            return false;
        }
        if (document.getElementById("<%=txtPassword.ClientID %>").value == "") {
            alert(" Enter Ur Password");
            document.getElementById("<%=txtPassword.ClientID %>").focus();
            return false;
        }
        return true;
    }
</script>
<table class="style1">
    <tr>
        <td class="style2">
            <asp:Label ID="lblUserName" runat="server" Text="User Name"></asp:Label>
        </td>
        <td class="style3">
            <asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2">
            <asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
        </td>
        <td class="style3">
            <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2">
            &nbsp;</td>
        <td class="style3">
            <asp:Button ID="btnSubmit" runat="server" Text="SignIn" onclick="btnSubmit_Click"
                 />
        </td>
    </tr>
    <tr>
        <td class="style2">
            &nbsp;</td>
        <td class="style3">
            &nbsp;</td>
    </tr>
</table>
THIS IS .ASCX CODE FILE
protected void Page_Load(object sender, EventArgs e)
    {
        btnSubmit.Attributes.Add("onClick", "validate()");
    }
//THIS IS .ASPX SOURCE FILE  PAGE
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="validateLogin.aspx.cs" Inherits="validateLogin" %>
<%@ Register Src="~/ValidateLoginControl.ascx" TagName="vlc" TagPrefix="validatelogin" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     
        <validatelogin:vlc ID="vlc1" runat="server" />
     
    </div>
    </form>
</body>
</html>



NUMERIC,ALPHA, AND ALPHANUMERIC VALIDATION IN ASP.NET 4.0

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="keycodevalidation.aspx.cs" Inherits="keycodevalidation" %>

<!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></title>
    <script language="javascript" type="text/javascript">
    //this is for numeric
        function benumeric() {
            var txtMin = document.getElementById('<%=txtMinExperience.ClientID%>');
            if (isNaN(txtMin.value) == true) {
                alert("This Field Should Be Numeric.");
                txtMin.focus();
                txtMin.value = "";
                return false;
            }
            return true;
        }
 </script>
    <script type = "text/javascript">
    //this for alpha
        function isAlpha(keyCode) {
            return ((keyCode >= 65 && keyCode <= 90) || keyCode == 8)
        }
    </script>
    <script type = "text/javascript">
//THIS IS FOR ALPHANUMNERIC
        function RestrictSpecialChars(e) {
            var keynum; var keychar; var numcheck;
            if (window.event) // IE
            {
                keynum = e.keyCode
            }
            else if (e.which) // Netscape/Firefox/Opera
            {
                keynum = e.which
            }
            if (keynum == 8) //for backspace
            {
                return true
            }

            else {
                keychar = String.fromCharCode(keynum)
                numcheck = /[a-zA-Z0-9]/
                return numcheck.test(keychar)
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Label1" runat="server" Text="Numeric"></asp:Label>
    <asp:TextBox ID="txtMinExperience" onKeyUp="benumeric();" runat="server" MaxLength="50" TabIndex="4" Width="50px">0</asp:TextBox>
    <%--    <asp:TextBox ID="txtNumeric" runat="server" onkeydown = "return isNumeric(event.keyCode);" onpaste = "return false;" ></asp:TextBox>--%>
        <asp:Label ID="lblNumeric" ForeColor = "Red" runat="server" Text="Only Numeric Characters Allowed" style ="visibility:hidden"></asp:Label><br /><br />
        <asp:Label ID="lblAlpha" runat="server" Text="Alpha"></asp:Label>
        <asp:TextBox ID="txtAlpha" runat="server" onkeydown = "return isAlpha(event.keyCode);" onpaste = "return false;"></asp:TextBox>
        <asp:Label ID="Label4" ForeColor = "Red" runat="server" Text="Only Aphabetic Characters Allowed" style ="visibility:hidden"></asp:Label><br /><br />
        <asp:Label ID="lblAlphaNum" runat="server" Text="AlphaNumeric"></asp:Label>
 <asp:TextBox ID="txtAlphaNum" runat="server" onKeyPress="return RestrictSpecialChars(event);" ></asp:TextBox>
        <asp:Label ID="Label5" ForeColor = "Red" runat="server" Text="Only Alphanumeric Characters Allowed" style ="visibility:hidden"></asp:Label>
    </div>
    </form>
</body>
</html>

CUSTOM CONTROLS TUTORIAL LINK

http://www.akadia.com/services/dotnet_user_controls.html
http://forums.asp.net/t/1216768.aspx

Monday, 9 May 2011

How to control ASP.NET Validator Controls Client Side validation from JavaScript

http://www.aspdotnetfaq.com/Faq/How-to-control-ASP-NET-Validator-Controls-Client-Side-validation-from-JavaScript.aspx

Friday, 6 May 2011

Search Records In GridView And Highlight Results Using AJAX ASP.NET



<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Grid Search example</title>
<style type="text/css">
.highlight {
text-decoration:none; font-weight:bold;
color:black; background:yellow;}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1"
runat="server" />

<div>
Enter first name to search:
</div>
<div>
<asp:TextBox ID="txtSearch" runat="server"
AutoPostBack="True"
OnTextChanged="txtSearch_TextChanged">
</asp:TextBox>&nbsp;
</div>
<div>
<asp:UpdatePanel ID="UpdatePanel1"
runat="server">
<ContentTemplate>
<asp:GridView ID="grdSearch"
runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName"
runat="server"
Text='<%# Highlight(Eval("FirstName").ToString()) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server"
Text='<%#(Eval("LastName")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server"
Text='<%#(Eval("Location")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtSearch"
EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>

</div>

</form>
</body>
</html>
 
//code file
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
string strConnection =
ConfigurationManager.AppSettings["ConnectionString"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}

private DataTable GetRecords()
{
SqlConnection conn = new SqlConnection(strConnection);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from Employees";
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
DataSet objDs = new DataSet();
dAdapter.Fill(objDs);
return objDs.Tables[0];

}
private void BindGrid()
{
DataTable dt = GetRecords();
if (dt.Rows.Count > 0)
{
grdSearch.DataSource = dt;
grdSearch.DataBind();
}
}
private void SearchText()
{
DataTable dt = GetRecords();
DataView dv = new DataView(dt);
string SearchExpression = null;
if (!String.IsNullOrEmpty(txtSearch.Text))
{
SearchExpression = string.Format("{0} '%{1}%'",
grdSearch.SortExpression, txtSearch.Text);

}
dv.RowFilter = "FirstName like" + SearchExpression;
grdSearch.DataSource = dv;
grdSearch.DataBind();

}
public string Highlight(string InputTxt)
{
string Search_Str = txtSearch.Text.ToString();
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(),
RegexOptions.IgnoreCase);

// Highlight keywords by calling the 
//delegate each time a keyword is found.
return RegExp.Replace(InputTxt,
new MatchEvaluator(ReplaceKeyWords));

// Set the RegExp to null.
RegExp = null;

}

public string ReplaceKeyWords(Match m)
{

return "<span class=highlight>" + m.Value + "</span>";

}

protected void txtSearch_TextChanged(object sender, EventArgs e)
{
SearchText();
}
}