Dropdownlist's enableviewstate not workin

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • user1980
    New Member
    • Dec 2009
    • 112

    Dropdownlist's enableviewstate not workin

    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
  • user1980
    New Member
    • Dec 2009
    • 112

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

    Code:
    <asp:DropDownList ID="hsstate" runat="server"
      DataSourceID="SqlDataSource5" DataTextField="StateName"
      DataValueField="Num" EnableViewState="true" AppendDataBoundItems="true">
        <asp:ListItem Value="" Text="---  Please select a state  ---" />
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource5" runat="server"
      ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
      SelectCommand="SELECT Num, StateName FROM State WHERE (Num < 63) OR (Num = 76) ORDER BY [StateName]">
    </asp:SqlDataSource>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator_hsstate" ControlToValidate="hsstate" runat="server" ErrorMessage="Please select your highschool state"
      Enabled="false" Display="Dynamic">*</asp:RequiredFieldValidator>


    javascript that populates the dropdown...

    Code:
    function pick(symbol) {
       var state,city,i;
          fullname = symbol.split(",");
    city = fullname[0];
      
       state = fullname[1];
    
    
      if (window.opener && !window.opener.closed)
                 window.opener.document.form2.hscity.value = city;
    
    
            for (i=0;i<window.opener.document.getElementById("hsstate").length;i++ )
            {
         
                if (window.opener.document.getElementById("hsstate").options[i].text = state )
                {
                 window.opener.document.getElementById("hsstate").selectedIndex = i;
                 break;
                }
            }
             
              window.close();
          
        }
    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,s ay 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..

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      User1980,

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

      -Frinny

      Comment

      • user1980
        New Member
        • Dec 2009
        • 112

        #4
        thank you for the response....yea h..the drop down is binding correctly. I have found what the problem is but unable to fix it....
        Code:
           1. function pick(symbol) {
           2.    var state,city,i;
           3.       fullname = symbol.split(",");
           4. city = fullname[0];
           5. state = fullname[1];
           7.    
           9.   if (window.opener && !window.opener.closed)
          10.              window.opener.document.form2.hscity.value = city;
          11.  
          13.         for (i=0;i<window.opener.document.getElementById("hsstate").length;i++ )
          14.         {
          15.             if (window.opener.document.getElementById("hsstate").options[i].text = state )
          17.             {
                        window.opener.document.getElementById("hsstate").selectedIndex = i;
          19.              break;
          20.             }
          21.         }
          22.  
          23.           window.close();
          24.  
          25.     }
        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...

        Comment

        • user1980
          New Member
          • Dec 2009
          • 112

          #5
          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

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            See the comments:

            Code:
            function pick(symbol) {
              var state,city,i;
              fullname = symbol.split(",");
            
            //making sure that there are 2 elements in the fullname array
            //before trying to access the elements.
              if(fullname.length==2){
                city = fullname[0];
                state = fullname[1];
              }
             
            //I added {} to the following if:  
              if (window.opener && !window.opener.closed)
            {
            
            //Accessing document.form2 directly is not a good idea.
            //Instead use the document.getElementByID() method 
            //to access the element "hscity"
            //    window.opener.document.form2.hscity.value = city; 
            
            //Added the check to make sure hscity exists 
            if(window.opener.document.getElementById("hscity") )
            {
              window.opener.document.getElementById("hscity").value = city =;
            }
            
            //I'm assuming that hsstate is the DropDownList
            //a DropDownList is rendered as an HTML <select> element
            //this element has a bunch of <option> elements within it 
            //you have to access the htmlSelectElement.options property 
            //to access the "options" 
            
            if(window.opener.document.getElementById("hsstate"))
            {
            
            //Notice that I added .options....
              for (i=0;i<window.opener.document.getElementById("hsstate").options.length;i++ )
              {
                if (window.opener.document.getElementById("hsstate").options[i].text = state )
                {
                  window.opener.document.getElementById("hsstate").selectedIndex = i;
                  break;
                }
              }
            }//close check if hsstate exists
            
            //Following line is your code..not sure why you're closing the window.
              window.close();
            }//close check if window opener is opened
            
            }

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              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....consi der using double quotes instead of single quotes (aka apostrophes) where ever this is a problem.

              -Frinny

              Comment

              Working...