如何从.aspx代码中传递查询中的会话值?

时间:2013-02-04 09:59:13

标签: asp.net sql linq

我想从.aspx代码传递会话值,即网络的源代码

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT Album.AlbumID, Album.DefaultPhotID, Album.AlbumName, PhotAlbum.Photo FROM Album INNER JOIN PhotAlbum ON Album.DefaultPhotID = PhotAlbum.PhotoID where userid=<%  Session["UserId"] %>">
    </asp:SqlDataSource>

我怀疑这段代码是否有效。

1 个答案:

答案 0 :(得分:1)

使用SessionParameterSelectParameters一起传递Session

<强> MSDN Doc

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT Album.AlbumID, Album.DefaultPhotID, Album.AlbumName, PhotAlbum.Photo FROM Album INNER JOIN PhotAlbum ON Album.DefaultPhotID = PhotAlbum.PhotoID where userid=@userid">
<SelectParameters>
         <asp:SessionParameter Name="userid" Type="String"  SessionField="UserId" />
</SelectParameters>
   </asp:SqlDataSource>