c# – GridView ButtonField后面的命令

我对GridView中ButtonField(图像类型)背后的命令有疑问.

得到了一个名为gvIngevuld的GridView,它显示了数据行和1个ButtonField行.
现在我必须将代码在这些按钮后面(每个按钮都相同)以放置一些PDF格式的东西.
问题是我不知道如何将代码在这些按钮后面?
我的代码

<asp:ButtonField ButtonType="Image" ImageUrl="~/Images/pdf.png"  CommandName="pdf_click" />

正如您所看到的,我使用了CommandName =“pdf_click”属性,但是当我单击其中一个按钮时,只有一个回发但没有任何反应.
谁可以帮助我在这里

如果需要,代码背后:

protected void pdf_click(object sender,EventArgs e)
{
    lblWelkom.Text = "Succes!";
}

谢谢.

解决方法

您应该使用gridview的RowCommand事件.将commandArgument设置为项目的唯一键. (认情况下,它传递行的索引)
void gvIngevuld_RowCommand(Object sender,GridViewCommandEventArgs e)
 {
 // If multiple buttons are used in a GridView control,use the
 // CommandName property to determine which button was clicked.
 if(e.CommandName=="pdf_click")
  {
   // Convert the row index stored in the CommandArgument
   // property to an Integer.
   int index = Convert.ToInt32(e.CommandArgument);

   // Retrieve the row that contains the button clicked 
   // by the user from the Rows collection.
   GridViewRow row = gvIngevuld.Rows[index]; 
   //gbIngevuld is your GridView's name

   // Now you have access to the gridviewrow.
  }  
}

此外,如果已设置gridview的DataKeyNames,则可以按如下方式访问它:

int index = Convert.ToInt32(e.CommandArgument);
    int ServerID = Convert.ToInt32(GridView1.DataKeys[index].Value);

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...