1

我有一个gridview,在每一行内我都有一个带有commandName“Activate”的按钮。我还将 onclick 行为添加到 gridview 行以选择行并显示带有一些信息的表单。问题是:当我单击按钮而不是激活命令时,选择命令将触发。

我是这样做的:

                        <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button CommandName="Activate" ID="btnActivate" runat="server" Text="Activate" CausesValidation="false"></asp:Button>
                        </ItemTemplate>
                        <ItemStyle Wrap="false" />
                        </asp:TemplateField>

然后我在这里管理命令:

 protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            switch (e.CommandName.ToUpper())
            {
                case "SELECT":
                    GridViewRow row1=(GridViewRow)grvCustomerCredit.Rows[Convert.ToInt32(e.CommandArgument)];
                    selectedRowIndex = row1.RowIndex;
                    txbCC_CITY.Text = row1.Cells[4].Text;
                    txbCC_PROVINCE.Text = row1.Cells[5].Text;
                    row1.ForeColor = System.Drawing.Color.White;
                    break;
                case "ACTIVATE":
                    GridViewRow row2 = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                    creditID = Convert.ToInt32(gridview.DataKeys[row2.RowIndex].Value);
                    {do the Activate Action}
                    break;
            }
     }

这就是我在每一行上添加 onclick 的方式:

  protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        foreach (GridViewRow row in gridview.Rows)
        {
            row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(gv, "Select$" + row.RowIndex.ToString(), true));
        }
        base.Render(writer);
    }

在 gridview 行上添加 onclick 激活按钮之前工作正常。如何将第一优先级设置为按钮单击而不是行单击?任何想法?

4

1 回答 1

0
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
    foreach (GridViewRow row in gridview.Rows)
    {
        row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(gv, "Select$" + row.RowIndex.ToString(), true));
    }
    base.Render(writer);
}

在此功能中,您提到了“选择”,因此仅使用选择功能。替换成

 row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(gv, "ACTIVATE$" + row.RowIndex.ToString(), true));
于 2012-06-21T07:23:50.887 回答