skip to Main Content

In my GridView I set AllowSorting="true" and obviuously the view gets sorted upon the column clicked. After this event, the select button besides each row references the row displayed before the sorting event and not the actual one.

A method to reference not the index but the data-key when I click the selcet button?

2

Answers


  1. Hum, sorting and a plain good old regular asp.net button dropped into the gridview should work just fine.

    So, say this markup:

    <asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" CssClass="table" AllowSorting="True"
        OnSorting="GHotels_Sorting"
        >
        <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName"   SortExpression="LastName" />
            <asp:BoundField DataField="HotelName" HeaderText="HotelName" SortExpression="HotelName"  />
            <asp:BoundField DataField="Description" HeaderText="Description"  />
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    <asp:Button ID="cmdEdit" runat="server" 
                        Text="Edit" CssClass="btn" OnClick="cmdEdit_Click" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    So, code to load:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
            LoadGrid
        End If
    
    End Sub
    
    Sub LoadGrid()
    
        Dim cmdSQL As New _
            SqlCommand("SELECT * FROM tblHotelsA ORDER BY HotelName")
        GHotels.DataSource = MyrstP(cmdSQL)
        GHotels.DataBind()
    
    End Sub
    

    And the sort event:

    Protected Sub GHotels_Sorting(sender As Object, e As GridViewSortEventArgs)
    
        Dim cmdSQL As New _
            SqlCommand($"SELECT * FROM tblHotelsA ORDER BY {e.SortExpression}")
        GHotels.DataSource = MyrstP(cmdSQL)
        GHotels.DataBind()
    
    End Sub
    

    Ok, so that works just fine.

    So, now the above edit button with a simple regular button, and a simple regular button click.

    When I click on that button, I have this code:

    Protected Sub cmdEdit_Click(sender As Object, e As EventArgs)
    
        Dim btn As Button = sender
        Dim gRow As GridViewRow = btn.NamingContainer
        Dim pkID = GHotels.DataKeys(gRow.RowIndex).Item("ID")
    
        Debug.Print($"Row index click is = {gRow.RowIndex}")
        Debug.Print($"Database PK id is = {pkID}")
        MyEditHotelC.MyPk = pkID
        MyEditHotelC.LoadData()
    
        ' lets call the js routine to pop our hidden edit div
        Dim strJava As String = "PopEdit('" & btn.ClientID & "')"
        ClientScript.RegisterStartupScript(Page.GetType(), "PopEditKey", strJava, True)
    
    End Sub
    

    The above correctly outputs the row index, and also correctly outputs the database PK id (hidden in datakeys).

    So, I get/see this:

    enter image description here

    And the debug.prints show this:

    Row index click is = 1
    Database PK id is = 5
    

    So, the simple plain standard button click shows the correct row index, and the PK value via datakeys. Note the use of "namingContainer". This picks up the row you clicked on, and thus lets you dispense with all of the gridview row event mumbo jumbo – which you don’t need nor want to bother with!!!

    Login or Signup to reply.
  2. So, the simple plain standard button click shows the correct row index, and the PK value via data keys. Note the use of "naming Container". This picks up the row you clicked on, and thus lets you dispense with all of the grid view row event mumbo jumbo – which you don’t need nor want to bother with!!!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search