How to configure GridView CommandField to run a full page refresh in UpdatePanel

I have 2 user controls on my page. One is used for searching, and the other is used for editing (along with several other things).

A user control that provides a search function uses a GridView to display search results. This GridView has a CommandField used for editing (showEditButton = "true").

I would like to put the GridView in the UpdatePanel so that the paging through the search results is smooth.

The fact is that when a user clicks on the “Edit Link” link (CommandField), I need to pre-generate a full page postback so that the user search control can be hidden and the user edit control can be displayed.

Edit: The reason I need to post back over a full page is because the custom edit control is outside of the UpdatePanel where my GridView is located. It is located not only outside of UpdatePanel, but also in a completely different user management.

I have no idea how to add a CommandField to a full-text callback for UpdatePanel. PostBackTrigger (which is used to indicate that controls cause a full page postback) takes a ControlID as a parameter; however, CommandButton does not have an identifier ... and you can understand why I have a problem with this.

Updating what else I was trying to solve the problem: I took a new approach to solving the problem.

In my new approach, I used TemplateField instead of CommandField. I put the LinkButton control in the TemplateField and gave it a name. During the GridView RowDataBound event, I received a LinkButton control and added it to UpdatePanel triggers.

This is ASP Markup for UpdatePanel and GridView

<asp:UpdatePanel ID="SearchResultsUpdateSection" runat="server">
  <ContentTemplate>
    <asp:GridView ID="SearchResultsGrid" runat="server" 
        AllowPaging="true" 
        AutoGenerateColumns="false">
      <Columns>
        <asp:TemplateField>
          <HeaderTemplate></HeaderTemplate>
          <ItemTemplate>
            <asp:LinkButton ID="Edit" runat="server" Text="Edit"></asp:LinkButton>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ......
      </Columns>
    </asp:GridView>
  </ContentTemplate>
</asp:UpdatePanel>

VB.NET. , GridView RowDataBound. LinkButton , PostBackTrigger LinkButton UpdatePanel. , PostBackTrigger "" LinkButton GridView Edit:, PostBackTrigger "edit" LinkButton, LinkButtons GridView.

Private Sub SearchResultsGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles SearchResultsGrod.RowDataBound
    If e.Row.RowType = DataControlRowType.Header Then
       ''I am doing stuff here that does not pertain to the problem
    Else
        Dim editLink As LinkButton = CType(e.Row.FindControl("Edit"), LinkButton)
        If editLink IsNot Nothing Then
            Dim fullPageTrigger As New PostBackTrigger
            fullPageTrigger.ControlID = editLink.ID
            SearchResultsUpdateSection.Triggers.Add(fullPageTrigger)
        End If

    End If
End Sub

, GridView RowEditing , RowCommand.

Private Sub SearchResultsGrid_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles SearchResultsGrid.RowCommand
        RaiseEvent EditRecord(Me, New EventArgs())
End Sub

, LinkButtons GridView .

MSDN UpdatePanel.Triggers Property , . , , VB, .

.

,

-Frinny

+3
6

, , GridView, UpdatePanel. , GridView . GridView PostBackTrigger UpdatePanel. GridView . , , .

, , , .

, - , .

-Frinny

+2

, ScriptManager RegisterPostBackControl.

     If editLink IsNot Nothing Then
           ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(editLink )
        End If

, . GridView. linkButton imageButton TemplatedField.

RowCreated Gridview RowDataBound: RowCreated Gridview Gridview, RowDataBound , RowCreated. , RowCreated init, postback, RowDataBound DataBind.

<asp:GridView ID="ContactsGridView" runat="server" AutoGenerateColumns="False"
        DataSourceID="ContactsContainerDataSource" EnableViewState="false"
        DataKeyNames="CompanyID,ContactId" AllowSorting="True" AllowPaging="true"
        OnRowCreated="ContactsGridView_RowCreated" PageSize="10" CssClass="GridSimple"
        OnRowCommand="ContactsGridView_RowCommand">
        <Columns>
               <asp:TemplateField HeaderStyle-CssClass="large" HeaderText="Contact" SortExpression="ContactNom">
                <ItemTemplate>
                    <asp:ImageButton ID="PreviewImageButton" runat="server" ImageUrl="../images/picto_pdf.gif"
                        CommandName="PreviewContact" CommandArgument=<%#Eval("ContactCode") %> BorderWidth="0"
                        ToolTip="View Pdf Document" />
                    <asp:LinkButton ID="ContactNamePreviewLinkButton" runat="server" CommandName="Select"
                        CommandArgument='<%#Eval("ContactCode") %>'><%#Eval("ContactName")%></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>




    protected void ContactsGridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Register download buttons as PostBack controls  (instead of AsyncPostback because of their updatepanel container))
            ImageButton ib = (ImageButton)e.Row.FindControl("PreviewImageButton");
            if (ib != null) ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(ib);
            LinkButton lb = (LinkButton)e.Row.FindControl("ContactNamePreviewLinkButton");
            if (lb != null) ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(lb);
        }

    }
    protected void ContactsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "PreviewContact")
        {

            _presenter.OnPreviewContactClicked((string)e.CommandArgument);
        }
    }
+7

, rowCreated scriptManager

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    LinkButton lnk = e.Row.FindControl("LinkButton1") as LinkButton;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        if (lnk != null)
        {
            ScriptManager1.RegisterPostBackControl(lnk);
        }
        //PostBackTrigger pb = new PostBackTrigger();

        //pb.ControlID = lnk.UniqueID;

    }

}

LinkButton lnk = gvMFDWise.FindControl("lnkbtn") as LinkButton; 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
if (lnk != null) 
{ 
ScriptManager scriptManager2 = ToolkitScriptManager.GetCurrent(Page); 
scriptManager2.RegisterPostBackControl(lnk); 
} 

} 
+2

Page_load . , , , ,

+1

, RegisterPostBackControl, PostBackTrigger , .

Oninitcomplete oninit

0

, , , . #, , VB. , - .

gridview ( RowCreated), scriptmanager - :

Protected Sub gridMusicLibrary_RowCreated(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridMusicLibrary.RowCreated

    'clicking the link button needs to do a full 
    'postback outside of the update panel

    Dim lbtn As LinkButton = e.Row.FindControl("YourLinkButtonControlId")

    If e.Row.RowType = DataControlRowType.DataRow Then

        ScriptManager1.RegisterPostBackControl(lbtn)

    End If

End Sub
0

Source: https://habr.com/ru/post/1715357/


All Articles