我通过下拉列表有一个简单的语言切换器,但我想切换到列表。据我所知,项目符号列表不支持AutoPostBack,只支持PostBack属性。在这种情况下,使用回发导致切换器不再起作用。有办法克服这个问题吗?
<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>
以下是代码:
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
}
}
所以我想实现类似的目标:
前端:
<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>
后端:
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会丢弃“对象引用未设置为对象实例”的错误。有没有办法在我的项目符号列表中获取所点击的列表项的价值?这可能是解决方案。
答案 0 :(得分:3)
有很多方法可以做到这一点,取决于你的逻辑的其余部分,你可以在前端做到这么简单:
<ul>
<li><asp:HyperLink ID="hypDE" runat="server" Text="DE" NavigateUrl="yourpage.aspx?culture=DE" /></li>
<li><asp:HyperLink ID="hypUS" runat="server" Text="US" NavigateUrl="yourpage.aspx?culture=US" /></li>
</ul>
在yourpage.aspx的代码隐藏中,你可以通过这样的方式设置适当的文化:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string culture = Request.QueryString["culture"];
}
}
但还有很多方法可以做,你可以使用基本上任何datacontrol,例如listview,转发器,......
如果您考虑通过从xml文件或数据库或其他任何方式加载sitecultures来使其更加动态,则应使用数据控件。以下是转发器的示例:
前端:
<asp:Repeater ID="repCultures" runat="server" OnItemDataBound="repCultures_ItemDataBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<li>
<asp:HyperLink ID="hypCulture" runat="server" />
</li>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
后端:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//This is where you would get your cultures out of an xml or database
//I'm using a non-dynamic list to make a simple representation
List<string> cultures = new List<string>() { "de-DE", "en-US", "en-UK" };
repCultures.DataSource = cultures;
repCultures.DataBind();
}
}
protected void repCultures_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string culture = e.Item.DataItem as string;
HyperLink hypCulture = e.Item.FindControl("hypCulture") as HyperLink;
hypCulture.Text = culture;
hypCulture.NavigateUrl = string.Format("~/yourpage.aspx?culture={0}", culture);
}
}
然后再次在yourpage.aspx上,您将获取查询字符串以获取您喜欢的文化。
同样,有很多方法可以做到这一点,所以如果这对你不起作用,请让我对你的项目有更多的了解,我会建议一个更好的方法。
希望这有用!
编辑:
首先将事件更改为:
前端:
<asp:BulletedList ID="cmbCulture" runat="server" OnClick="cmbCulture_Click" DisplayMode="LinkButton">
后端:
protected void cmbCulture_Click(object sender, BulletedListEventArgs e)
{
//Save Current Culture in Cookie- will be used in InitializeCulture in BasePage
Response.Cookies.Add(new HttpCookie("Culture", cmbCulture.Items[e.Index].Value));
Response.Redirect(Request.Url.AbsolutePath); //Reload and Clear PostBack Data
}
这应该有效。