capture value from dropdownlist where the display dropdownlist is not default

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












i want to capture selected date on my dropdown list, where there 5 day will display on dropdownlist. im usually put default value on dropdown, but not this time because in drop down list i want it always display current daya and next 5 day. but i dont know how to capture the data.



<asp:DropDownList ID="ddldate" runat="server">
</asp:DropDownList>



 protected void Page_Load(object sender, EventArgs e)

List<ListItem> items = new List<ListItem>();

for (int i = 0; i < 5; i++)

items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));

ddldate.DataSource = items;
ddldate.DataBind();
ddldate.Items[0].Selected = true;




 protected void Button1_Click(object sender, EventArgs e)

string deliverytime = ddldate.SelectedValue.ToString();
lbltest.Text = deliverytime;










share

























    up vote
    6
    down vote

    favorite












    i want to capture selected date on my dropdown list, where there 5 day will display on dropdownlist. im usually put default value on dropdown, but not this time because in drop down list i want it always display current daya and next 5 day. but i dont know how to capture the data.



    <asp:DropDownList ID="ddldate" runat="server">
    </asp:DropDownList>



     protected void Page_Load(object sender, EventArgs e)

    List<ListItem> items = new List<ListItem>();

    for (int i = 0; i < 5; i++)

    items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));

    ddldate.DataSource = items;
    ddldate.DataBind();
    ddldate.Items[0].Selected = true;




     protected void Button1_Click(object sender, EventArgs e)

    string deliverytime = ddldate.SelectedValue.ToString();
    lbltest.Text = deliverytime;










    share























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      i want to capture selected date on my dropdown list, where there 5 day will display on dropdownlist. im usually put default value on dropdown, but not this time because in drop down list i want it always display current daya and next 5 day. but i dont know how to capture the data.



      <asp:DropDownList ID="ddldate" runat="server">
      </asp:DropDownList>



       protected void Page_Load(object sender, EventArgs e)

      List<ListItem> items = new List<ListItem>();

      for (int i = 0; i < 5; i++)

      items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));

      ddldate.DataSource = items;
      ddldate.DataBind();
      ddldate.Items[0].Selected = true;




       protected void Button1_Click(object sender, EventArgs e)

      string deliverytime = ddldate.SelectedValue.ToString();
      lbltest.Text = deliverytime;










      share













      i want to capture selected date on my dropdown list, where there 5 day will display on dropdownlist. im usually put default value on dropdown, but not this time because in drop down list i want it always display current daya and next 5 day. but i dont know how to capture the data.



      <asp:DropDownList ID="ddldate" runat="server">
      </asp:DropDownList>



       protected void Page_Load(object sender, EventArgs e)

      List<ListItem> items = new List<ListItem>();

      for (int i = 0; i < 5; i++)

      items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));

      ddldate.DataSource = items;
      ddldate.DataBind();
      ddldate.Items[0].Selected = true;




       protected void Button1_Click(object sender, EventArgs e)

      string deliverytime = ddldate.SelectedValue.ToString();
      lbltest.Text = deliverytime;








      c# asp.net





      share












      share










      share



      share










      asked 1 hour ago









      Ariff Naj

      507




      507






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You're repopulating the DropDownList for every postback and reloading the page, hence SelectedValue property value may be different from posted value. Just put a check against IsPostBack to prevent repopulating DropDownList data on postback:



          protected void Page_Load(object sender, EventArgs e)

          if (!IsPostBack)

          List<ListItem> items = new List<ListItem>();

          for (int i = 0; i < 5; i++)

          items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));


          ddldate.DataSource = items;
          ddldate.DataBind();
          ddldate.Items[0].Selected = true;







          share|improve this answer




















          • thx,, i miss look at that
            – Ariff Naj
            24 mins ago

















          up vote
          5
          down vote













          You should not bind data on PostaBack, change your FormLoad code to below sample:



          protected void Page_Load(object sender, EventArgs e)

          if(!Page.IsPostBack)

          List<ListItem> items = new List<ListItem>();

          for (int i = 0; i < 5; i++)

          items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));

          ddldate.DataSource = items;
          ddldate.DataBind();
          ddldate.Items[0].Selected = true;




          If you check the PostBack property as condition, your SelectedValue will keep, otherwise DropDown will bind on each page-post.



          And I also recommend you to check SelectedValue status before use it, don't try to get value if this null, check the following code:



          protected void Button1_Click(object sender, EventArgs e)

          if(ddldate.SelectedValue != null)

          string deliverytime = ddldate.SelectedValue.ToString();
          lbltest.Text = deliverytime;







          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "",
            contentPolicyHtml: "",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53076426%2fcapture-value-from-dropdownlist-where-the-display-dropdownlist-is-not-default%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            You're repopulating the DropDownList for every postback and reloading the page, hence SelectedValue property value may be different from posted value. Just put a check against IsPostBack to prevent repopulating DropDownList data on postback:



            protected void Page_Load(object sender, EventArgs e)

            if (!IsPostBack)

            List<ListItem> items = new List<ListItem>();

            for (int i = 0; i < 5; i++)

            items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));


            ddldate.DataSource = items;
            ddldate.DataBind();
            ddldate.Items[0].Selected = true;







            share|improve this answer




















            • thx,, i miss look at that
              – Ariff Naj
              24 mins ago














            up vote
            3
            down vote



            accepted










            You're repopulating the DropDownList for every postback and reloading the page, hence SelectedValue property value may be different from posted value. Just put a check against IsPostBack to prevent repopulating DropDownList data on postback:



            protected void Page_Load(object sender, EventArgs e)

            if (!IsPostBack)

            List<ListItem> items = new List<ListItem>();

            for (int i = 0; i < 5; i++)

            items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));


            ddldate.DataSource = items;
            ddldate.DataBind();
            ddldate.Items[0].Selected = true;







            share|improve this answer




















            • thx,, i miss look at that
              – Ariff Naj
              24 mins ago












            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            You're repopulating the DropDownList for every postback and reloading the page, hence SelectedValue property value may be different from posted value. Just put a check against IsPostBack to prevent repopulating DropDownList data on postback:



            protected void Page_Load(object sender, EventArgs e)

            if (!IsPostBack)

            List<ListItem> items = new List<ListItem>();

            for (int i = 0; i < 5; i++)

            items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));


            ddldate.DataSource = items;
            ddldate.DataBind();
            ddldate.Items[0].Selected = true;







            share|improve this answer












            You're repopulating the DropDownList for every postback and reloading the page, hence SelectedValue property value may be different from posted value. Just put a check against IsPostBack to prevent repopulating DropDownList data on postback:



            protected void Page_Load(object sender, EventArgs e)

            if (!IsPostBack)

            List<ListItem> items = new List<ListItem>();

            for (int i = 0; i < 5; i++)

            items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));


            ddldate.DataSource = items;
            ddldate.DataBind();
            ddldate.Items[0].Selected = true;








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 51 mins ago









            Tetsuya Yamamoto

            12.5k41839




            12.5k41839











            • thx,, i miss look at that
              – Ariff Naj
              24 mins ago
















            • thx,, i miss look at that
              – Ariff Naj
              24 mins ago















            thx,, i miss look at that
            – Ariff Naj
            24 mins ago




            thx,, i miss look at that
            – Ariff Naj
            24 mins ago












            up vote
            5
            down vote













            You should not bind data on PostaBack, change your FormLoad code to below sample:



            protected void Page_Load(object sender, EventArgs e)

            if(!Page.IsPostBack)

            List<ListItem> items = new List<ListItem>();

            for (int i = 0; i < 5; i++)

            items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));

            ddldate.DataSource = items;
            ddldate.DataBind();
            ddldate.Items[0].Selected = true;




            If you check the PostBack property as condition, your SelectedValue will keep, otherwise DropDown will bind on each page-post.



            And I also recommend you to check SelectedValue status before use it, don't try to get value if this null, check the following code:



            protected void Button1_Click(object sender, EventArgs e)

            if(ddldate.SelectedValue != null)

            string deliverytime = ddldate.SelectedValue.ToString();
            lbltest.Text = deliverytime;







            share|improve this answer
























              up vote
              5
              down vote













              You should not bind data on PostaBack, change your FormLoad code to below sample:



              protected void Page_Load(object sender, EventArgs e)

              if(!Page.IsPostBack)

              List<ListItem> items = new List<ListItem>();

              for (int i = 0; i < 5; i++)

              items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));

              ddldate.DataSource = items;
              ddldate.DataBind();
              ddldate.Items[0].Selected = true;




              If you check the PostBack property as condition, your SelectedValue will keep, otherwise DropDown will bind on each page-post.



              And I also recommend you to check SelectedValue status before use it, don't try to get value if this null, check the following code:



              protected void Button1_Click(object sender, EventArgs e)

              if(ddldate.SelectedValue != null)

              string deliverytime = ddldate.SelectedValue.ToString();
              lbltest.Text = deliverytime;







              share|improve this answer






















                up vote
                5
                down vote










                up vote
                5
                down vote









                You should not bind data on PostaBack, change your FormLoad code to below sample:



                protected void Page_Load(object sender, EventArgs e)

                if(!Page.IsPostBack)

                List<ListItem> items = new List<ListItem>();

                for (int i = 0; i < 5; i++)

                items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));

                ddldate.DataSource = items;
                ddldate.DataBind();
                ddldate.Items[0].Selected = true;




                If you check the PostBack property as condition, your SelectedValue will keep, otherwise DropDown will bind on each page-post.



                And I also recommend you to check SelectedValue status before use it, don't try to get value if this null, check the following code:



                protected void Button1_Click(object sender, EventArgs e)

                if(ddldate.SelectedValue != null)

                string deliverytime = ddldate.SelectedValue.ToString();
                lbltest.Text = deliverytime;







                share|improve this answer












                You should not bind data on PostaBack, change your FormLoad code to below sample:



                protected void Page_Load(object sender, EventArgs e)

                if(!Page.IsPostBack)

                List<ListItem> items = new List<ListItem>();

                for (int i = 0; i < 5; i++)

                items.Add(new ListItem(DateTime.Now.AddDays(i).ToShortDateString(), DateTime.Now.AddDays(i).ToShortDateString()));

                ddldate.DataSource = items;
                ddldate.DataBind();
                ddldate.Items[0].Selected = true;




                If you check the PostBack property as condition, your SelectedValue will keep, otherwise DropDown will bind on each page-post.



                And I also recommend you to check SelectedValue status before use it, don't try to get value if this null, check the following code:



                protected void Button1_Click(object sender, EventArgs e)

                if(ddldate.SelectedValue != null)

                string deliverytime = ddldate.SelectedValue.ToString();
                lbltest.Text = deliverytime;








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 58 mins ago









                Siavash Ghanbari

                1,232920




                1,232920



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53076426%2fcapture-value-from-dropdownlist-where-the-display-dropdownlist-is-not-default%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?