如何在ext.net中设置(获取)cookie值

时间:2011-03-02 07:21:24

标签: asp.net ext.net

场景:当我点击ext:ComboBox中的项目并想要将项目选择值设置为cookie变量时。最后,在我点击ext:Button后,ext:Label获取cookie值并显示它。

但是我收到一个错误:Ext.Ajax通信失败,任何帮助都将不胜感激。

ASPX:

 <ext:ComboBox ID="ComboBox1" runat="server" StoreID="Store1" Width="100" Editable="false"
                                DisplayField="name" ValueField="value" Mode="Local" TriggerAction="All`enter code here`" EmptyText="Select a locale...">

.....        

aspx.cs

protected  void lngIndexChanged(object sender, DirectEventArgs e)
    {
        //Sets the cookie that is to be used by Global.asax
        HttpCookie cookie = new HttpCookie("CultureInfo");
        cookie.Value = ComboBox1.SelectedItem.Value ;
        Response.Cookies.Add(cookie);

        Label1.Text = cookie.Value;
        //Set the culture and reload for immediate effect. 
        //Future effects are handled by Global.asax
        Thread.CurrentThread.CurrentCulture = new CultureInfo(ComboBox1.SelectedItem.Value);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(ComboBox1.SelectedItem.Value);


    }

1 个答案:

答案 0 :(得分:0)

我测试了您的代码,它似乎正常工作。没有抛出异常,Cookie .Value被正确保留。

以下是演示完整方案的示例,其中包括第二个用于在将来的请求中获取Cookie .Value的按钮。

示例

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>

<%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %>

<script runat="server">
    protected void ComboBox1_Select(object sender, DirectEventArgs e)
    {
        // Sets the cookie that is to be used by Global.asax
        HttpCookie cookie = new HttpCookie("CultureInfo");
        cookie.Value = this.ComboBox1.SelectedItem.Value;
        this.Response.Cookies.Add(cookie);

        this.Label1.Text = "Cookie Value: " + cookie.Value;

        // Set the culture and reload for immediate effect. 
        // Future effects are handled by Global.asax
        Thread.CurrentThread.CurrentCulture = 
            new CultureInfo(this.ComboBox1.SelectedItem.Value);

        Thread.CurrentThread.CurrentUICulture = 
            new CultureInfo(this.ComboBox1.SelectedItem.Value);
    }

    protected void Button1_Click(object sender, DirectEventArgs e)
    {
        this.Label1.Text = "Cookie Value Again : " + this.Request.Cookies["CultureInfo"].Value;
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Ext.NET Example</title>  
</head>
<body>
    <form runat="server">
        <ext:ResourceManager runat="server" />

         <ext:ComboBox 
            ID="ComboBox1" 
            runat="server" 
            OnDirectSelect="ComboBox1_Select"
            EmptyText="Select a locale...">
            <Items>
                <ext:ListItem Text="Australia" Value="en-AU" />
                <ext:ListItem Text="Brasil" Value="br-BR" />
                <ext:ListItem Text="Canada" Value="en-CA" />
                <ext:ListItem Text="Denmark" Value="da-DK" />
            </Items>
        </ext:ComboBox>

        <br />

        <ext:Button runat="server" Text="Get Cookie Value" OnDirectClick="Button1_Click" />

        <br />
        <ext:Label ID="Label1" runat="server" />

    </form>
</body>
</html>