Saturday 7 July 2012

Beginning .Net : Submit a page to the server when the TextBox value has changed

You can submit a page when textbox value changed.
For that you need to take asp textbox control and set it's AutoPostBack property to true and handle it's ontextchanged event.

This article is very useful for .Net Beginners.

Here is example of this.
In this example we set autopostback=true and handle textbox change event. And on server side we handle change event and set textbox value to label control.

C# ASPX Code : 
<asp:TextBox runat="server" ID="txtProductName" AutoPostBack="true" ontextchanged="txtProductName_TextChanged"></asp:TextBox>
<br />
<asp:Label runat="server" ID="lblProductName"></asp:Label>

C# Example :
    protected void txtProductName_TextChanged(object sender, EventArgs e)
    {
        lblProductName.Text = txtProductName.Text;
    }

VB.Net ASPX Code :
<asp:TextBox runat="server" AutoPostBack="true" ID="txtProductName"></asp:TextBox>
<br />
<asp:Label runat="server" ID="lblProductName"></asp:Label>

VB.net Example :
    Protected Sub txtProductName_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtProductName.TextChanged
        lblProductName.Text = txtProductName.Text
    End Sub

In Vb.Net we are using handle keyword to handle event.So that we do not set ontextchanged property.
You can also set ontextchanged property as we do in C# Example. In this case you need to remove that Handle from server side code. Otherwise textchanged event call two times.

Output : 


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


No comments:

Post a Comment