I have a simple language switcher through a drop down list, however I would like to switch to a list. As far as I know, AutoPostBack is not supported for bulleted list but only the PostBack property. In this case, using postback leads the switcher not to function anymore. Is there a way to come over this?
<asp:DropDownList ID="cmbCulture" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="cmbCulture_SelectedIndexChanged"
CssClass="lang_switcher"
DisplayMode="LinkButton">
<asp:ListItem Value="de-DE">DE</asp:ListItem>
<asp:ListItem Value="en-US">EN</asp:ListItem>
</asp:DropDownList>
And here is the code:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HttpCookie cultureCookie = Request.Cookies["Culture"];
string cultureCode = (cultureCookie != null) ? cultureCookie.Value : null;
if (!string.IsNullOrEmpty(cultureCode))
{
cmbCulture.SelectedValue = cultureCode;
}
}
}
protected void cmbCulture_SelectedIndexChanged(object sender, EventArgs e)
{
//Save Current Culture in Cookie- will be used in InitializeCulture in BasePage
Response.Cookies.Add(new HttpCookie("Culture", cmbCulture.SelectedValue));
Response.Redirect(Request.Url.AbsolutePath); //Reload and Clear PostBack Data
}
}
So I would like to achieve something like:
front-end:
<asp:BulletedList ID="cmbCulture" runat="server" PostBack="True" OnSelectedIndexChanged="cmbCulture_SelectedIndexChanged" DisplayMode="LinkButton">
<asp:ListItem Value="en-US">EN</asp:ListItem>
<asp:ListItem Value="de-DE">DE</asp:ListItem>
</asp:BulletedList>
back-end:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HttpCookie cultureCookie = Request.Cookies["Culture"];
string cultureCode = (cultureCookie != null) ? cultureCookie.Value : null;
if (!string.IsNullOrEmpty(cultureCode))
{
cmbCulture.SelectedItem.Value = cultureCode;
}
}
}
protected void cmbCulture_SelectedIndexChanged(object sender, EventArgs e)
{
//Save Current Culture in Cookie- will be used in InitializeCulture in BasePage
Response.Cookies.Add(new HttpCookie("Culture", cmbCulture.SelectedItem.Value));
Response.Redirect(Request.Url.AbsolutePath); //Reload and Clear PostBack Data
}
}
SelectedItem.Value drops an error of "Object reference not set to an instance of an object." Is there a way to get the value of the clicked listitem in my bulletedlist? This may be the solution.