469,270 Members | 1,117 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

When to use AutopostBack

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
1 46078
Good Post. Simple and Useful.
Jun 18 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Susan van Houen | last post: by
3 posts views Thread by Lloyd Sheen | last post: by
1 post views Thread by John Oakes | last post: by
2 posts views Thread by Tom Edelbrok | last post: by
3 posts views Thread by Brad | last post: by
6 posts views Thread by Peter | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.