Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dropdown list value is not retaining on page post back [modified]

Dropdown list value is not retaining on page post back [modified]

Scheduled Pinned Locked Moved ASP.NET
databasegraphicsdesignxmlhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    skhan17
    wrote on last edited by
    #1

    Dear all, I am having problem with placing the proper condition inside the postback method. I am trying to do the following: 1. There are 2 dropdown lists in my program. 2. I have two DropDownLists databound on the webform. The autopostback of the first dropdown list control is set to true. The contents of the first dropdown list is hardcoded. The contents of the second depend on the selected item in the first. In other words: The selectedvalue of the first DropDownList is passed as a parameter for the query at the basis of the second DropDownList's datasource. And this works: Whenever I click on an item in the first DropDownList, the dependant items are shown in the second DropDownList. 3. If I click submit then the specific rows from the DB will be pulled based on the values selected from dropdown list 1 and dropdown list 2. Problem: In the postback method I think all the times the "ANY" MLNO is selected instead of selecting a specific value from the dropdown list 2. And that's why always all the values are pulled from the DB. The code is as follows: Code:

    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Xml;
    using System.Globalization;

    namespace HIV
    {
    /// <summary>
    /// Summary description for run_specific_query.
    /// </summary>
    public class run_specific_query : System.Web.UI.Page
    {
    protected HIV.Controls.NavMenu navMenu;
    protected HIV.Controls.NavSubMenu navSubMenu;
    protected System.Web.UI.HtmlControls.HtmlGenericControl message;
    protected System.Web.UI.WebControls.Label resultsLabel;
    protected System.Web.UI.WebControls.DropDownList Specific_Query_DDL;
    protected System.Web.UI.WebControls.DropDownList mlno_DDL;
    //protected System.Web.UI.WebControls.TextBox date_TB;
    protected System.Web.UI.WebControls.CompareValidator dateValidator;
    protected System.Web.UI.WebControls.DataGrid resultsDatagrid;
    protected System.Web.UI.WebControls.Button submitButton;
    protected System.Web.UI.WebControls.Label data_src;
    protected System.Web.UI.WebControls.LinkButton exportLinkbutton;

        private void Page\_Load(object sender, System.EventArgs e)
        {
            navMenu.SelectedMainItem = HIV.Controls.NavMenu.MainItems.QUERY;
            navSubMe
    
    A C 2 Replies Last reply
    0
    • S skhan17

      Dear all, I am having problem with placing the proper condition inside the postback method. I am trying to do the following: 1. There are 2 dropdown lists in my program. 2. I have two DropDownLists databound on the webform. The autopostback of the first dropdown list control is set to true. The contents of the first dropdown list is hardcoded. The contents of the second depend on the selected item in the first. In other words: The selectedvalue of the first DropDownList is passed as a parameter for the query at the basis of the second DropDownList's datasource. And this works: Whenever I click on an item in the first DropDownList, the dependant items are shown in the second DropDownList. 3. If I click submit then the specific rows from the DB will be pulled based on the values selected from dropdown list 1 and dropdown list 2. Problem: In the postback method I think all the times the "ANY" MLNO is selected instead of selecting a specific value from the dropdown list 2. And that's why always all the values are pulled from the DB. The code is as follows: Code:

      using System.Data;
      using System.Data.OleDb;
      using System.Drawing;
      using System.Web;
      using System.Web.SessionState;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.HtmlControls;
      using System.IO;
      using System.Xml;
      using System.Globalization;

      namespace HIV
      {
      /// <summary>
      /// Summary description for run_specific_query.
      /// </summary>
      public class run_specific_query : System.Web.UI.Page
      {
      protected HIV.Controls.NavMenu navMenu;
      protected HIV.Controls.NavSubMenu navSubMenu;
      protected System.Web.UI.HtmlControls.HtmlGenericControl message;
      protected System.Web.UI.WebControls.Label resultsLabel;
      protected System.Web.UI.WebControls.DropDownList Specific_Query_DDL;
      protected System.Web.UI.WebControls.DropDownList mlno_DDL;
      //protected System.Web.UI.WebControls.TextBox date_TB;
      protected System.Web.UI.WebControls.CompareValidator dateValidator;
      protected System.Web.UI.WebControls.DataGrid resultsDatagrid;
      protected System.Web.UI.WebControls.Button submitButton;
      protected System.Web.UI.WebControls.Label data_src;
      protected System.Web.UI.WebControls.LinkButton exportLinkbutton;

          private void Page\_Load(object sender, System.EventArgs e)
          {
              navMenu.SelectedMainItem = HIV.Controls.NavMenu.MainItems.QUERY;
              navSubMe
      
      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      Yes.. the culprit is your page load method.. Just for every post-back, even when submit button is clicked, ASP.NET first calles Page_Load and after its execution it calles the general event handler. Just omit the lines

      if (this.IsPostBack)
      {
      if(Specific_Query_DDL.SelectedItem.Value=="ResistantL")
      {
      getMLNO(Specific_Query_DDL.SelectedItem.Value);
      }
      if(Specific_Query_DDL.SelectedItem.Value=="NegativeL")
      {
      getMLNO(Specific_Query_DDL.SelectedItem.Value);
      }
      }

      and place them in Listbox1_selectedIndexChanged event. I thin as that is called specific when postback occurs on selected indexchanged event, the submit will not load the 2nd dropdown onsubmit. The problem will be fixed then... :thumbsup::thumbsup::cool:

      Abhishek Sur


      My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

      **Don't forget to click "Good Answer" if you like to.

      S 1 Reply Last reply
      0
      • A Abhishek Sur

        Yes.. the culprit is your page load method.. Just for every post-back, even when submit button is clicked, ASP.NET first calles Page_Load and after its execution it calles the general event handler. Just omit the lines

        if (this.IsPostBack)
        {
        if(Specific_Query_DDL.SelectedItem.Value=="ResistantL")
        {
        getMLNO(Specific_Query_DDL.SelectedItem.Value);
        }
        if(Specific_Query_DDL.SelectedItem.Value=="NegativeL")
        {
        getMLNO(Specific_Query_DDL.SelectedItem.Value);
        }
        }

        and place them in Listbox1_selectedIndexChanged event. I thin as that is called specific when postback occurs on selected indexchanged event, the submit will not load the 2nd dropdown onsubmit. The problem will be fixed then... :thumbsup::thumbsup::cool:

        Abhishek Sur


        My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

        **Don't forget to click "Good Answer" if you like to.

        S Offline
        S Offline
        skhan17
        wrote on last edited by
        #3

        Thank you Mr. Sur. I knew that the culprit is the page load method.. but didn't know how to populate the second dropdown list without the pageload. Should I put the code you mentioned exactly inside the Specific_Query_DDL_selectedIndexChanged function ? And should I call that function inside the page_Load function? Please suggest. Thanks in advance.

        A 1 Reply Last reply
        0
        • S skhan17

          Thank you Mr. Sur. I knew that the culprit is the page load method.. but didn't know how to populate the second dropdown list without the pageload. Should I put the code you mentioned exactly inside the Specific_Query_DDL_selectedIndexChanged function ? And should I call that function inside the page_Load function? Please suggest. Thanks in advance.

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #4

          No.. You dont need to call selectedIndexChanged function from page_load, it will automatically been called. Rather than that, you just write if(IsPostBack) return; in the page_load, to eliminate any code to be executed in page_load during postbacks. When the selectedIndexChanged is posting back the page to the server, it always calls the Page_Load event first, then its original event handler. So you just need to suppress page_load during event calls. Hope you got it clearly.

          Abhishek Sur


          My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

          **Don't forget to click "Good Answer" if you like to.

          C 1 Reply Last reply
          0
          • A Abhishek Sur

            No.. You dont need to call selectedIndexChanged function from page_load, it will automatically been called. Rather than that, you just write if(IsPostBack) return; in the page_load, to eliminate any code to be executed in page_load during postbacks. When the selectedIndexChanged is posting back the page to the server, it always calls the Page_Load event first, then its original event handler. So you just need to suppress page_load during event calls. Hope you got it clearly.

            Abhishek Sur


            My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

            **Don't forget to click "Good Answer" if you like to.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            Abhishek Sur wrote:

            if(IsPostBack) return; in the page_load, to eliminate any code to be executed in page_load during postbacks.

            This is nuts. It may work for the code he has today, but it's a terrible paradigm. Why should one not write code in page load that needs to run every time ? Instead, put if (!IsPostback) blocks around the specific code you need to stop running

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            A 1 Reply Last reply
            0
            • S skhan17

              Dear all, I am having problem with placing the proper condition inside the postback method. I am trying to do the following: 1. There are 2 dropdown lists in my program. 2. I have two DropDownLists databound on the webform. The autopostback of the first dropdown list control is set to true. The contents of the first dropdown list is hardcoded. The contents of the second depend on the selected item in the first. In other words: The selectedvalue of the first DropDownList is passed as a parameter for the query at the basis of the second DropDownList's datasource. And this works: Whenever I click on an item in the first DropDownList, the dependant items are shown in the second DropDownList. 3. If I click submit then the specific rows from the DB will be pulled based on the values selected from dropdown list 1 and dropdown list 2. Problem: In the postback method I think all the times the "ANY" MLNO is selected instead of selecting a specific value from the dropdown list 2. And that's why always all the values are pulled from the DB. The code is as follows: Code:

              using System.Data;
              using System.Data.OleDb;
              using System.Drawing;
              using System.Web;
              using System.Web.SessionState;
              using System.Web.UI;
              using System.Web.UI.WebControls;
              using System.Web.UI.HtmlControls;
              using System.IO;
              using System.Xml;
              using System.Globalization;

              namespace HIV
              {
              /// <summary>
              /// Summary description for run_specific_query.
              /// </summary>
              public class run_specific_query : System.Web.UI.Page
              {
              protected HIV.Controls.NavMenu navMenu;
              protected HIV.Controls.NavSubMenu navSubMenu;
              protected System.Web.UI.HtmlControls.HtmlGenericControl message;
              protected System.Web.UI.WebControls.Label resultsLabel;
              protected System.Web.UI.WebControls.DropDownList Specific_Query_DDL;
              protected System.Web.UI.WebControls.DropDownList mlno_DDL;
              //protected System.Web.UI.WebControls.TextBox date_TB;
              protected System.Web.UI.WebControls.CompareValidator dateValidator;
              protected System.Web.UI.WebControls.DataGrid resultsDatagrid;
              protected System.Web.UI.WebControls.Button submitButton;
              protected System.Web.UI.WebControls.Label data_src;
              protected System.Web.UI.WebControls.LinkButton exportLinkbutton;

                  private void Page\_Load(object sender, System.EventArgs e)
                  {
                      navMenu.SelectedMainItem = HIV.Controls.NavMenu.MainItems.QUERY;
                      navSubMe
              
              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Yes, this is still far too much code to be posting. You don't need to be reloading the data every time, unless you turn off viewstate. If you do need to, do it in page_prerender, that's the right place to do it anyhow. Then it will happen AFTER your event fires.

              skhan17 wrote:

              if(Specific_Query_DDL.SelectedItem.Value=="default")

              Hard coded strings are a bad idea. can't you store an enum or something in the value ?

              skhan17 wrote:

              string sql= "SELECT re_MLNO,sum(re_HIV1_Status) AS hivNeg, MAX(re_SpecimenDate)- MIN(re_SpecimenDate) AS DaysSeronegative FROM ml_hiv_status GROUP BY re_MLNO"; OleDbConnection connection = new OleDbConnection(HIV.Database.DataConstants.CONNECTION_STRING); OleDbDataAdapter adapter = new OleDbDataAdapter(); OleDbCommand command = new OleDbCommand(sql, connection);

              SQL in the presentation layer is always a sign of no design. I'd fire anyone who presented code like this to me.

              skhan17 wrote:

              if(Specific_Query_DDL.SelectedItem.Value=="ResistantL") { getMLNO(Specific_Query_DDL.SelectedItem.Value); } if(Specific_Query_DDL.SelectedItem.Value=="NegativeL") { getMLNO(Specific_Query_DDL.SelectedItem.Value); }

              Hard coded strings are still bad. And, have you never heard of the || operator ?

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

              1 Reply Last reply
              0
              • C Christian Graus

                Abhishek Sur wrote:

                if(IsPostBack) return; in the page_load, to eliminate any code to be executed in page_load during postbacks.

                This is nuts. It may work for the code he has today, but it's a terrible paradigm. Why should one not write code in page load that needs to run every time ? Instead, put if (!IsPostback) blocks around the specific code you need to stop running

                Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                A Offline
                A Offline
                Abhishek Sur
                wrote on last edited by
                #7

                Actually If you saw the code above, the person is populating the Dropdown from Page_load.

                Christian Graus wrote:

                Why should one not write code in page load that needs to run every time

                Actually your question will get you the answer. Yes of course.. If one needs to run a code every time, he could have write this before the line, or if specific to the postback he could use it just within the if block. I think it depends upon the requirement. :-\ :-\ The code that he wrote doesnt require to populate dropdown during postbacks. so he should eliminate the function call during each postback as the page which is posted back already had the items in the select. :rose:

                Abhishek Sur


                My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

                **Don't forget to click "Good Answer" if you like to.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups