为什么我的GridView里的LinkBut​​ton提高其OnClick事件? [英] Why won't my LinkButton inside a GridView raise its OnClick event?

查看:224
本文介绍了为什么我的GridView里的LinkBut​​ton提高其OnClick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView里面一个LinkBut​​ton(通过模板列)。不管我怎么努力,LinkBut​​ton的不会调用它的事件处理程序。我曾尝试两项:


  1. 传统的事件处理程序(onclick事件)

  2. 系统OnRowCommand事件处理在GridView的水平。

在这两种情况下,我已经调试,并没有连追事件处理程序。

如果我移动的LinkBut​​ton出来的页面上(所以它不是在GridView),它工作得很好,所以我知道的语法是正确的。

下面是传统的方法:

 < ASP:的TemplateField>
  <&ItemTemplate中GT;
    < ASP:LinkBut​​ton的文本=取消ID =DeleteButton的CausesValidation =假的OnClick =CancelThis=服务器/>
  < / ItemTemplate中>
< ASP:的TemplateField>

有趣的是,如果我从code去掉CancelThis方法的背后,它抛出一个错误。所以我知道这是知道它的事件处理程序,因为它会寻找它时,它编译。<​​/ P>

下面是RowCommand方式:

 &LT; ASP:的TemplateField&GT;
  &LT;&ItemTemplate中GT;
    &LT; ASP:LinkBut​​ton的文本=取消ID =DeleteButton的CausesValidation =假的CommandName =CancelThis=服务器/&GT;
  &LT; / ItemTemplate中&GT;
&LT; ASP:的TemplateField&GT;

在这种情况下,在GridView具有:

  OnRowCommand =GridView_RowCommand

据postsback,但从来没有的提示的提高对事件。

任何想法,我在这里丢失?


解决方案

你是如何结合你的 GridView控件?您是否使用了数据源控件?如果您在的Page_Load 手动约束力,但它可能是由于网格结合每个往返,事件处理程序不正确醒目。如果是这样的话,你可能会想尝试这样的:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!Page.IsPostBack)
    {
        //做绑定
    }
}

您可以发布样本结合code去与你的标记?

如果您真的要强制问题,你可以钩到对电网的RowDataBound事件,手动查找按钮,在code添加处理程序之后。是这样的:

标记片段:

 &LT; ASP:GridView控件ID =gvTest=服务器OnRowDataBound =gvTest_RowDataBound/&GT;

背后code:

 保护无效gvTest_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowType == DataControlRowType.DataRow)
    {
        //此行中,可以找到按钮
        LinkBut​​ton的按钮= e.Row.FindControl(DeleteButton)作为按钮;
        如果(按钮!= NULL)
        {
            button.Click + =新的EventHandler(DeleteButton_Click);
        }
    }
}保护无效DeleteButton_Click(对象发件人,EventArgs的发送)
{
    LinkBut​​ton的按钮=(LinkBut​​ton的)寄件人;
    //根据按键需要做的。
}

我不知道该按钮的目的是什么,但假定它是一个行删除按钮,你可能不希望采取这种方法,因为在事件处理程序,你不必直接访问的行问题,如你会使用 RowCommand 事件。

有没有您所使用的模板领域的原因是什么? VS说 ButtonField字段?如果使用 ButtonField字段,那么你可以挂接到 RowCommand 事件。

标记片段:

 &LT; ASP:GridView控件ID =gvTest=服务器OnRowCommand =gvTest_RowCommand&GT;
    &LT;列&GT;
        &LT; ASP:ButtonField字段按钮类型=链接的CommandName =删除文本=删除/&GT;
        ....
    &LT; /列&GT;
&LT; / ASP:GridView的&GT;

背后code:

 保护无效gvTest_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
    如果(e.CommandName ==删除)
    {
        //根据需要,该行采取行动,例如
        INT的rowIndex = Convert.ToInt32(e.CommandArgument);
        GridViewRow currentRow =(发件人为GridView控件).Rows [rowIndex位置]        //做对行的东西... ...
    }
}

您可能希望就其中一些议题协商MSDN文档:

编辑:

要回答你的问题ButtonField字段 - 是的,我不明白为什么你不能还带着ButtonField字段处理。这里有一个片断发现在绑定行数据的ButtonField字段和隐藏它(未经测试,但我认为会工作......)

 保护无效gvTest_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowType == DataControlRowType.DataRow)
    {
        //让我们假设你是ButtonField字段列1
        //(您想知道此基础上您的标记......)
        DataControlFieldCell细胞= e.Row.Cells [1]作为DataControlFieldCell;
        如果(细胞!= NULL)
        {
            ButtonField字段字段= cell.ContainingField如ButtonField字段;            //根据您的标准,显示或隐藏按钮
            field.Visible = FALSE;
            //要么
            field.Visible = TRUE;
        }
    }
}

I have a LinkButton inside a GridView (via an TemplateField). No matter what I try, the LinkButton will not invoke its event handler. I have tried both:

  1. A traditional event handler ("OnClick")
  2. A OnRowCommand event handler at the GridView level.

In both cases, I've debugged and it doesn't even catch the event handler.

If I move the LinkButton out on the page (so it's not in the GridView), it works fine, so I know the syntax is right.

Here is the "traditional" method:

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

What's interesting is if I remove the "CancelThis" method from the code behind, it throws an error. So I know it's aware of its event handler, because it looks for it when it compiles.

Here is the RowCommand method:

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

In this case, the GridView has:

OnRowCommand="GridView_RowCommand"

It postsback, but never hints at raising the event.

Any idea what I'm missing here?

解决方案

How are you binding your GridView? Are you using a datasource control? If you are binding manually during Page_Load, it's possible that since the grid is binding every round trip, the event handler isn't catching properly. If this is the case, you may want to try something like:

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

Can you post sample binding code to go with your markup?

If you really want to force the issue, you could hook into the RowDataBound event on the Grid, find the button manually and add the handler in the code behind. Something like:

markup snippet:

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

code behind:

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.
}

I'm not sure what the purpose of the button is, but assuming it is a row delete button, you may not want to take this approach as in the event handler, you don't have direct access to the row in question, like you would using the RowCommand event.

Is there a reason you're using the Template field? Vs say a ButtonField? If you use a ButtonField, then you can hook into the RowCommand event.

markup snippet:

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

code behind:

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...
    }
}

You might want to consult MSDN docs on some of these topics:

EDIT:

To answer your question on the ButtonField - yes I don't see why you couldn't still deal with a buttonfield. Here's a snippet to find the buttonfield during row data bound and hide it (untested but I think would work...)

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;
        }
    }
}

这篇关于为什么我的GridView里的LinkBut​​ton提高其OnClick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