Connect with Expertise | Find Experts, Get Answers, Share Insights

Dropdownlist's enableviewstate not workin

 
Join Date: Dec 2009
Posts: 86
#1: Jan 21 '10
Hi there..

This problem is stopping me from winding up my project. I have a webform where there is a pop-up page that populates one text box and 2 dropdowns. And I also have a checkbox list that does a postback. Whenever the checkboxlist does a postback, one of the dropdownlist's value is reset. It happens with only one dropdown list. The text box and the other dropdownlist retain their value. I have put enableviewstate = true at both the page level and control level.

Somebody please help me.....what is the reason for this strange behavior.
Any response is highly appreciated...

thank you

 
Join Date: Dec 2009
Posts: 86
#2: Jan 22 '10

re: Dropdownlist's enableviewstate not workin


thank you for your response.......it did not help me much though...
please find my code below....

Expand|Select|Wrap|Line Numbers
  1. <asp:DropDownList ID="hsstate" runat="server"
  2.   DataSourceID="SqlDataSource5" DataTextField="StateName"
  3.   DataValueField="Num" EnableViewState="true" AppendDataBoundItems="true">
  4.     <asp:ListItem Value="" Text="---  Please select a state  ---" />
  5. </asp:DropDownList>
  6. <asp:SqlDataSource ID="SqlDataSource5" runat="server"
  7.   ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  8.   SelectCommand="SELECT Num, StateName FROM State WHERE (Num < 63) OR (Num = 76) ORDER BY [StateName]">
  9. </asp:SqlDataSource>
  10. <asp:RequiredFieldValidator ID="RequiredFieldValidator_hsstate" ControlToValidate="hsstate" runat="server" ErrorMessage="Please select your highschool state"
  11.   Enabled="false" Display="Dynamic">*</asp:RequiredFieldValidator>


javascript that populates the dropdown...

Expand|Select|Wrap|Line Numbers
  1. function pick(symbol) {
  2.    var state,city,i;
  3.       fullname = symbol.split(",");
  4. city = fullname[0];
  5.  
  6.    state = fullname[1];
  7.  
  8.  
  9.   if (window.opener && !window.opener.closed)
  10.              window.opener.document.form2.hscity.value = city;
  11.  
  12.  
  13.         for (i=0;i<window.opener.document.getElementById("hsstate").length;i++ )
  14.         {
  15.  
  16.             if (window.opener.document.getElementById("hsstate").options[i].text = state )
  17.             {
  18.              window.opener.document.getElementById("hsstate").selectedIndex = i;
  19.              break;
  20.             }
  21.         }
  22.  
  23.           window.close();
  24.  
  25.     }
I am using a drop-down pulling its data from a table...and the data is binding properly after every page load..it is the selected value that disappears..I mean the selectedindex,say 5, is reset after page load..whatever value the pop-up page selects is present till the page postsback...and after postback it is reset to "Please select the value".


thank you for your time..
Frinavale's Avatar
E
M
C
 
Join Date: Oct 2006
Location: The Great White North
Posts: 6,866
#3: Jan 25 '10

re: Dropdownlist's enableviewstate not workin


User1980,

The DropDownList that is working correctly...is it bound to using an SqlDataSource as well?

-Frinny
 
Join Date: Dec 2009
Posts: 86
#4: Jan 25 '10

re: Dropdownlist's enableviewstate not workin


thank you for the response....yeah..the drop down is binding correctly. I have found what the problem is but unable to fix it....
Expand|Select|Wrap|Line Numbers
  1.  
  2.    1. function pick(symbol) {
  3.    2.    var state,city,i;
  4.    3.       fullname = symbol.split(",");
  5.    4. city = fullname[0];
  6.    5. state = fullname[1];
  7.    7.    
  8.    9.   if (window.opener && !window.opener.closed)
  9.   10.              window.opener.document.form2.hscity.value = city;
  10.   11.  
  11.   13.         for (i=0;i<window.opener.document.getElementById("hsstate").length;i++ )
  12.   14.         {
  13.   15.             if (window.opener.document.getElementById("hsstate").options[i].text = state )
  14.   17.             {
  15.                 window.opener.document.getElementById("hsstate").selectedIndex = i;
  16.   19.              break;
  17.   20.             }
  18.   21.         }
  19.   22.  
  20.   23.           window.close();
  21.   24.  
  22.   25.     }
  23.  
  24.  
