首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GridView ButtonField后面的命令

GridView ButtonField后面的命令
EN

Stack Overflow用户
提问于 2011-05-11 19:54:45
回答 3查看 36.8K关注 0票数 7

我有一个关于GridView中ButtonField (图像类型)后面的命令的问题。

在我显示数据行和1个ButtonField行的地方,得到一个名为gvIngevuld的GridView。现在我必须将代码放在这些按钮后面(每个按钮都是一样的),才能将一些东西放入PDF格式。问题是我不知道如何把代码放在这些按钮后面?我拥有的代码如下:

代码语言:javascript
复制
<asp:ButtonField ButtonType="Image" ImageUrl="~/Images/pdf.png"  CommandName="pdf_click" />

正如您所看到的,我使用了CommandName="pdf_click“属性,但当我单击其中一个按钮时,只有一个回发,但没有任何反应。有谁能帮我吗?

在需要的情况下,代码隐藏:

代码语言:javascript
复制
protected void pdf_click(object sender, EventArgs e)
{
    lblWelkom.Text = "Succes!";
}

谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-11 20:03:51

你应该使用gridview的RowCommand事件。将commandArgument设置为项目的唯一键。(默认情况下,它传递行的索引)

代码语言:javascript
复制
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.
  }  
}   

此外,如果您设置了网格视图的DataKeyNames,则可以按如下方式访问它:

代码语言:javascript
复制
    int index = Convert.ToInt32(e.CommandArgument);
    int ServerID = Convert.ToInt32(GridView1.DataKeys[index].Value);
票数 30
EN

Stack Overflow用户

发布于 2017-07-11 15:32:31

只是一个补充,我写的和尝试的完全一样,但是按钮仍然不工作…

...it花了一些时间,直到我意识到我忘记了一件事,那就是**.cs*文件中GridView元素中的OnRowCommand="<YourGridViewName>_RowCommand"

票数 1
EN

Stack Overflow用户

发布于 2011-05-11 20:04:01

CommandName属性未定义要调用的方法。为此,您需要创建一个绑定到控件的命令事件的方法。

恐怕我不太了解GridViews,但我认为最简单的方法是在Visual Studio的设计视图中选择它,转到属性,单击事件按钮(看起来像闪电),然后双击RowCommand事件属性。这将自动创建一个绑定到命令按钮事件的方法。

然后,您可以使用GridViewCommandEventArgs参数来访问CommandName、CommandArgument和CommandSource属性,以确定哪个按钮导致了事件的触发。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5963806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档