【发布时间】: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 中包含会话中的值吗?目前尚不清楚您要在这里做什么。