0

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.

4

1 回答 1

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"];
    }
}

但是还有更多的方法可以做到这一点,您基本上可以使用任何数据控件,例如列表视图、中继器、...

如果您考虑通过例如从 xml 文件或从数据库或其他任何方式加载站点文化来使其更具动态性,则应使用数据控件。这是一个带有中继器的示例:

前端:

<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
}

这应该有效。

于 2012-08-03T20:42:37.023 回答