0

我试图在一个页面中显示超链接,当用户单击链接时,页面正在导航,但我想获取超链接的名称并在不同的网页中显示为标签。在这里我正在尝试使用 cookie,但是 cookie 只需要数据库的最后一个值,可以将所有值存储在 cookie 中吗?谢谢

 String sql1 = "select title,song_id from up_song where Song_type='Indian Pop Album'";
    adpt = new SqlDataAdapter(sql1, cn);
    ds = new DataSet();
    adpt.Fill(ds, "title1");
    var last6Uploaded1 = ds.Tables["title1"]
                              .AsEnumerable()
                              .OrderByDescending(r=> r.Field<int>("song_id"))
                              .Take(2);
    foreach (DataRow row in last6Uploaded1)
    {

        int songID = row.Field<int>("song_id");
        // you don't need the array of hyperlinks neither
        HyperLink hl = new HyperLink(); 
        hl.ID = "hyperlink" + songID;
        string title = row.Field<string>("title");
        hl.Text = title;
        hl.NavigateUrl = "Downloadpage.aspx";
        hl.ForeColor = System.Drawing.Color.White;
        HttpCookie coo = new HttpCookie("song");
        coo["sogtit"] = title;
        Response.Cookies.Add(coo);
        Panel2.Controls.Add(hl);
        Panel2.Controls.Add(new LiteralControl("<br>"));
     }
4

1 回答 1

1

Cookie 可以存储多个值,您可以这样做:

string title = row.Field<string>("title");
string song_id = row.Field<int>("song_id").ToString();

HttpCookie coo = new HttpCookie("song");
cookie.Values.Add("songtit", title);
cookie.Values.Add("song_id", song_id);

您将检索目标页面中的值,如下所示:

string title = response.Cookies["song"]["songtit"];
string song_id = response.Cookies["song"]["song_id"];

在网页之间传递数据的另一种常见方式是通过从QueryString目标页面中获取的请求参数。像这样设置您的网址:

hl.NavigateUrl = "Downloadpage.aspx?name=" + title;

Downloadpage.aspx它的Page_Load事件处理程序集中:

yourLabel.Text = Request.QueryString["name"];

whereyourLabel是您要为其设置文本的标签的名称。

您可以通过请求传递多个参数,如下所示:

hl.NavigateUrl = "Downloadpage.aspx?name=" + title + "&otherParam=" + otherParam;

在目标页面中,您可以通过 获取第二个参数的值Request.QueryString["otherParam"]

于 2013-02-16T12:39:33.363 回答