2016-05-06 5 views
0

URLにセッションを作成し、その値をデータベースから割り当てたいと思っています。asp.netの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を使うという悪い習慣を許してください。

これは、私が希望する方法でセッションを使用できるかどうかを理解すると変更されます。

+1

あなたは、生成されたURL内のセッションから値を含めることを言っていますか?ここで何をしようとしているのかは不明です。 – DVK

答えて

0

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> 

関連する問題