【问题标题】:ASP.NET webforms: set cookie using jQuery on the OnSelectedIndexChanged eventASP.NET webforms:在 OnSelectedIndexChanged 事件上使用 jQuery 设置 cookie
【发布时间】:2010-11-14 05:52:47
【问题描述】:

考虑以下场景:

  • 一个 ASP.NET 网络表单页面有一个 DropDownList 和一个 ListView。
  • 此 DropDownList 必须为其 OnSelectedIndexChanged 事件设置一个 cookie 值。
  • DropDownList 也设置为 AutoPostBack="True" 以强制 ListView 使用 DropDownList 的新值重新加载。
  • 页面正在引用当前的 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 中?答案包括如何调用 post-back 方法重新绑定 ListView 的超级奖励积分!我绝对愿意为用户保存一个回发的“flash”。

【问题讨论】:

    标签: asp.net jquery cookies webforms


    【解决方案1】:

    以下内容会在更改事件时将值保存到 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 });
    });
    

    【讨论】:

      【解决方案2】:

      1) 获取 DropDownList 的值:

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

      2) 加载列表视图:

      一个。为此,我建议查阅 Dave 在他的网站 Encosia 上的帖子。

      b. Rick Strahl 在blog 上也有关于使用 jQuery ajax 的精彩帖子

      c。 Muhammad Mosa 有 post 用 ajax 描述 Master-Detail 场景

      【讨论】:

      • 感谢村。对于其他人,您能否调整代码以删除英镑和百分比之间的空格?
      【解决方案3】:
      <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>
      

      【讨论】:

      • 感谢拉加夫!不过,change 函数中的 selectedValue 没有做任何事情:)
      猜你喜欢
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多