简体   繁体   English

在ASP.Net Gridview中使用会话变量

[英]Using A Session Variable In ASP.Net Gridview

I have a session variable which holds the current logged in user's details. 我有一个会话变量,其中包含当前登录用户的详细信息。 I'd like to run a query based on the variable and display it in a Grid view . 我想基于该变量运行查询并将其显示在Grid视图中 I want to replace the default value of the grid view with the session variable but that didn't work. 我想用会话变量替换网格视图的默认值,但这没有用。

Can anyone point me in the right direction? 谁能指出我正确的方向?

This is what I tried: 这是我尝试的:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:..... %>" 

    SelectCommand="SELECT [Username], [Password], [CustomerName], [CustomerAddress], [Contact] FROM [Customers] WHERE ([Username] = @Username)">
    <SelectParameters>
        <asp:Parameter DefaultValue="session[new]" Name="Username" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

You can supply the Session variable value just before the Query is executed by SqlDataSource using the Selecting event of the datasource. 您可以使用数据源的Selecting事件在SqlDataSource执行查询之前提供Session变量值。 Set this event in Markup using OnSelecting property. 使用OnSelecting属性在标记中设置此事件。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:..... %>" 
    OnSelecting="SqlDataSource1_Selecting"
    SelectCommand="SELECT [Username], [Password], [CustomerName], [CustomerAddress], [Contact] FROM [Customers] WHERE ([Username] = @Username)">
    <SelectParameters>
        <asp:Parameter Name="Username" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

In the Selecting Event Handler, you do as below: 在选择事件处理程序中,您可以执行以下操作:

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
    { 
          if(Session["UserName"]!=null)
           {
           // do this only when Session Variable stores as a string the Username
           e.Command.Parameters["@Username"].Value = Session["UserName"];
           }
         else
           {
              // assign the default value if session variable is Null
              e.Command.Parameters["@Username"].Value ="DefaultValue";
            }

    }

In this event handler, you can access the command that's about to be executed through the SqlDataSourceSelectingEventArgs.Command property, and modify its parameter values programmatically. 在此事件处理程序中,您可以通过SqlDataSourceSelectingEventArgs.Command属性访问将要执行的命令,并以编程方式修改其参数值。

What I ended up doing was kind of similar to FlopScientist's answer. 我最终所做的事情与FlopScientist的回答有点类似。 I found that the DataSource actually has an option for selecting a session variable which I did in the design view and it has the code below: 我发现DataSource实际上有一个选择会话变量的选项,这是我在设计视图中所做的,它具有以下代码:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:..... %>" 

SelectCommand="SELECT [Password], [CustomerName], [CustomerAddress], [Contact], [Username] FROM [Customers] WHERE ([Username] = @Username)">



<SelectParameters>
            <asp:SessionParameter Name="Username" SessionField="mySessionVariable" Type="String" />


</SelectParameters>
</asp:SqlDataSource>

Session object in ASP.NET is one of the server side state maintenance variable. ASP.NET Session对象是服务器端状态维护变量之一。

More you can find here . 你可以在这里找到更多。

Session variable simply stores any value that you assign it to. 会话变量仅存储您为其分配的任何值。 And in simple words, you can access this variable on any other page, even if you created it on some other page. 简而言之,即使您在其他页面上创建了该变量,也可以在任何其他页面上访问此变量。

so suppose on page1.aspx you did: 因此,假设您在page1.aspxpage1.aspx了以下操作:

  Session["uniqueName"] = GetGridViewData(); //you can store anything.

where signature of GetGridViewData() is suppose like: 其中GetGridViewData()签名假定为:

public DataTable GetGridViewData();

then on page2.aspx (or any other page), you can do this: 然后在page2.aspx (或任何其他页面)上,您可以执行以下操作:

protected void Page_Load(object sender, EventArgs e)
{
   if(Session["uniqueName"]!=null)
   {
      //typecast it to your datastructure or whatever you assigned into it
      DataTable gridDataSource = (DataTable)Session["uniqueName"];

      //any custom logic you want to perform on gridDataSource
      gridView1.DataSource = gridDataSource;
      gridView.DataBind();
   }
}

Just make sure, you access the variable only after you have assigned/created earlier in the flow. 只需确保,您仅在流程的早期分配/创建后才能访问变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM