Vishal Gilbile

Vishal Gilbile

  • NA
  • 12k
  • 4.8m

How to hold values generated from javascript function during post back in asp.net

Jul 5 2011 3:16 AM
Dear All,

I came across a project where we have 3 drop downlist in asp.net these are server side control, lets for example consider these 3 drop downlist will hold value of year, month and day. Considering the year and month selected the 3rd dropdown list will display the no of day within that particular month and a year.The values generated within this year drop down list and month drop down list is generated by using javascript function. below is the code for the same.

 <script type="text/javascript">
  var month=new Array(12)
  month[0]="January";
  month[1]="February";
  month[2]="March";
  month[3]="April";
  month[4]="May";
  month[5]="June";
  month[6]="July";
  month[7]="August";
  month[8]="September";
  month[9]="October";
  month[10]="November";
  month[11]="December";
  function getYear()
  {
  var yidx=new Option("Select Year","Select Year");
  document.forms[0]["ddyear"].options.add(yidx);
  for(var i=1;i<=30;i++)
  {
  var item=new Option(1977+i,1977+i,true); 
  document.forms[0]["ddyear"].options.add(item);
  }
 }
 function getMonth()
 {
 var midx=new Option("Select Month","Select Month");
  document.forms[0]["ddmonth"].options.add(midx);
  for(var j=0;j<12;j++)
  {
  var monthname=new Option(month[j],month[j],true);
  document.forms[0]["ddmonth"].options.add(monthname);
  }
  }

I'm calling these function on onload event of the body.

The problem I'm facing is that when a particular value is selected from ddyear or ddmonth drop downlist then the value is not retained because the values are being generated at the client side through javascript functions.

Kindly if anyone knows about how to retain values generated from javascript function in asp.net between server roundtrips then pls let me know the viewstate property of all the 3 drop downlist is set to true.

Answers (5)