English 中文(简体)
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 help 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>
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签