Pages

Friday, 14 October 2011

How to use OnTextChanged event in TextBox

TextBoxOnTextChanged.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void TextBox1_TextChanged(object sender, System.EventArgs e)
    {
        TextBox2.Text = TextBox1.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use OnTextChanged event in TextBox</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">TextBox Example: OnTextChanged</h2>
        <asp:Label
             ID="Label1"
             runat="server"
             Text="Email"
             >
        </asp:Label>
        <asp:TextBox
             ID="TextBox1"
             runat="server"
             AutoPostBack="true"
             OnTextChanged="TextBox1_TextChanged"
             >
        </asp:TextBox>
        <br /><br />
        <asp:Label
             ID="Label2"
             runat="server"
             Text="Confirm Email"
             >
        </asp:Label>
        <asp:TextBox
             ID="TextBox2"
             runat="server"
             BackColor="LightGoldenrodYellow"
             ForeColor="Crimson"
             >
        </asp:TextBox>
    </div>
    </form>
</body>
</html>









Click Here!

Thursday, 13 October 2011

Javascript validate positive integer value with decimal places

This javascript function will allow user to enter integer value with decimal places.

This JavaScript function you can all in onkeypress event of the textbox and it will validate the user entered value in client side.





function allowPositiveNumber(e) {
            var charCode = (e.which) ? e.which : event.keyCode
  
            if (charCode > 31 && (charCode < 46 || charCode > 57 )) {
                return false;
            }
            return true;
 
}

And Below is the HTML Text Box code



<input name="myTextBox" onkeypress="return allowPositiveNumber(event);" /><br>

Extended ASCII Codes


ASCII Table and Description


Monday, 10 October 2011

Call Page Method Using Javascript


Hi all,
Try this example to Call Page Method Using Client Side -Javascript in ASP.Net
ASP.Net AJAX ScriptManager allows you to call Server Side ASP.net methods from client side without any PostBack using PageMethods. Actually it is an AJAX call to the server but it allows us to call the method or function defined server side.
Enabling PageMethods
The first thing you need to do is add a ASP.Net AJAX ScriptManager to the page and set itsEnablePageMethods property to true as shown below
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

HTML Markup :-
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div>
Your Name :
<asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time" onclick="ShowCurrentTime()"/>
</div>
</form>

Client Side Method :-
<script type="text/javascript">
function ShowCurrentTime() {
PageMethods.GetCurrentTime(document.getElementById("<%=txtUserName.ClientID%>").value, OnSuccess);
}
function OnSuccess(response, userContext, methodName) {
alert(response);
}
</script>

Server Side Methods :-
[System.Web.Services.WebMethod]
publicstaticstring GetCurrentTime(string name)
{
return"Hello " + name + Environment.NewLine + "The Current Time is: "
        + DateTime.Now.ToString();
}

Hope this helps
Good Luck