Gridview linkbutton没有触发RowCommand

时间:2015-03-24 19:54:22

标签: c# asp.net gridview

我正在设计一个搜索页面,如果单击“查看/编辑”链接按钮,则另一个页面会加载有关该主题的更多信息;单击该按钮时,它不会抛出Gridview的RowCommand事件。 Gridview是基于搜索项动态构建的,并通过SQLDataSource与数据库进行比较。这是Gridview的html。

<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>

目前,支持RowCommand事件只是为了测试目的而回发到标签。

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

        errorLabel.Text = row.ToString();
    }

我确保ViewState已启用,OnRowCommand位于正确的位置,而我的PageLoad事件为空。除了这个,我找不到任何论坛帖子来帮助我。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用CommandName语句添加对GridView1_RowCommandif的检查,如下所示

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();
      }
    }