<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
   <Columns>
      <asp:BoundField DataField="XM" HeaderText="姓名" />
      <asp:BoundField DataField="NL" HeaderText="年龄" />
      <asp:TemplateField HeaderText="查看">
         <ItemTemplate>
            <asp:Button ID="Button1" onclick="Button1_Click" runat="server" Text="编辑" /> 
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>这是后台代码:
protected void Page_Load(object sender, EventArgs e)
{
   bingData(0);
}
protected void bingData(int PageCount)
{
    DataTable MYDT = MyData.GetDBList("Price");
    this.GridView1.DataSource = MYDT;
}
protected void Button1_Click(object sender, EventArgs e)
{
    不知道能不这样直接在这里面写?如果不能的话应该怎样呢?
}

解决方案 »

  1.   

    定义个 CommandName="del" 属性
    在行事件里
     protected void repeaterSD_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
          
            if (e.CommandName == "del")
            {
                try
                {
                    int i = int.Parse(e.CommandArgument.ToString());
                    article.DeleteArticle(i);
                    repeaterSD.DataSource = GetArticle(0, 13, " 1=1");
                    repeaterSD.DataBind();
                }
                catch (Exception eee)
                {
                    Response.Write("<script>alert('删除失败!');</script>");  
                }
            }    }
      

  2.   

        protected void Button1_Click(object sender, EventArgs e)
        {
           直接在这里写也可以……
        }
      

  3.   

    顶1楼的,3楼地不要.....因为你不可能在每行里按钮执行完全相同的功能(总会需要一些与当前行相关的数据)....那样你的按钮就应该是在GridView外面了。
      

  4.   

    protected void Button1_Click(object sender, EventArgs e)
      {
      直接在这里写也可以
      }你也可以调用GridView的RowCommand事件。
      

  5.   

    http://blog.csdn.net/xianfajushi/archive/2008/11/30/3413317.aspx
      

  6.   

    protected void Button1_Click(object sender, EventArgs e)
      {
      Button btn= (Button)sender;
      GridViewRow gvr = btn.NamingContainer as GridViewRow;
      }  
      

  7.   

    同意1楼的,也可以用链接转到另外一个页面,然后给他传个Id也ok,如果非用按钮,同意1楼
      

  8.   

    hehe~~~~~,都讲完了,我没有说的了
      

  9.   

    可以直接用ButtonField字段类型或者TemplateField模板字段,你用的是TemplateField模板字段,那就这样写<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
      <Columns>
      <asp:BoundField DataField="XM" HeaderText="姓名" />
      <asp:BoundField DataField="NL" HeaderText="年龄" />
      <asp:TemplateField HeaderText="查看">
      <ItemTemplate>
      <asp:Button ID="Button1" CommandName="edt" runat="server" Text="编辑" />  
      </ItemTemplate>
      </asp:TemplateField>
      </Columns>
    </asp:GridView>
    protected void Page_Load(object sender, EventArgs e)
    {
      bingData(0);
    }
    protected void bingData(int PageCount)
    {
      DataTable MYDT = MyData.GetDBList("Price");
      this.GridView1.DataSource = MYDT;
      this.GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "edt")
            {
              //Your code;
            }
                }
      

  10.   

    protected void Button1_Click(object sender, EventArgs e)
    {
      //没这样写过
    }
    不过commandName=“select”时,点击按钮时貌似会触发gridview的一个什么selected事件的
      

  11.   

    <ItemTemplate>  <div style="color:Gray;text-align:center;height:20px;">  <asp:Label ID="file_name" runat="server" Text='<%# Eval("Name") %>'></asp:Label><asp:ImageButton ID="ImageButton1" runat="server" Height="17px" ImageUrl="~/del.png" onclick="ImageButton1_Click" Width="19px" /></div> </ItemTemplate>
    在ItemTemplate中加就可以了 后台用e.commandname来判断就行了
    这是哥imagebutton的例子 其他的你照葫芦画瓢就可以了
      

  12.   

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "edt")
            {
              //Your code;
            }
                }