简体   繁体   English

将会话添加到asp.net中的URL

[英]Add Session to URL in asp.net

I am wanting to create a session in an URL and assign its value from a database. 我想在URL中创建会话并从数据库分配其值。

For example i currently have URL creating a list according to the select statement. 例如,我目前有URL根据select语句创建一个列表。

I am wanting to include Favourite_ID but assign it to a session instead. 我想包含Favourite_ID,但将其分配给会话。

So something like: Session ["Fav"] = Favourite_ID; 类似于: Session ["Fav"] = Favourite_ID;

ASP. ASP。

<asp:DataList ID="DataList1" runat="server" ShowFooter="False" ShowHeader="False" Width="460px" CellPadding="1" Height="193px">
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<ItemTemplate>
    <table class="auto-style2">
        <tr>
            <ul>
            <td class="auto-style3">
                <h6><li><asp:HyperLink Runat ="server" NavigateUrl ='<%#"RecipePage?id=" + DataBinder.Eval(Container.DataItem, "Recipe_ID").ToString()%>' ID="Hyperlink1"><%#DataBinder.Eval(Container.DataItem, "Recipe_Name")%></asp:HyperLink></asp></li></h6>
            </td>
            </ul>
        </tr>
    </table>
</ItemTemplate>

C# C#

private void loadRecipe()
        {

            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
            con.Open();
            try
            {
                //Fetching top recipe     
                string query = ("SELECT * FROM Recipe LEFT JOIN Favourite on Recipe.Recipe_ID = Favourite.Recipe_ID WHERE Favourite.Student_ID = '"+UserID.Text+"' ORDER BY Recipe_Name");
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DataList1.DataSource = ds;
                DataList1.DataBind();
            }
            catch (Exception)
            {

                //catch exception here

            }

            con.Close();
        }

Also im just testing what i can do at the moment. 我也只是测试我现在能做什么。 So excuse the bad practise of not using a parameter and using Try-catch. 因此,请原谅不使用参数而不使用Try-catch的错误做法。

This will change once i understand whether i can use a session the way im hoping to. 一旦我了解我是否可以以我希望的方式使用会话,这种情况就会改变。

I have used changed my current code to allow the use of a CommandArgument. 我已经更改了当前代码,以允许使用CommandArgument。 In which calls and creates a session. 在其中调用并创建一个会话。

<asp:DataList ID="DataList1" runat="server" ShowFooter="False" ShowHeader="False" Width="460px" CellPadding="1" Height="193px">
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<ItemTemplate>
    <table class="auto-style2">
        <tr>
            <ul>
            <td class="auto-style3">

               <h6><li><asp:LinkButton runat="server" OnCommand="Call_Fav" CommandArgument='<%# Eval("Favourite_ID") %>' ID="bob" Text='<%#DataBinder.Eval(Container.DataItem, "Recipe_Name")%>' /></li></h6>

            </td>
            </ul>
        </tr>
    </table>
</ItemTemplate>

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

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