【问题标题】:Pass a gridview column value to a session using javascript使用 javascript 将 gridview 列值传递给会话
【发布时间】:2012-04-28 18:52:46
【问题描述】:

我有一个包含产品列表的网格视图。我想要做的是使用 ItemTemplate 是使用 Onclick 事件将所选项目的 ProductID 传递给会话,这样我就可以在另一个页面上查找该会话以避免在 URL 中显示 ProductID。

    <asp:GridView ID="GVProducts" runat="server" 
        onselectedindexchanged="GVProducts_SelectedIndexChanged">
     <Columns>
                <asp:ImageField DataImageUrlField="FileName"  
                    DataImageUrlFormatString="Images/{0}" HeaderText="Image">
                    <ControlStyle Height="80px" Width="80px" />
            </asp:ImageField>
              <asp:TemplateField HeaderText="Title" SortExpression="ProductID" > 
<ItemTemplate> 
        <a onclick="javascript:function setSessionVariable(<%#Eval("ProductID")%>)"  href="ProductDetail.aspx"><%#Eval("Title")%></a> 
</ItemTemplate></asp:TemplateField> 

        </Columns>
              <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
              <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
              <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
              <RowStyle BackColor="White" ForeColor="#003399" />
              <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
              <SortedAscendingCellStyle BackColor="#EDF6F6" />
              <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
              <SortedDescendingCellStyle BackColor="#D6DFDF" />
              <SortedDescendingHeaderStyle BackColor="#002876" />
    </asp:GridView>

希望这很简单,因为我刚开始使用 javascript,但我似乎无法将其传递给会话。

谢谢

【问题讨论】:

    标签: javascript asp.net session gridview


    【解决方案1】:

    如果您的项目中有 jQuery 库,您可以使用 jQuery ajax 将数据发送到服务器页面。

    function setSessionVariable(selectedval)
    {
     $.post("yourserverpage.aspx", { productId : selectedval },function(data){
        alert("set to session!");
     });    
    }
    

    并创建一个名为 youserverpage.aspx 的 aspx 页面,并在页面加载中读取查询字符串值并设置为会话。

    【讨论】:

      【解决方案2】:

      从您的 javascript(使用 jQuery)调用网络方法并在网络方法上设置会话:

      public partial class _Default : Page 
      {
        [WebMethod]
        public static void SetVar(string myVar)
        {
          Session["var"] = myVar;
        }
      }
      

      jQuery:

      $.ajax({
        type: "POST",
        url: "Default.aspx/SetVar",
        data: "{'myVar':'SessionVarValueSetOnJavascript'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
          // Do something interesting here.
        }
      });
      

      See this page for a nice example

      【讨论】:

        【解决方案3】:

        设置会话应该在服务器端完成。试试这个

        标记

        <asp:TemplateField HeaderText="Title" SortExpression="ProductID" > 
            <ItemTemplate> 
                <asp:LinkButton ID="MyLink" runat="server" 
                          CommandName="SendID" 
                          CommandArgument='<%#Eval("ProductID")%>'>
                    <%#Eval("Title")%>
                </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        

        代码隐藏

           protected void GVProducts_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                // If multiple buttons are used in a GridView control, use the 
                // CommandName property to determine which button was clicked. 
                if (e.CommandName == "SendID")
                {
                    // Get the ProductID stored in the CommandArgument 
                    // property to an String. 
                    string clickedProductID = e.CommandArgument.ToString();
        
                    // Set that ProductID to Session        
                    Session["ProductID"] = clickedProductID;
        
                    // Redirect to the newpage. 
                    Response.Redirect("ProductDetails.aspx");
                }
        }
        

        请不要忘记将OnRowCommand="GVProducts_RowCommand" 添加到 GridView 标记中。


        更新:
          <asp:GridView ID="GVProducts" runat="server" 
            OnRowCommand="GVProducts_RowCommand">
        

        【讨论】:

        • 感谢 Naveen,我已经尝试过了,但是行命令没有被执行。
        猜你喜欢
        • 1970-01-01
        • 2012-01-07
        • 1970-01-01
        • 1970-01-01
        • 2010-12-11
        • 2023-04-09
        • 2023-03-29
        • 2017-10-26
        • 2011-06-12
        相关资源
        最近更新 更多