By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,011 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

When to use AutopostBack

P: 1
AutopostBack is a property typically used in Dropdownlist,Radiobuttonlist, ListBox controls.
You will use this controls to dispaly set of items,
when you click on item in the list,selectedindexchanged
event will be fired.anyway by default nothing happens with
this event.
if you want an action has to be occured.you should make autopostback="True".so whenever an event should fire and
need to data to be postedback you will use this..
here is an example of autopostback..

Source Code:

Expand|Select|Wrap|Line Numbers
  1. <body>
  2.     <form id="form1" runat="server">
  3.     <div>
  4.     <asp:ListBox ID="lb1" Rows="4" runat="server" AutoPostBack="true" >
  5.     <asp:ListItem>India</asp:ListItem>
  6.     <asp:ListItem>Southafrica</asp:ListItem>
  7.     <asp:ListItem>Australia</asp:ListItem>
  8.     <asp:ListItem>England</asp:ListItem>
  9.  
  10.  
  11.     </asp:ListBox>
  12.     <br />
  13.     <br />
  14.     <asp:Label ID="lab1" runat="server">
  15.     </asp:Label>
  16.     </div>
  17.     </form>
  18. </body> 
  19.  
Code behind:

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.  
  4.         }
  5.  
  6.         protected void lb1_SelectedIndexChanged(object sender, EventArgs e)
  7.         {
  8.             if (lb1.SelectedItem != null)
  9.             {
  10.                 lab1.Text = "you selected" + lb1.SelectedItem.Value;
  11.             }
  12.             else
  13.             {
  14.                 lab1.Text = "";
  15.             }
  16.  
  17.         }
  18.  
Note: you can also write that code in Page_load event also...
May 9 '12 #1
Share this Article
Share on Google+
1 Comment


India777
P: 61
Good Post. Simple and Useful.
Jun 18 '12 #2