简体   繁体   中英

Gridview linkbutton not firing RowCommand

I am designing a search page where if the View/Edit link button is clicked, another page loads with more info on the subject, however; when the button is clicked, it does not throw the Gridview's RowCommand event. The Gridview is built dynamically based on search terms and comparing to a database through an SQLDataSource. Here is the html for the Gridview.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RID" DataSourceID="Report_System" CellPadding="4" ForeColor="#333333" GridLines="None" OnDataBound="GridView1_DataBound" Width="739px" OnRowCommand="GridView1_RowCommand">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="RID" HeaderText="Report ID" InsertVisible="False" ReadOnly="True" SortExpression="RID" />
            <asp:BoundField DataField="Rdate" HeaderText="Report Date" SortExpression="Rdate" />
            <asp:BoundField DataField="Rtype" HeaderText="Type" SortExpression="Rtype" />
            <asp:BoundField DataField="OID" HeaderText="Officer ID" SortExpression="OID" />
            <asp:BoundField DataField="Ofirstname" HeaderText="Officer" SortExpression="Ofirstname" />
            <asp:BoundField DataField="Pname" HeaderText="Student Reportee" SortExpression="Pname" />
            <asp:ButtonField CommandName="View/Edit" Text="View/Edit" />
        </Columns>
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>

Currently, the RowCommand event is SUPPOSED to simply post back to a label for testing purposes.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //get row index
        int row = Convert.ToInt32(e.CommandArgument);

        errorLabel.Text = row.ToString();
    }

I have made sure that ViewState is Enabled, the OnRowCommand is in the right place, and my PageLoad event is empty. I can't find any forum posts to help me beyond this. Any ideas?

Try adding a check on CommandName inside GridView1_RowCommand with an if statement, like this

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      if(e.CommandName == "View/Edit")
      {
        //get row index
        int row = Convert.ToInt32(e.CommandArgument);

        errorLabel.Text = row.ToString();
      }
    }

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