Welcome to weblogs.com.pk Sign in | Join | Help

ASP.NET :: Using Java Script!

This is for a friend, showing how easy it is to use JavaScript in ASP.NET apps. Note how the inline code is used to get the element id. To make it more runtime error prone; simply create a method in code-beside file and use that…

    <script language="javascript" type="text/javascript">

        function test()

        {

                var textbox1 = document.getElementById("<%=TextBox1.ClientID %>");

                var label1 = document.getElementById("<%=Label1.ClientID %>");

                label1.innerHTML = textbox1.value;

                alert(textbox1.value);

        }

    </script>

    <form id="form1" runat="server">

    <div>

        TextBox1

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

        TextBox2 &nbsp;<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

        <br />

        This label should show server side change event value:

        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />

    </div>

    </form>

 

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!this.Page.IsPostBack)

            {

                this.TextBox1.Attributes.Add("onfocusout", "return test();");

            }

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            this.Label1.Text = this.TextBox1.Text;

        }

For many readers; this might be a straight forward and too simplistic; I am just posting it here for the reference.

Published Sunday, October 15, 2006 1:06 AM by khurram
Filed under:

Comments

# re: ASP.NET :: Using Java Script!

Sunday, October 15, 2006 2:34 AM by Mohamed A. Meligy
Hmm.. yeah, I know a couple of guys out there who maybe don't know this.
One small addition is that you don't have to put the attributes using server code (as in page load in the example above) if you are using ASP.NET 2.0. In ASP.NET 2.0, this will work if you just add the
onfocusout="return test();"
dircty inside the asp:textbox tag,it'll work pretty well. The source editor in VS2005 won't like it and will put a green line under it, but this is just because it's not in its schema, and, this won't prevent it from working without errors.
Regards,
New Comments to this post are disabled