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.
c# html asp.net .net
add a comment |
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.
c# html asp.net .net
add a comment |
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.
c# html asp.net .net
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
c# html asp.net .net
edited Nov 14 at 7:39
asked Nov 14 at 5:32
user10356084
77
77
add a comment |
add a comment |
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.
Thanks, works fine
– user10356084
Nov 14 at 8:33
add a comment |
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));
}
}
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
add a comment |
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.
Thanks, works fine
– user10356084
Nov 14 at 8:33
add a comment |
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.
Thanks, works fine
– user10356084
Nov 14 at 8:33
add a comment |
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.
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.
answered Nov 14 at 8:23
SehaxX
5561516
5561516
Thanks, works fine
– user10356084
Nov 14 at 8:33
add a comment |
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
add a comment |
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));
}
}
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
add a comment |
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));
}
}
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
add a comment |
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));
}
}
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));
}
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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