Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

UseSubmitBehavior property of the Button control - Part 39

Suggested Videos
Part 36 - Wizard control in asp.net
Part 37 - Asp.net Wizard control properties
Part 38 - Asp.net Wizard control events

In this video we will discuss about the UseSubmitBehavior property of the asp.net Button control. Let us understand the use of this property with an example. 

Design a webform with a TextBox, Label and 2 Button controls as shown in the image below.




For your convinience, I have included the HTML of the aspx page.
<div style="font-family: Arial">
    <strong>Name : </strong>
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Button ID="btnClear" runat="server" onclick="btnClear_Click" Text="Clear"/>
    &nbsp;
    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
    <br />
    <br />
    <asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="#009933"></asp:Label>
</div>



Dobule click the Clear and Submit buttons, to generate the event handlers, and paste the following code in the code behind page.
protected void btnSubmit_Click(object sender, EventArgs e)
{
    lblMessage.Text = "You enetered: " + txtName.Text;
}

protected void btnClear_Click(object sender, EventArgs e)
{
    txtName.Text = "";
}

Now, run the web application project. Enter a name in the textbox and hit the enter key on the keyboard. Notice that the cancel button has the submit behaviour.

In the HTML of the aspx page, set UseSubmitBehavior="false" for the clear button.
<asp:Button ID="btnClear" UseSubmitBehavior="false" runat="server" 
onclick="btnClear_Click" Text="Clear"/>

Now, run the web application project again. Enter a name in the textbox and hit the enter key on the keyboard. Notice that the submit button has the submit behaviour, as expected.

The UseSubmitBehavior property specifies if the Button control uses the browser's built-in submit function or the ASP.NET postback mechanism.

This property is TRUE by default. When set to FALSE, ASP.NET adds a client-side script to post the form. To view the client side script added by the ASP.NET, right click on the broswer and view source.

2 comments:

  1. hi Sir your content presentation is really great.

    God bless U Sir...
    U really Like God for .Net learners...

    ReplyDelete
  2. Can you please tell me in which instance would one want to "Use Submit Behavior" if the button performs the same steps?

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.