ASP.NET LinkBut​​ton的不是调用'按需'事件 [英] ASP.NET LinkButton not invoking 'OnCommand' event

查看:122
本文介绍了ASP.NET LinkBut​​ton的不是调用'按需'事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有嵌套的GridView的列中一个ListView,它看起来是这样的:

I have a ListView nested inside the column of a GridView, which looks something like this:

<asp:panel id="myPanel" Runat="server" EnableViewState="False">
    <asp:GridView id="myDescription" AutoGenerateColumns="False" Width="100%" runat="server"
         OnRowCommand="my_RowCommand"  OnRowDataBound="GetDataForListView" EnableViewState="False">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="ID">
                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="data1" HeaderText="Thing 1">
                <HeaderStyle   HorizontalAlign="Center"></HeaderStyle>
                <ItemStyle   HorizontalAlign="Center"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="data2" HeaderText="Thing2">
                <HeaderStyle   HorizontalAlign="Center"></HeaderStyle>
                <ItemStyle   HorizontalAlign="Center"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="data3" HeaderText="Thing3">
                <HeaderStyle   HorizontalAlign="Center"></HeaderStyle>
                <ItemStyle   HorizontalAlign="Center"></ItemStyle>
            </asp:BoundField>
            <asp:TemplateField SortExpression="id">
                <ItemTemplate> 
                    <asp:HyperLink id="hlView"  runat="server"  NavigateUrl="...">View</asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Column to contain my list of things">
                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
                <ItemTemplate>
                    <% /* This is my list view: */ %>
                    <asp:ListView ID="myListView" runat="server" OnItemCommand="DoSomething" EnableViewState="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="lbAttachment" Runat="Server" Text='<%# Eval("FILE_NAME") %>'
                                CommandName='<%# Eval("ROW_NUM") %>' CausesValidation="False" > 
                            </asp:LinkButton><br/>
                        </ItemTemplate>
                    </asp:ListView>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</asp:panel>

的数据是成功的结合,但是按需事件不被上点击的LinkBut​​ton烧制(也不是用于LinkBut​​ton的任何其他事件,如onClick事件)。

The data is successfully binding, but the OnCommand event is not being fired on clicking the LinkButton (nor is any other event for the LinkButton, such as an OnClick event).

有关链接按钮呈现的HTML显示,上点击它,页面执行回发:的javascript:__ doPostBack('...','')

The rendered HTML for the link button shows that on clicking it, the page is performing a postback: javascript:__doPostBack('...','')

这意味着它会回到我的'的Page_Load和刷新页面的内容 - 网格视图在这里绑定。

This means it is going back into my 'Page_Load' and refreshing the contents of the page - the grid view is binded here.

我可以通过添加此属性LinkBut​​ton的无法执行回发停止

I can stop it from performing a postback by adding this attribute to the LinkButton:

OnClientClick="return false;"

但是,这只是发生停止回发时,按需事件仍然​​不火。

But this only stops the postback from occurring, the OnCommand event still doesn't fire.

任何想法?

在code-背后的事件的签名是:

The event signature in the code-behind is:

protected void DoSomething(object sender, CommandEventArgs e) { ... }

我也尝试过与此事件签名ListView控件使用OnItemCommand事件,但同样不调用事件:

I have also tried using an OnItemCommand event on the ListView control with this event signature, but similarly the event is not invoked:

protected void DoSomething(object sender, ListViewCommandEventArgs e)

父GridView中OnRowdataBound事件调用成功,它只是嵌套的ListView失败调用它的事件。

The OnRowdataBound event on the parent GridView is successfully invoked, it's only the nested ListView that fails to invoke its event.

在code我已经证明是页面上的第二个GridView控件,还有另外一个过了,这是这一次它获取的Page_Load事件绑定。页面加载事件有事件的顺序如下,其中第一GridView控件(我们称之为GridView1)被绑定:

The code I have shown is for the 2nd GridView on the page, there is another one too, and it is this one which gets binded on the Page_Load event. The Page Load event has a sequence of events as follows, where the 1st GridView (which we'll call GridView1) is bound:

的Page_Load

有关GridView的数据从数据库中检索

Data for GridView is retrieved from database


  • 的数据被分配给一个数据视图'对象和分配给GridView1 DataSource属性

  • GridView1.DataBind()被调用

  • 其他条件逻辑从GridView1删除某些列

  • 一个onclick属性添加到每一行。

因此​​,实际上,在Page_Load结合的第一个GridView控件。第二GridView控件是一个包含了我有一个问题,在ListView之一。这第二次的GridView(在code这是在后顶部)填充一号的GridView的OnRowCommand事件,并有一个顺序是这样的:

So actually, the Page_Load binds the first GridView. The 2nd GridView is the one which contains the ListView which I am having a problem with. This 2nd GridView (the code of which is at the top of the post) is populated on the 'OnRowCommand' event of the 1st GridView, and has a sequence like this:

GetDataForListView

从数据库中获得的数据


  • 分配包含数据的第二个GridView控件的DataSource属性数据集

  • 调用DataBind()方法

然后,您可以从code我张贴在顶部看到,我有失败的code-后面调用它的事件OnRowDataBound事件。

Then, as you can see from the code I posted at the top, I have the OnRowDataBound event which fails to invoke its event in the code-behind.

推荐答案

夏兰,我复制了整个场景,并发现了以下问题。

Ciaran, I replicated the whole scenario and found out the below issue.

问题::当涉及到无论是GridView2或ListView或LinkBut​​ton的任何事件被解雇它首先打在Page_Load功能即可。在的Page_Load功能你是不是数据绑定GridView2和ListView控件所以显然他们几乎消失,因此事件不会被解雇。

Issue: When any event related to either the GridView2 or ListView or the LinkButton is being fired it first hits the Page_load function. In the Page_load function you are not data binding GridView2 and the ListView so apparently they almost vanish and so the Event will not be fired.

可能的修正:


  1. 您需要绑定的GridView2在Page_Load功能,以激发这些相关活动,并与ListView控件也是如此了。

  1. You need to bind the GridView2 in the Page_load function in order fire those related events and the same goes with the ListView Too.

这修复是非常大的。我不知道,如果你可以试试这个。

This fix is a very big one. I am not sure if you could try this.

一个。如果绑定根据点击则这些参数存储在一些隐藏字段,由GridView1产生的条件GridView2

a. If you are binding the GridView2 depending on the condition generated by the GridView1 click then store those parameters in some hidden field.

乙。有一个名为LoadGridView2(参数),您绑定GridView2并根据参数传递和调用这个函数在Page_Load函数中的每个回发完成每一次和隐藏字段传递存储的参数给LoadGridView2 ListView的一个函数(参数)

b. Have a function named LoadGridView2(parameters) where you bind the GridView2 and the ListView depending on the parameters passed and call this function in the Page_Load function each every time a PostBack is done and pass the stored parameters in the hidden field to the LoadGridView2(parameters)

℃。通过以上你可以做绑定根据GirdView1点击条件GridView2,使GridView2和可用的ListView的回传也。所以,现在的事件可能被解雇。

c. By doing above you can bind the GridView2 according the GirdView1 Click condition and make the GridView2 and ListView available in the PostBack too. So now the event could be fired.

让我知道任何疑问的情况下。

Let me know in case of any queries.

这篇关于ASP.NET LinkBut​​ton的不是调用'按需'事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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