c# - EventArgs VS GridViewRowEventArgs

标签 c# asp.net gridview

我正在尝试从我的 asp.net gridview 访问按钮单击事件。如果我这样保留我的语法

protected void Remove(Object sender, GridViewRowEventArgs e)
{
}

他们没有编译错误,但我收到调试错误。的

No overload for '' matches delegate 'System.EventHandler'

好吧,如果我把我的语法改成这样

protected void Remove(Object sender, EventArgs e)
{
  var command = ((Button)sender).CommandArgument;            
  if (command.CommandName == "Remove")
  {
    DataGridItem gr = (DataGridItem)command.NamingContainer;
    string abcd = gr.Cells[0].Text;
  }
}

我得到多个编译错误:

'string' does not contain a definition for 'CommandName' and no extension method 'CommandName' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

'string' does not contain a definition for 'NamingContainer' and no extension method 'NamingContainer' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

我想做的是从我的 GridView 中的按钮按下事件访问值并运行存储过程。

这是 aspx 标记

<asp:GridView ID="gvwEditDashboard" runat="server"  AutoGenerateColumns="False" ShowFooter="true" CssClass="DataGrids"
Width="500px" HorizontalAlign="Center" GridLines="Both" >
<Columns> 
<ItemTemplate>
  <asp:LinkButton ID = "btnDelete" runat = "server" CssClass="ButtonLink"  Text = "[Delete]" OnClick = "Remove" />
</ItemTemplate>
</Columns>
</asp:GridView>

最佳答案

首先,了解GridViewRowEventArgs适用于事件:RowCreatedRowDataBound .这些事件不是按钮点击事件。

其次,单击 LinkBut​​ton 时,可能会引发两个事件:LinkButton.ClickGridView.RowCommand .

我的建议是使用/处理 RowCommand GridView 的事件。

有很多技术可以获取在此事件中单击按钮所在行的数据。

根据您的具体情况,将 LinkBut​​ton 声明为:

<ItemTemplate>
  <asp:LinkButton ID = "btnDelete" runat = "server"
       CssClass="ButtonLink"  Text = "[Delete]" OnClick = "Remove" 
       CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>

注意 LinkBut​​ton 的 CommandArgument 属性。

还有 RowCommand 事件。请注意,第二个参数是 GridViewCommandEventArgs

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
        // The RowIndex of the Row where LinkButton was clicked
        int rowIndex = Convert.ToInt32(e.CommandArgument);

        // Get the GridView Row
        GridViewRow row = GridView1.Rows[rowIndex];

        // Read the Column values
        // e.g. for BoundFields use row.Cells[] 
        string Column_1_Value = row.Cells[0].Text;

        // for Columns defined using <itemTemplate> etc...
        // row.FindControl("<Control_ID>")
        string firstName = (row.FindControl("txtFirstName") as TextBox).Text;
}

替代 LinkBut​​ton,您可以使用 ButtonFields以及。检查此链接中的行命令事件。 MSDN 示例很好地解释了读取行数据。

关于c# - EventArgs VS GridViewRowEventArgs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32925985/

相关文章:

asp.net - 如何从 asp.net 中的列表中填充 gridview?

c# - IList 和 IList<object> 之间的比较 - 应该首选哪个?

c# - 从 C# 调用 F# 函数

c# - MailKit 保存附件

c# - Entity Framework 错误 : AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager

c# - ASP.NET Entity Framework NotSupportedException

c# - 如何获取某个 GridView 列中的所有值?

c# - 如何在异常情况下正确使用带有 WCF 服务的 IParameterInspector 来记录 Web 请求?

asp.net - 为什么这违反了类型约束?

android - 将图片从一项 Activity 传递到另一项 Activity