在UpdatePanel中的GridView中,由LinkButton触发的完整回发

时间:2021-10-31 15:05:16

I have a GridView inside of a UpdatePanel. 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?

我在UpdatePanel中有一个GridView。在模板字段中是我用来标记项目的按钮。在功能上,这很好,但是按钮总是触发全页回发,而不是部分回发。如何让按钮触发部分回发?

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

8 个解决方案

#1


72  

You need to register each and every LinkButton as an 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:

您需要将每个链接按钮注册为AsyncPostBackTrigger。在GridView中绑定每一行后,需要搜索LinkButton,并通过代码-behind注册如下:

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 " LinkButton设置,在这里提到(由于Răzvan熊猫指向这一点)。

#2


5  

It's probably not advised but you can make everything on the GridView work asynchronously by excluding the EventName on the AsyncPostBackTrigger so e.g.

可能不建议这样做,但是您可以通过排除AsyncPostBackTrigger上的EventName,使GridView上的所有内容以异步方式工作。

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

This will make the RowCommand event and any other event on the GridView fire asynchronously. Note as well that when you make ClientIDMode="Static" on the GridView it will cause a full postback.

这将使RowCommand事件和GridView中的任何其他事件异步发生。请注意,当您在GridView上创建ClientIDMode="Static"时,它将导致一个完整的回发。

#3


3  

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:

在我输入的链接按钮的点击事件中:

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

#4


1  

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]ets或设置一个值,该值指示来自UpdatePanel控件的直接子控件的回发是否更新面板的内容”(参见http://msdn.microsoft.com/en- us/library/system.web.ui.epanel.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.

由于您的LinkButton似乎不是一个“立即子控件”,所以我建议将您的LinkButton配置为一个显式的AsyncPostBackTrigger。

Below your </ContentTemplate> tag, try adding this:

在您的标签下面,尝试添加以下内容:

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

#5


1  

Put the following element inside system.web element in web.config file

将以下元素放入系统中。在web网页元素。配置文件

<xhtmlConformance mode="Transitional"/>

#6


0  

I had an issue where I had one form working fine (page1), another doing whole post backs (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.

我有一个问题,我有一个表单工作得很好(page1),另一个做整个post back (page2)。结果当我创建第二页时,我做了太多的剪切/粘贴,它在表单定义中仍然有一个javascript调用。

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

But checkstuff was not defined in page 2.

但是checkstuff在第2页中没有定义。

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. Just for grins, I put an alert in checkstuff, and sure enough, it is called for all submits, partial or not. And, if I changed the stub to just return false, nothing happened at all.

在工作页面-第1页中,已经定义了checkstuff,但只是一个存根,它只返回true。为了让大家笑一笑,我在checkstuff中设置了一个警告,当然,所有提交(部分提交或不提交)都需要这个警告。如果我将存根改为返回false,那么就什么都没发生。

Point in all this, the javascript is still exercised, as if a full page is being submitted. So double check your client side scripts.

指出所有这些,javascript仍然被执行,就好像提交了一个完整的页面一样。因此,请仔细检查客户端脚本。

#7


0  

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. Try that.

触发器应该是gridview,外部触发器应该是gridview和pageindexchange。试试。

#8


-1  

You need to register each controls for each RowState. 1: Register your controls for RowState = Alternate and Normal) 2: Register your controls for RowState = Edit 3: ...

您需要为每个RowState注册每个控件。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 );      
    }
}

#1


72  

You need to register each and every LinkButton as an 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:

您需要将每个链接按钮注册为AsyncPostBackTrigger。在GridView中绑定每一行后,需要搜索LinkButton,并通过代码-behind注册如下:

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 " LinkButton设置,在这里提到(由于Răzvan熊猫指向这一点)。

#2


5  

It's probably not advised but you can make everything on the GridView work asynchronously by excluding the EventName on the AsyncPostBackTrigger so e.g.

可能不建议这样做,但是您可以通过排除AsyncPostBackTrigger上的EventName,使GridView上的所有内容以异步方式工作。

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

This will make the RowCommand event and any other event on the GridView fire asynchronously. Note as well that when you make ClientIDMode="Static" on the GridView it will cause a full postback.

这将使RowCommand事件和GridView中的任何其他事件异步发生。请注意,当您在GridView上创建ClientIDMode="Static"时,它将导致一个完整的回发。

#3


3  

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:

在我输入的链接按钮的点击事件中:

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

#4


1  

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]ets或设置一个值,该值指示来自UpdatePanel控件的直接子控件的回发是否更新面板的内容”(参见http://msdn.microsoft.com/en- us/library/system.web.ui.epanel.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.

由于您的LinkButton似乎不是一个“立即子控件”,所以我建议将您的LinkButton配置为一个显式的AsyncPostBackTrigger。

Below your </ContentTemplate> tag, try adding this:

在您的标签下面,尝试添加以下内容:

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

#5


1  

Put the following element inside system.web element in web.config file

将以下元素放入系统中。在web网页元素。配置文件

<xhtmlConformance mode="Transitional"/>

#6


0  

I had an issue where I had one form working fine (page1), another doing whole post backs (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.

我有一个问题,我有一个表单工作得很好(page1),另一个做整个post back (page2)。结果当我创建第二页时,我做了太多的剪切/粘贴,它在表单定义中仍然有一个javascript调用。

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

But checkstuff was not defined in page 2.

但是checkstuff在第2页中没有定义。

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. Just for grins, I put an alert in checkstuff, and sure enough, it is called for all submits, partial or not. And, if I changed the stub to just return false, nothing happened at all.

在工作页面-第1页中,已经定义了checkstuff,但只是一个存根,它只返回true。为了让大家笑一笑,我在checkstuff中设置了一个警告,当然,所有提交(部分提交或不提交)都需要这个警告。如果我将存根改为返回false,那么就什么都没发生。

Point in all this, the javascript is still exercised, as if a full page is being submitted. So double check your client side scripts.

指出所有这些,javascript仍然被执行,就好像提交了一个完整的页面一样。因此,请仔细检查客户端脚本。

#7


0  

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. Try that.

触发器应该是gridview,外部触发器应该是gridview和pageindexchange。试试。

#8


-1  

You need to register each controls for each RowState. 1: Register your controls for RowState = Alternate and Normal) 2: Register your controls for RowState = Edit 3: ...

您需要为每个RowState注册每个控件。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 );      
    }
}