Reputation: 24766
I have drop down list and asp button in my page like this:
<select id="select1" >
<option value="smiley.gif">Smiley</option>
<option value="angry.gif">Angry</option>
<option value="stickman.gif">Stickman</option>
</select>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
Once the button is clicked, I need to get the selected value from the drop down list and save it in the session.
But when the list is changed, it should not reload the page (Simply I don't need to use runat="server"
).
How to do this?
Upvotes: 0
Views: 13570
Reputation: 2095
You get the value from the Request object.
Edit, thanks to comment by @Péter, add name property to the select element:
<select id="select1" name="select1">
<option value="smiley.gif">Smiley</option>
<option value="angry.gif">Angry</option>
<option value="stickman.gif">Stickman</option>
</select>
In code-behind:
var selectedOption = Request["select1"];
Though, if you are using asp.net I suggest you use the server version (runat=server) of the select and thereby could access the value directly from a Select object in code-behind.
To avoid postback you can set AutoPostback=false, the following solution would not post back when selecting a value in the drop down list. And you get the bonus of type safety etc by using an asp.net control for your drop down:
<asp:DropDownList id="select1" runat="server" AutoPostback="False">
<asp:ListItem Value="smiley.gif" Text="Smiley"/>
<asp:ListItem Value="angry.gif" Text="Angry"/>
<asp:ListItem Value="stickman.gif" Text="Stickman"/>
</asp:DropDownList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
In code-behind:
var selectedOption = select1.SelectedValue;
Upvotes: 2
Reputation: 3662
You need to set name of the html control, and then you can get it from Request Object.
<select id="select1" name="myselect" >
<option value="smiley.gif">Smiley</option>
<option value="angry.gif">Angry</option>
<option value="stickman.gif">Stickman</option>
</select>
Inside you button click event handler:
string selection = Request["myselect"];
Upvotes: 0