Get selected value











up vote
0
down vote

favorite












I like to have a filter for a table. That's why I have a dropdown list and when I press the button filter it should change the table. After pressing the button, I come to the method btnFilter_Click. When I try to get the selected value from the dropdown list, I get the defaut value (the last inserted value in the Dropdown list) and not the one I've selected before.



HTML:



<div class="col-sm-9">
<asp:DropDownList runat="server" ID="ddlYearForProject" CssClass="form-control">
</asp:DropDownList>
</div>


C#:



Page_Load:



if(!IsPostback){
if (ddlYearForProject.Items.Count == 0)
{
ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-3).Year.ToString(), string.Empty));
ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-2).Year.ToString(), string.Empty));
ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));
ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.Year.ToString(), string.Empty));
}
}


btnFilter_Click:



stringYear = ddlYearForProject.SelectedItem.Text;


I also tried to get the value in the page_Load, but there the selected value is the default value too.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I like to have a filter for a table. That's why I have a dropdown list and when I press the button filter it should change the table. After pressing the button, I come to the method btnFilter_Click. When I try to get the selected value from the dropdown list, I get the defaut value (the last inserted value in the Dropdown list) and not the one I've selected before.



    HTML:



    <div class="col-sm-9">
    <asp:DropDownList runat="server" ID="ddlYearForProject" CssClass="form-control">
    </asp:DropDownList>
    </div>


    C#:



    Page_Load:



    if(!IsPostback){
    if (ddlYearForProject.Items.Count == 0)
    {
    ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-3).Year.ToString(), string.Empty));
    ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-2).Year.ToString(), string.Empty));
    ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));
    ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.Year.ToString(), string.Empty));
    }
    }


    btnFilter_Click:



    stringYear = ddlYearForProject.SelectedItem.Text;


    I also tried to get the value in the page_Load, but there the selected value is the default value too.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I like to have a filter for a table. That's why I have a dropdown list and when I press the button filter it should change the table. After pressing the button, I come to the method btnFilter_Click. When I try to get the selected value from the dropdown list, I get the defaut value (the last inserted value in the Dropdown list) and not the one I've selected before.



      HTML:



      <div class="col-sm-9">
      <asp:DropDownList runat="server" ID="ddlYearForProject" CssClass="form-control">
      </asp:DropDownList>
      </div>


      C#:



      Page_Load:



      if(!IsPostback){
      if (ddlYearForProject.Items.Count == 0)
      {
      ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-3).Year.ToString(), string.Empty));
      ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-2).Year.ToString(), string.Empty));
      ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));
      ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.Year.ToString(), string.Empty));
      }
      }


      btnFilter_Click:



      stringYear = ddlYearForProject.SelectedItem.Text;


      I also tried to get the value in the page_Load, but there the selected value is the default value too.










      share|improve this question















      I like to have a filter for a table. That's why I have a dropdown list and when I press the button filter it should change the table. After pressing the button, I come to the method btnFilter_Click. When I try to get the selected value from the dropdown list, I get the defaut value (the last inserted value in the Dropdown list) and not the one I've selected before.



      HTML:



      <div class="col-sm-9">
      <asp:DropDownList runat="server" ID="ddlYearForProject" CssClass="form-control">
      </asp:DropDownList>
      </div>


      C#:



      Page_Load:



      if(!IsPostback){
      if (ddlYearForProject.Items.Count == 0)
      {
      ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-3).Year.ToString(), string.Empty));
      ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-2).Year.ToString(), string.Empty));
      ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));
      ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.Year.ToString(), string.Empty));
      }
      }


      btnFilter_Click:



      stringYear = ddlYearForProject.SelectedItem.Text;


      I also tried to get the value in the page_Load, but there the selected value is the default value too.







      c# html asp.net .net






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 at 7:39

























      asked Nov 14 at 5:32









      user10356084

      77




      77
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Your problem is in the string.empty all your values will be empty and this is what the dropdown uses to identify the selected item.



          Change



          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));


          to



          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), DateTime.Today.AddYears(-1).Year.ToString()));


          This will give the text and value to the dropdown. Or you can use an other unique value like an int or so. But if it is the same for multiple items it will not work like you expect.






          share|improve this answer





















          • Thanks, works fine
            – user10356084
            Nov 14 at 8:33


















          up vote
          0
          down vote













          Make this update in the Page Load.



          if (!IsPostBack)
          {
          if (ddlYearForProject.Items.Count == 0)
          {
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-3).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-2).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.Year.ToString(), string.Empty));
          }
          }





          share|improve this answer

















          • 1




            Please explain your answer
            – Nakul
            Nov 14 at 7:09










          • Forget to write it, but there is already a if(!IsPostback)
            – user10356084
            Nov 14 at 7:40











          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          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%2f53293746%2fget-selected-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          Your problem is in the string.empty all your values will be empty and this is what the dropdown uses to identify the selected item.



          Change



          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));


          to



          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), DateTime.Today.AddYears(-1).Year.ToString()));


          This will give the text and value to the dropdown. Or you can use an other unique value like an int or so. But if it is the same for multiple items it will not work like you expect.






          share|improve this answer





















          • Thanks, works fine
            – user10356084
            Nov 14 at 8:33















          up vote
          0
          down vote



          accepted










          Your problem is in the string.empty all your values will be empty and this is what the dropdown uses to identify the selected item.



          Change



          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));


          to



          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), DateTime.Today.AddYears(-1).Year.ToString()));


          This will give the text and value to the dropdown. Or you can use an other unique value like an int or so. But if it is the same for multiple items it will not work like you expect.






          share|improve this answer





















          • Thanks, works fine
            – user10356084
            Nov 14 at 8:33













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Your problem is in the string.empty all your values will be empty and this is what the dropdown uses to identify the selected item.



          Change



          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));


          to



          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), DateTime.Today.AddYears(-1).Year.ToString()));


          This will give the text and value to the dropdown. Or you can use an other unique value like an int or so. But if it is the same for multiple items it will not work like you expect.






          share|improve this answer












          Your problem is in the string.empty all your values will be empty and this is what the dropdown uses to identify the selected item.



          Change



          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));


          to



          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), DateTime.Today.AddYears(-1).Year.ToString()));


          This will give the text and value to the dropdown. Or you can use an other unique value like an int or so. But if it is the same for multiple items it will not work like you expect.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 at 8:23









          SehaxX

          5561516




          5561516












          • Thanks, works fine
            – user10356084
            Nov 14 at 8:33


















          • Thanks, works fine
            – user10356084
            Nov 14 at 8:33
















          Thanks, works fine
          – user10356084
          Nov 14 at 8:33




          Thanks, works fine
          – user10356084
          Nov 14 at 8:33












          up vote
          0
          down vote













          Make this update in the Page Load.



          if (!IsPostBack)
          {
          if (ddlYearForProject.Items.Count == 0)
          {
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-3).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-2).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.Year.ToString(), string.Empty));
          }
          }





          share|improve this answer

















          • 1




            Please explain your answer
            – Nakul
            Nov 14 at 7:09










          • Forget to write it, but there is already a if(!IsPostback)
            – user10356084
            Nov 14 at 7:40















          up vote
          0
          down vote













          Make this update in the Page Load.



          if (!IsPostBack)
          {
          if (ddlYearForProject.Items.Count == 0)
          {
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-3).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-2).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.Year.ToString(), string.Empty));
          }
          }





          share|improve this answer

















          • 1




            Please explain your answer
            – Nakul
            Nov 14 at 7:09










          • Forget to write it, but there is already a if(!IsPostback)
            – user10356084
            Nov 14 at 7:40













          up vote
          0
          down vote










          up vote
          0
          down vote









          Make this update in the Page Load.



          if (!IsPostBack)
          {
          if (ddlYearForProject.Items.Count == 0)
          {
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-3).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-2).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.Year.ToString(), string.Empty));
          }
          }





          share|improve this answer












          Make this update in the Page Load.



          if (!IsPostBack)
          {
          if (ddlYearForProject.Items.Count == 0)
          {
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-3).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-2).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.AddYears(-1).Year.ToString(), string.Empty));
          ddlYearForProject.Items.Insert(0, new ListItem(DateTime.Today.Year.ToString(), string.Empty));
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 at 6:45









          Thilina Nakkawita

          651827




          651827








          • 1




            Please explain your answer
            – Nakul
            Nov 14 at 7:09










          • Forget to write it, but there is already a if(!IsPostback)
            – user10356084
            Nov 14 at 7:40














          • 1




            Please explain your answer
            – Nakul
            Nov 14 at 7:09










          • Forget to write it, but there is already a if(!IsPostback)
            – user10356084
            Nov 14 at 7:40








          1




          1




          Please explain your answer
          – Nakul
          Nov 14 at 7:09




          Please explain your answer
          – Nakul
          Nov 14 at 7:09












          Forget to write it, but there is already a if(!IsPostback)
          – user10356084
          Nov 14 at 7:40




          Forget to write it, but there is already a if(!IsPostback)
          – user10356084
          Nov 14 at 7:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293746%2fget-selected-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to send String Array data to Server using php in android

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Is anime1.com a legal site for watching anime?