简体   繁体   English

由UpdatePanel内的GridView内的LinkBut​​ton触发的完整回发

[英]Full postback triggered by LinkButton inside GridView inside UpdatePanel

I have a GridView inside of a UpdatePanel. 我在UpdatePanel中有一个GridView。 In a template field is a button I use for marking items. 在模板字段中是我用于标记项目的按钮。 Functionally, this works fine, but the button always triggers a full page postback instead of a partial postback. 在功能上,这工作正常,但按钮总是触发整页回发而不是部分回发。 How do I get the button to trigger a partial postback? 如何让按钮触发部分回发?

<asp:ScriptManager ID="ContentScriptManager" runat="server" />
<asp:UpdatePanel ID="ContentUpdatePanel" runat="server" ChildrenAsTriggers="true">
    <ContentTemplate>
        <asp:GridView ID="OrderGrid" runat="server" AllowPaging="false" AllowSorting="false"
            AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:LinkButton ID="MarkAsCompleteButton" runat="server" Text="MarkAsComplete"
                            CommandName="MarkAsComplete" CommandArgument='<%# Eval("Id") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="LoadDate" HeaderText="Load Date" />
                <asp:BoundField DataField="EmployeeCutOffDate" HeaderText="Cut Off Date" />
                <asp:BoundField DataField="IsComplete" HeaderText="Is Completed" />
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

You need to register each and every LinkButton as an AsyncPostBackTrigger . 您需要将每个LinkBut​​ton注册为AsyncPostBackTrigger After each row is bound in your GridView, you'll need to search for the LinkButton and register it through code-behind as follows: 在GridView中绑定每一行之后,您需要搜索LinkBut​​ton并通过代码隐藏注册它,如下所示:

protected void OrderGrid_RowDataBound(object sender, GridViewRowEventArgs e)  
{  
   LinkButton lb = e.Row.FindControl("MarkAsCompleteButton") as LinkButton;  
   ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lb);  
}  

This also requires that ClientIDMode="AutoID" be set for the LinkButton, as mentioned here (thanks to Răzvan Panda for pointing this out). 这也要求ClientIDMode="AutoID"为LinkBut​​ton的设定,如提到这里 (感谢的Răzvan熊猫指出这一点)。

It's probably not advised but you can make everything on the GridView work asynchronously by excluding the EventName on the AsyncPostBackTrigger so eg 可能没有建议,但你可以通过排除AsyncPostBackTrigger上的EventName来使GridView上的所有内容异步工作,例如

<Triggers>
  <asp:AsyncPostBackTrigger ControlID="OrderGrid" />
</Triggers>

This will make the RowCommand event and any other event on the GridView fire asynchronously. 这将使GridView上的RowCommand事件和任何其他事件异步触发。 Note as well that when you make ClientIDMode="Static" on the GridView it will cause a full postback. 另请注意,当您在GridView上创建ClientIDMode =“Static”时,它将导致完整的回发。

My grid view is in conditional mode. 我的网格视图处于条件模式。

protected void gvAgendamentoExclui_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) {
            LinkButton lnk = e.Row.FindControl("LinkButton2") as LinkButton;
            AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
            trigger.ControlID = lnk.UniqueID;
            trigger.EventName = "Click";
            UpdatePanel2.Triggers.Add(trigger);

        }
    }

And in the click event of the linkbutton I put: 在链接按钮的click事件中,我把:

protected void LinkButton2_Click(object sender, EventArgs e)
    {
        UpdatePanel2.Update();
    }

将以下元素放在web.config文件中的system.web元素中

<xhtmlConformance mode="Transitional"/>

I had an issue where I had one form working fine ( page1 ), another doing whole post backs ( page2 ). 我有一个问题,我有一个形式工作的罚款( page1 ),另一个在做整个后背上( page2 )。 Turned out when I made the 2nd page, I had done a bit too much cut/paste , and it still had a javascript call in the form definition. 当我进入第二页时,我做了一些cut/paste太多了,它仍然在表单定义中有一个javascript调用。

