简体   繁体   English

将数据列表从gridview传输到另一页

[英]Transfer list of data from gridview to another page

I have gridview look like this 我有GridView看起来像这样 在此处输入图片说明

Any time user click on Bookmark button, I wanna send the ProgramID of that row to the List and transfer it to another page by using Session.But my gridview transfer the all programID, even though that ID existed in the list. 每当用户单击“书签”按钮时,我都希望将该行的ProgramID发送到列表,并使用Session将其传输到另一页。但是,即使该ID存在于列表中,我的gridview也会传输所有programID。 What am I doing wrong? 我究竟做错了什么? This is the code for Bookmark button: 这是“书签”按钮的代码:

protected void btnSelect_Click(object sender, EventArgs e)
{


   Button b = (Button)sender;      
    GridViewRow row = (GridViewRow)b.NamingContainer;
    var ProgramID = row.FindControl("lblProgramID") as Label;
    string stringProgramID = ProgramID.Text;      
    List<string> bookmarkPrograms = (List<string>)Session["BookmarkProgram"];
    if (bookmarkPrograms == null)
        bookmarkPrograms = new List<string>();
    bookmarkPrograms.Add(stringProgramID);
    Session["BookmarkProgram"] = bookmarkPrograms;

}

And here is the code for the gridview in another page: 这是另一页中gridview的代码:

 protected void Page_Load(object sender, EventArgs e)
{
    List<string> bookMarkPrograms = (List<string>)Session["BookmarkProgram"];

    GridView1.DataSource = bookMarkPrograms;
    GridView1.DataBind();
}

Just want to post the whole solution for someone need it in the future: 只想将整个解决方案发布给将来需要的人:

 protected void btnBookmark_Click(object sender, EventArgs e)
{


    Button b = (Button)sender;      
    GridViewRow row = (GridViewRow)b.NamingContainer;
    var ProgramID = row.FindControl("lblProgramID") as Label;
    string stringProgramID = ProgramID.Text;      
    List<string> bookmarkPrograms = (List<string>)Session["BookmarkProgram"];
    if (bookmarkPrograms == null)
        bookmarkPrograms = new List<string>();


    if (bookmarkPrograms.Any(c => c.Equals(stringProgramID)))
    {
        FormMessage.Text = "You bookmarked this program already";
    }
    else
    {
        bookmarkPrograms.Add(stringProgramID);
    }
    Session["BookmarkProgram"] = bookmarkPrograms;




}

Not really sure about the flow of events. 不确定事件的流向。 But if I get you correctly, you want the list to be constantly adding. 但是,如果我正确地理解了您,则希望列表不断添加。 You are doing 你在做

 Session["BookmarkProgram"] = bookmarkPrograms;

Which means that this variable is that specific value. 这意味着该变量就是该特定值。 But then if you do it again then this happens: 但是,如果再次执行此操作,则会发生这种情况:

   List<string> bookmarkPrograms = new List<string>();

So you are erasing the old one. 因此,您正在擦除旧版本。 Make a method of getting the list so far, if is null or empty then create a new one. 制定一种获取列表的方法,如果该方法为null或为空,则创建一个新列表。 Otherwise don't or you'll empty what you have so far. 否则请不要,否则您将清空您目前拥有的东西。

Update: You can try this: 更新:您可以尝试以下操作:

List<string> bookmarkPrograms = (List<string>)Session["BookmarkProgram"] == null ? new List<string>():(List<string>)Session["BookmarkProgram"];

For checking if the value exists already then: 要检查该值是否已经存在,则:

if (bookmarkPrograms.Any(c=>c.Equals(ProgramID)){//don't add}

The inside expression returns a boolean that is true when a value inside the list equals what you are comparing. 当列表中的值等于您要比较的值时,内部表达式将返回一个布尔值。 This example asumes ProgramID is a string. 本示例假定ProgramID是一个字符串。 As they are string you might want to add trim() and tolower() but doesn't seems like in this case a blank space might accidentally be added... or that casing might affect. 由于它们是字符串,因此您可能想要添加trim()和tolower(),但在这种情况下似乎并没有意外地添加空格...或者该大小写可能会影响。 Since this returns true for when it exists, add a ! 由于存在时返回true,因此添加! in front of it so it adds to the list when it doesn't exists. 在它前面,因此它不存在时会添加到列表中。

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

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