简体   繁体   中英

How to remove selected row in a gridview from a Session in VB.NET?

Hi guys I am trying to make a webpage that has a list of items on 1 page which is displayed in a gridview that has the auto generate selected button enabled, they can click this button and it loads the item into a collection which i have stored in a session. On another page the session containing the items they selected gets displayed in a gridview.

Now I want to be able for them to click the auto generated delete button so it will remove the item they want to remove from the gridview?? this is where im having trouble and need some one to help me out.

I have a function in my index page(page that shows the available products) this is the code for it

 Public Function addToCollection() As Collection
    If Session("Order") Is Nothing Then
        colOrder = New Collection
        Session("Order") = colOrder
    Else
        colOrder = Session("Order")
    End If
    Return colOrder
End Function

I then have in the Page load my code to make the collection which is as follows:

         addToCollection()
    Dim gvRow As GridViewRow = gvCDs.SelectedRow
    Dim objOrder As Order = New Order
    objOrder.ID = gvRow.Cells(1).Text
    objOrder.Title = gvRow.Cells(2).Text
    objOrder.Artist = gvRow.Cells(3).Text
    objOrder.Price = gvRow.Cells(5).Text
    colOrder.Add(objOrder)

    Session("Order") = colOrder

then in my other page I display the session in the gridview as follows:

     Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete

    gvOrder.DataSource = Session("Order")
    gvOrder.DataBind()

End Sub

the gridview has the auto generated delete button next to each row and I want it so that when the button is clicked it removes the item from the gridview. Im not sure how to go about this but I do know I need to have it in the gvOrder_DeletedRow sub and that I need to just remove the item from the session then reload the page, Please help me solve this im not sure what code to write

You need to handle the GridView.OnRowDeleting method. Create a method in your code-behind:

Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)

    ' Handle the removal of the row here
    ' The index of the row will be in e.RowIndex property

End Sub

On your ASPX page, you need to add onRowDeleting="GridView1_RowDeleting" to the markup for the GridView control.

How you remove the row depends on what DataSource you're using (eg, if you have database as the datasource, do you want to update the database as well). Posting some of your code will help get better, more detailed answers - but this should get you pointed in the right direction.

Edited To Add You posted some code, but then removed it, so I'm posting it below to continue my answer:

Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete

    gvOrder.DataSource = Session("Order") 
    gvOrder.DataBind() 

End Sub


Protected Sub gvOrder_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvOrder.RowDeleting 

    Session.RemoveAt(gvOrder.SelectedRow.RowIndex) 
End Sub

The code in your gvOrder_RowDeleting method isn't quite what you want; depending on how many things you have in your Session object, you'll either delete whatever is at that index or get an index out of range exception, I think.

Try this:

Protected Sub gvOrder_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvOrder.RowDeleting 

   ' Session stores everything as an object, so its best to cast the object to the type it really is
    colOrder = CType(Session("Order"), colOrder)
    colOrder.RemoveAt(e.RowIndex)
    Session("Order") = colOrder
    gvOrder.DataSource = colOrder
    gvOrder.DataBind()

End Sub

Essentially, get the object out of the Session, remove the selected row, put the object back in the Session and then bind it to the GridView.

Whenever the Delete button is hit, the RowCommand Event of GridView fires, and there you can check by Command Name, like.. e.CommandName == "Delete"

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        Int32 Id = Convert.ToInt32(e.CommandArgument);// this will return the selected 
        //row Id, which help you to identify and helps you to delete particular row
        // Put your deletion code here, that delete the record from list.....     
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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