in the above javascript, I have found that the selectedindex is always set to 0..ie; when i = 0, the if condition works and breaks and the selectedindex is always set 0, that is the reason my main page is not able to preserve the value and resets back after every postback.
I have no clue why the if condition is true only when i = 0....it is supposed to be true when the original dropdown's text is equal to the required text

I assume the way I am trying to select the dropdown using this script is wrong...
 
Join Date: Dec 2009
Posts: 86
#5: Jan 25 '10

re: Dropdownlist's enableviewstate not workin


hello..i have temporarily solved my problem by replacing the dropdown with a text box..but I have one more problem...

in my pop-up window, the dataview has names that contain " 's " in their names like Byte's, etc....so when I am using my javascript to select this name, the javascript breaks at " 's ".
Ex: pick(Byte
where as it is expected to do pick(Bytes's, testcity)
How can I do this???

thank you for your time
Frinavale's Avatar
E
M
C
 
Join Date: Oct 2006
Location: The Great White North
Posts: 6,866
#6: Jan 25 '10

re: Dropdownlist's enableviewstate not workin


See the comments:

Expand|Select|Wrap|Line Numbers
  1. function pick(symbol) {
  2.   var state,city,i;
  3.   fullname = symbol.split(",");
  4.  
  5. //making sure that there are 2 elements in the fullname array
  6. //before trying to access the elements.
  7.   if(fullname.length==2){
  8.     city = fullname[0];
  9.     state = fullname[1];
  10.   }
  11.  
  12. //I added {} to the following if:  
  13.   if (window.opener && !window.opener.closed)
  14. {
  15.  
  16. //Accessing document.form2 directly is not a good idea.
  17. //Instead use the document.getElementByID() method 
  18. //to access the element "hscity"
  19. //    window.opener.document.form2.hscity.value = city; 
  20.  
  21. //Added the check to make sure hscity exists 
  22. if(window.opener.document.getElementById("hscity") )
  23. {
  24.   window.opener.document.getElementById("hscity").value = city =;
  25. }
  26.  
  27. //I'm assuming that hsstate is the DropDownList
  28. //a DropDownList is rendered as an HTML <select> element
  29. //this element has a bunch of <option> elements within it 
  30. //you have to access the htmlSelectElement.options property 
  31. //to access the "options" 
  32.  
  33. if(window.opener.document.getElementById("hsstate"))
  34. {
  35.  
  36. //Notice that I added .options....
  37.   for (i=0;i<window.opener.document.getElementById("hsstate").options.length;i++ )
  38.   {
  39.     if (window.opener.document.getElementById("hsstate").options[i].text = state )
  40.     {
  41.       window.opener.document.getElementById("hsstate").selectedIndex = i;
  42.       break;
  43.     }
  44.   }
  45. }//close check if hsstate exists
  46.  
  47. //Following line is your code..not sure why you're closing the window.
  48.   window.close();
  49. }//close check if window opener is opened
  50.  
  51. }
Frinavale's Avatar
E
M
C
 
Join Date: Oct 2006
Location: The Great White North
Posts: 6,866
#7: Jan 25 '10

re: Dropdownlist's enableviewstate not workin


I never saw any code that would have a problem with the apostrophe (')...

Usually this will mess things up if you are defining strings using apostrophes instead of double quotes....consider using double quotes instead of single quotes (aka apostrophes) where ever this is a problem.

-Frinny
Reply

Tags
dropdownlist, enableviewstate