1
votes

I am new to ASP.NET and C# and I'm trying to create a forum. I have a HTML select on my .aspx file and a HTML button, and inside .aspx.cs file I want to get that HTML select value when the button is pressed, but it always gives me the "default" value, not the one actually selected.

This is in my .aspx file:

<select id="sortBySelect" runat="server">
    <option value="default">Default</option>
    <option value="username">Username</option>
    <option value="date">Data</option>
</select>

<button id="sortByButton" runat="server" onserverclick="sortBy">Sort</button>

And inside my .aspx.cs file I have this:

public void sortBy(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine(sortBySelect.Value);
}

I also can't use asp dropdownlist/asp button because it says that it must stay inside a form tag with "runat="server"", but I already have a form tag on my .aspx file, and if I add another one it says that I can't have 2 form tags with "runat="server"".

How can I take the actually selected value of that HTML select?

2
use <form ...></form> - Igor Semin
Hi Alexandru. If you're new to asp.net and c#, especially if you have any other web programming experience I would strongly recommend you use the asp.net mvc framework rather than the webforms framework that you're using. Mvc has far simpler concepts that are more natural to the format of the web, and generally higher quality advice available. Webforms can be useful sometimes, but I would never use it for something like this and would never recommend it to a beginner. - George Mauer
@Igor Semin I tried to put my HTML select inside a form tag, but with method="get" my function "sortBy" is not called and with/without method="post" it changes the URL which is used by my application and thus the rendering of the page fails. - Alexandru Andrei
@George Mauer, I worked with PHP and Symfony2/Zend frameworks which are both MVC, but this is a college project where we have to use Web forms. - Alexandru Andrei
@AlexandruAndrei You should not manipulate form tag in ASP.Net Web Form. - Win

2 Answers

2
votes

In ASP.Net Web Form, you need form tag to be runat="server". In addition, you should not manipulate form tag.

ASP.Net controls uses ViewState to maintain state information between post backs. Therefore, you want to use Server Controls such as DropDownList (unless you know you do not need ViewState).

<form id="form1" runat="server">
    <asp:DropDownList ID="SortByDropDownList" 
        runat="server">
        <asp:ListItem Value="default">Default</asp:ListItem>
        <asp:ListItem Value="username">Username</asp:ListItem>
        <asp:ListItem Value="date">Data</asp:ListItem>
    </asp:DropDownList>
    <asp:Button runat="server" ID="SortByButton"
        Text="Sort"
        OnClick="SortByButton_Click" />
</form>

protected void SortByButton_Click(object sender, EventArgs e)
{
    string value = SortByDropDownList.SelectedValue;
}
0
votes

You are not getting value back as "select" is not part of the form and thus not being posted back on the submit. The button you use for sorting here should be targeting form which contains "select". Consider having only one form on your page.

EDIT: To your comment about number of forms. If you add additional form, you will need to support logic to support it, as by default ASP.Net maintains only those marked with runat="server". That includes persisting data between postbacks, not having ASP.Net server events, etc.