Full postback triggered by LinkButton inside GridView inside UpdatePanel

前端 未结 8 806
余生分开走
余生分开走 2020-12-02 13:22

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

相关标签:
8条回答
  • 2020-12-02 13:54

    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:

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

    0 讨论(0)
  • 2020-12-02 14:00

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

    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 );      
        }
    }
    
    0 讨论(0)
提交回复
热议问题