Hi,
I have a user control which contains the following code:
<form id="CurrencyForm" method="post" runat="server">
Display prices in
<asp:DropDownList ID="currency" runat="server" AutoPostBack="true">
<asp:ListItem Value="GBP">GBP (£)</asp:ListItem>
<asp:ListItem Value="USD">USD ($)</asp:ListItem>
<asp:ListItem Value="EUR">EUR (€)</asp:ListItem>
</asp:DropDownList>
</form>
This user control is included in my Master page so it is available on every page of the site.
Here is the codebehind:
protected void page_load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
currency.SelectedValue = (string)Session["currency"];
}
else
{
session["currency"] = currency.SelectedValue;
Response.Redirect(Request.RawUrl);
}
}
When I change the selection of the drop down list, a postback is triggered. However, the value of IsPostBack
seems to always be false. I can't quite put my finger on what I am doing wrong here?
Solution
After looking at the advice from the majority of posts I understand I was going wrong by using code_behind code etc instead of the controllers. I figured out how to use an action to basically redirect back to the same page so the following is now what I have:
User control:
<% using (Html.BeginForm("ChangeCurrency", "Home", FormMethod.Post)) {%>
Display prices in
<select id="currency" name="currency">
<option value="GBP">GBP (£)</option>
<option value="USD">USD ($)</option>
<option value="GBP">EUR (€)</option>
</select>
<input type="submit" value="change" /> // this is temporary to test the submit
<%} %>
Action
public ActionResult ChangeCurrency(string currency)
{
Session["currency"] = currency;
return new RedirectResult(Request.UrlReferrer.AbsoluteUri);
}