Monday, July 20, 2009

ViewState and ListBox

ViewState is to retain value when a page is post back from server.
To preserve value and not to re-populate ListBox or DropDownList in asp.net use following
if (!IsPostBack) //fill the lists
{
  //for network dropdown
  query = "select network from network";
  reader = da.readData(query);

  while (reader.Read())
  {
    Network.Items.Add(reader["network"].ToString());
  }
}
TextBox "readonly" and viewstate
(ref http://aspadvice.com/blogs/joteke/archive/2006/04/12/16409.aspx)
To retain data in a TextBox (datetime picker) in PostBack action use server side code
TextBox1.Attribute.Add ("readonly", "readonly");

rather than the client side


I had trouble figuring out this but this may help some other.