【问题标题】:Add Session to URL in asp.net在 asp.net 中将会话添加到 URL
【发布时间】:2016-05-07 15:24:49
【问题描述】:

我想在 URL 中创建一个会话并从数据库中分配它的值。

例如,我目前有根据 select 语句创建列表的 URL。

我想包含 Favourite_ID 但将其分配给会话。

类似:Session ["Fav"] = Favourite_ID;

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#

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();
        }

我也只是测试我目前能做什么。所以请原谅不使用参数和使用 Try-catch 的不良做法。

一旦我了解我是否可以按照我希望的方式使用会话,这将改变。

【问题讨论】:

  • 您是说要在生成的 URL 中包含会话中的值吗?目前尚不清楚您要在这里做什么。

标签: c# asp.net session


【解决方案1】:

我已更改我当前的代码以允许使用 CommandArgument。在其中调用并创建会话。

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2012-03-23
    • 2014-03-01
    • 2013-03-20
    • 2013-09-10
    • 1970-01-01
    • 2012-03-11
    相关资源
    最近更新 更多