开发者

how to set(get) cookie value in ext.net

scene: when I click item in ext:ComboBox and want to set the item selected value to cookie variable. Finally, after I click ext:Button, the ext:Label get cookie value and display it.

But I get a error :Ext.Ajax Communication Failure , any he开发者_开发问答lp will be appreciated.

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);


    }


I tested your code and it appears to work correctly. No Exceptions were thrown and the Cookie .Value is persisted properly.

Here's a sample demonstrating the full scenario including a second Button for fetching the Cookie .Value on a future request.

Example

<%@ 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