Please help me understand AutoPostBack property of an ASP.NET control

前端 未结 6 1672
时光取名叫无心
时光取名叫无心 2021-01-21 00:16

I\'m learning about ASP.NET, mainly by following through a book, but also making an effort to actually do things along the way. But, I came across an explanation about list cont

相关标签:
6条回答
  • 2021-01-21 00:21

    The responsibility of an asp.net control contained in a Page is to render a part of the html that the user will end up seeing in his browser. Some controls support the AutoPostBack property. What it does is that it makes the control emit some extra javascript that will submit the form whenever the value of that control is changed, so that you can react to this on the server side.

    0 讨论(0)
  • 2021-01-21 00:26

    If you set AutoPostBack="true" on a control, when it's value changes, it will automatically postback to the server.

    For example if you wanted a dropdown that when changed displayed different data in a table below or something, you might want to postback get the new value so your page could refresh the data.

    This is opposed to style of the dropdown and a button beside it you click to postback, so instead of change value, click the button, you can just change the value with AutoPostBack="true".

    0 讨论(0)
  • 2021-01-21 00:29

    For normal client controls (such as a list control with AutoPostBack set to false), when a user chooses an item in the list, the browser does not communicate with the server. There's no network traffic and no delay for your user before they see the results of the choice, but there's also no opportunity to do anything in your server code, like calculate dependent values. If you want to do anything to the screen in response to the choice, you have to use a client-side script.

    When AutoPostBack is set to true, selecting an item in the list sends a message to the server (via an HTTP POST). ASP.NET then executes whatever code you have attached to the list's changed event, rebuilds the page, and sends the revised page to the client.

    0 讨论(0)
  • 2021-01-21 00:32
    • Basically AutoPostBack is used so that whenever there is some change in the controls text or anyother change in the controls property, the page is submitted to the server.
    • Posting the page means, the page is submitted to the server. Suppose i use a textbox and i make its AutoPostBack = "true", now i write some text into it and click outside the textbox, then the page will refresh.
    • This refresh indicates that your value which you entered into the textbox has been submitted to the server.
    • The postback is handled by ASP.NET server. AutoPostBack will automatically post back your page to the server.
    • Add an event Handler. This will give you a better picture. In your case of DropDownList: Add an eventhandler: double click the DropDownList, it will route you to an eventhandler:
    • Write something in that event handler let us say : Response.Write("message");
    • The page will refresh and you will see your message, this means the page was posted to the server and the server has executed your event handler and displayed you the message.

    I hope this was usefull

    0 讨论(0)
  • 2021-01-21 00:38

    A ListBox has a SelectedIndexChanged event that you can handle to detect when the selected item in a ListBox has changed. You'd configure it like this:

    <asp:ListBox ID="ListBox1" runat="server" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"/>
    
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Do something         
    }
    

    With AutoPostBack="false" (the default), that event handler doesn't actually happen on the server in "real time". The user has to perform an unrelated action to submit (POST) the form, such as clicking a button, to make that event fire on the server.

    If you want to take "real time" action on that event, you set AutoPostBack="true" which makes the form automatically submit every time the selected item is changed.

    The benefit - you get "real time" notification of events. The drawback - the page talks a lot more to the server, so each click costs bandwidth and causes client "lag".

    Further reading: http://www.dotnetspider.com/resources/189-AutoPostBack-What-How-works.aspx

    0 讨论(0)
  • 2021-01-21 00:40

    When a user selects a ListItem (or whatever the collection item is), the page should automatically submit the web form to the server with a POST event.

    here's the wikipedia page on HTTP POST events http://en.wikipedia.org/wiki/HTTP_POST

    0 讨论(0)
提交回复
热议问题