代码之家  ›  专栏  ›  技术社区  ›  p.campbell

ASP.NET WebForms:对OnSelectedIndexChanged事件使用jQuery设置cookie

  •  2
  • p.campbell  · 技术社区  · 15 年前

    考虑以下情况:

    • ASP.NET Web窗体页具有DropDownList和ListView。
    • 此DropDownList必须在其上设置一个cookie值 OnSelectedIndexChanged 事件。
    • DropDownList也设置为 AutoPostBack="True" 使用DropDownList的新值强制重新加载ListView。
    • 该页引用当前的jquery库,包括 great set of cookie plugins

    此代码将用于分配cookie的值:

    var date = new Date();
    date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
    $.cookie('cookieName', 'theValue', { path: '/', expires: date });
    

    问题 :您能否建议如何使用jquery将DropDownList的值提取并保存到cookie中?一个答案的超级奖励点数,包括如何调用回发方法重新绑定ListView!我很乐意为用户保存一个回邮“flash”。

    3 回复  |  直到 15 年前
        1
  •  2
  •   p.campbell    15 年前

    1)获取DropDownList的值:

    var ddlVal = $('#<%= ddList.ClientID %>').val();
    

    2)加载列表视图:

    A.为此,我建议咨询Dave在其网站上的帖子。 Encosia .

    B.里克·斯特拉尔在IS也有很好的职位 blog 关于使用jquery ajax

    C.穆罕默德·摩萨 post 用Ajax描述主细节场景

        2
  •  1
  •   Jason Berry    15 年前

    如下所示将值保存到更改事件的cookie中:

    $("#" + <%=YourDropDownList.ClientID %>).change(function(){
        var date = new Date();
        date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
        $.cookie('cookieName', $(this).val(), { path: '/', expires: date });
    });
    
        3
  •  1
  •   Raghav    15 年前
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
    
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script src="Scripts/jquery.cookie.js" type="text/javascript"></script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="TestDropDownList" runat="server">
                <asp:ListItem Text="Please Select" Value="0"></asp:ListItem>
                <asp:ListItem Text="FirstText" Value="1"></asp:ListItem>
                <asp:ListItem Text="SecondText" Value="2"></asp:ListItem>
                <asp:ListItem Text="ThirdText" Value="3"></asp:ListItem>
                <asp:ListItem Text="FourthText" Value="4"></asp:ListItem>
            </asp:DropDownList>
        </div>
        </form>
    </body>
    
    <script type="text/javascript">
        $(function()
        {
            $('#<%= TestDropDownList.ClientID %>').change(function(e)
            {
                var selectedValue = this.value;
                var date = new Date();
                date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
                $.cookie('cookieName', 'theValue', { path: '/', expires: date });
    
            });
        });     
    </script>
    </html>