< form id="form1" runat="server" onsubmit="return checkstuff();">

But checkstuff was not defined in page 2 . 但是page 2没有定义checkstuff

deleted the onsubmit , and the partial posts started working. 删除了onsubmit ,部分帖子开始工作了。

In the working page - page 1, checkstuff was defined, but was just a stub, which did nothing more than return true. 在工作页面 - 第1页, checkstuff被定义,但只是一个存根,它只是返回true。 Just for grins, I put an alert in checkstuff , and sure enough, it is called for all submits, partial or not. 只是为了笑容,我在checkstuff发出警报, checkstuff ,所有提交都会被调用,部分或不是。 And, if I changed the stub to just return false, nothing happened at all. 并且,如果我将存根更改为仅返回false,则根本不会发生任何事情。

Point in all this, the javascript is still exercised, as if a full page is being submitted. 指出所有这些,javascript仍然被运用,就像提交整页一样。 So double check your client side scripts. 因此请仔细检查您的客户端脚本。

this may be old but my solution was to put an update panel inside the itemTemplate and one outside the gridview as well. 这可能是旧的,但我的解决方案是在itemTemplate中放置一个更新面板,在gridview外放置一个更新面板。

the trigger should be the gridview and the outside trigger should be the gridview and PageIndexChanging. 触发器应该是gridview,外部触发器应该是gridview和PageIndexChanging。 Try that. 试试吧。

MSDN specifies that the UpdatePanel.ChildrenAsTriggers property "[g]ets or sets a value that indicates whether postbacks from immediate child controls of an UpdatePanel control update the panel's content" (see http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.childrenastriggers.aspx ). MSDN指定UpdatePanel.ChildrenAsTriggers属性“[g]设置或设置一个值,该值指示UpdatePanel控件的直接子控件的回发是否更新了面板的内容”(请参阅http://msdn.microsoft.com/en-us/ library / system.web.ui.updatepanel.childrenastriggers.aspx )。

Since your LinkButton does not appear to be an "immediate child control," then I would recommend configuring your LinkButton as an explicit AsyncPostBackTrigger. 由于您的LinkBut​​ton似乎不是“直接子控件”,因此我建议将LinkBut​​ton配置为显式的AsyncPostBackTrigger。

Below your </ContentTemplate> tag, try adding this: 在</ ContentTemplate>标记下方,尝试添加以下内容:

<Triggers>
  <asp:AsyncPostBackTrigger ControlID="MarkAsCompleteButton" EventName="Click" />
</Triggers>

You need to register each controls for each RowState. 您需要为每个RowState注册每个控件。 1: Register your controls for RowState = Alternate and Normal) 2: Register your controls for RowState = Edit 3: ... 1:为RowState = Alternate和Normal注册控件2:为RowState注册控件=编辑3:...

ASPX: ASPX:

<asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:LinkButton runat="server" ID="Btn1" 
                        CommandName="Edit" CommandArgument='<%# Container.DataItemIndex + ";" + Eval("idinterlocuteur") %>'><i class="fa fa-pencil-square-o"></i></asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="Btn2" runat="server" CommandName="Update" CommandArgument='<%# Container.DataItemIndex + ";" + Eval("idinterlocuteur") %>'><i class="fa fa-check"></i></asp:LinkButton>
                </EditItemTemplate>
            </asp:TemplateField>

Code behind : 代码背后:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow 
        && (e.Row.RowState == DataControlRowState.Normal 
            || e.Row.RowState == DataControlRowState.Alternate))
    {
        LinkButton Btn1 = e.Row.FindControl("Btn1 ") as LinkButton; 
        ScriptManager.GetCurrent(this.Parent.Page).RegisterAsyncPostBackControl(Btn1 );
    }
    if (e.Row.RowType == DataControlRowType.DataRow 
        && e.Row.RowState == DataControlRowState.Edit)
    {
        LinkButton Btn2 = e.Row.FindControl("Btn2 ") as LinkButton;
        ScriptManager.GetCurrent(this.Parent.Page).RegisterAsyncPostBackControl(Btn2 );      
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM