RSS

Monthly Archives: August 2012

Regular expression in javascript

In Javascript,

function Validate()

{

            var txt = document.getElementById(“<%=txt.ClientID %>”);

            var regEx = /^[0-9]{1,6}?$/; // For not more than 6 numbers

            if (regEx.test(txt.value))

                alert(“Correct.”);

            else

           {

                alert(“Only 6 numbers.”);

                txt.focus();

            }

}

In CodeBehind,

<asp:TextBox ID=”txt” runat=”server” onblur=”Validate()”>

 
Leave a comment

Posted by on August 23, 2012 in ASP.NET

 

AJAX AutoCompleteExtender without WebService

Consider an example of search by name,

In database create a table that consist of names.

In design page,

<div>
Name to search
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
<asp:ToolkitScriptManager ID=”ToolkitScriptManager1″ runat=”server”>
</asp:ToolkitScriptManager>
<asp:AutoCompleteExtender ID=”AutoCompleteExtender1″ TargetControlID=”TextBox1″ runat=”server”
UseContextKey=”True” ServiceMethod=”GetCountries” MinimumPrefixLength=”1″
CompletionSetCount=”1″ CompletionInterval=”50″>
</asp:AutoCompleteExtender>
</div>

In code behind,

protected void Page_Load(object sender, EventArgs e)
{
}

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetCountries(string prefixText)
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings[“dbconnection”].ToString());
con.Open();
SqlCommand cmd = new SqlCommand(“select name from tblName where name like @Name+’%'”, con);
cmd.Parameters.AddWithValue(“@Name”, prefixText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
List<string> CountryNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CountryNames.Add(dt.Rows[i][0].ToString());
}
return CountryNames;
}

 
Leave a comment

Posted by on August 8, 2012 in ASP.NET