8

我在 GridView 中有一个 LinkBut​​ton(通过 TemplateField)。无论我尝试什么,LinkBut​​ton 都不会调用它的事件处理程序。我都试过了:

  1. 传统的事件处理程序(“OnClick”)
  2. GridView 级别的 OnRowCommand 事件处理程序。

在这两种情况下,我都进行了调试,它甚至没有捕获事件处理程序。

如果我在页面上将 LinkBut​​ton 移出(所以它不在 GridView 中),它工作正常,所以我知道语法是正确的。

这是“传统”方法:

<asp:TemplateField>
  <ItemTemplate>
    <asp:LinkButton Text="Cancel" ID="DeleteButton" CausesValidation="false" OnClick="CancelThis" runat="server" />
  </ItemTemplate>
<asp:TemplateField>

有趣的是,如果我从后面的代码中删除“CancelThis”方法,它会引发错误。所以我知道它知道它的事件处理程序,因为它在编译时会查找它。

这是 RowCommand 方法:

<asp:TemplateField>
  <ItemTemplate>
    <asp:LinkButton Text="Cancel" ID="DeleteButton" CausesValidation="false" CommandName="CancelThis" runat="server" />
  </ItemTemplate>
<asp:TemplateField>

在这种情况下,GridView 具有:

OnRowCommand="GridView_RowCommand"

它回发,但从未暗示要引发事件。

知道我在这里缺少什么吗?

4

3 回答 3

10

你怎么绑定你的GridView?您是否使用数据源控件?如果您在 期间手动绑定Page_Load,则由于网格每次往返都绑定,因此事件处理程序可能无法正确捕获。如果是这种情况,您可能想尝试以下方法:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        //do binding
    }
}

您可以发布示例绑定代码以与您的标记一起使用吗?

如果你真的想强制解决这个问题,你可以挂钩到 Grid 上的 RowDataBound 事件,手动找到按钮并在后面的代码中添加处理程序。就像是:

标记片段:

<asp:GridView ID="gvTest" runat="server" OnRowDataBound="gvTest_RowDataBound" />

后面的代码:

protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        //find button in this row
        LinkButton button = e.Row.FindControl("DeleteButton") as button;
        if(button != null)
        {
            button.Click += new EventHandler("DeleteButton_Click");
        }
    }
}

protected void DeleteButton_Click(object sender, EventArgs e)
{
    LinkButton button = (LinkButton)sender;
    // do as needed based on button.
}

我不确定按钮的用途是什么,但假设它是一个行删除按钮,您可能不想像在事件处理程序中那样采用这种方法,您无法直接访问有问题的行,例如你会使用这个RowCommand事件。

您使用“模板”字段是否有原因?VS说一个ButtonField?如果您使用 a ButtonField,那么您可以挂钩RowCommand事件。

标记片段:

<asp:GridView ID="gvTest" runat="server" OnRowCommand="gvTest_RowCommand">
    <columns>
        <asp:buttonfield buttontype="Link" commandname="Delete" text="Delete"/>
        ....
    </columns>
</asp:GridView>

后面的代码:

protected void gvTest_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName == "Delete")
    {
        //take action as needed on this row, for example
        int rowIndex = Convert.ToInt32(e.CommandArgument);
        GridViewRow currentRow = (sender as GridView).Rows[rowIndex];

        //do something against the row...
    }
}

您可能需要参考 MSDN 文档,了解其中一些主题:

编辑:

要回答您在 ButtonField 上的问题 - 是的,我不明白为什么您仍然无法处理按钮字段。这是在行数据绑定期间查找按钮字段并将其隐藏的片段(未经测试,但我认为可以工作......)

protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //let's assume your buttonfield is in column 1
        // (you'd know this based on your markup...)
        DataControlFieldCell cell = e.Row.Cells[1] as DataControlFieldCell;
        if(cell != null)
        {
            ButtonField field = cell.ContainingField as ButtonField;

            //based on your criteria, show or hide the button
            field.Visible = false;
            //or
            field.Visible = true;
        }
    }
}
于 2010-02-02T19:00:35.990 回答
0

是否在您的 GridView 上打开了视图状态?这让我无数次感到困惑。

于 2010-02-02T17:29:32.387 回答
-1
<button onclick="window.open('<%#Eval("ReportLinks")%>', '_blank');" title='<%#Eval("ReportLinks")%>'> Link</button>
于 2015-10-05T11:34:43.213 回答