Asp.net链接onRowCommand无法正常工作

时间:2014-02-06 14:23:16

标签: c# asp.net gridview webforms

我在下面给出的GridView ID,(来自应用程序的Asp.net web)

asp:GridView ID="grid"  runat="server" AutoGenerateColumns="false" Width="913px" ViewStateMode="Enabled" 
          ShowHeaderWhenEmpty="true" ShowHeader="true" onrowcommand="ContactsGridView_RowCommand" >

onRowCommand的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FirstWebForm.business_objects;

namespace FirstWebForm
{
    public partial class About : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grid.DataSource = dataaccess.Instance.getAll();
                grid.DataBind();
            }
        }

        void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
        }

    }
}

但它遇到了错误 enter image description here

Compiler Error Message: CS1061: 'ASP.about_aspx' does not contain a definition for 'ContactsGridView_RowCommand' and no extension method 'ContactsGridView_RowCommand' accepting a first argument of type 'ASP.about_aspx' could be found (are you missing a using directive or an assembly reference?)

1 个答案:

答案 0 :(得分:1)

如果要在后面的代码中运行,请将ContactsGridView_RowCommand方法更改为publicprotected

protected void ContactsGridView_RowCommand

或者将该方法放在脚本标记的页面上,它将按原样运行

<script runat="server">

  void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
   ...
  }
</script>