代码之家  ›  专栏  ›  技术社区  ›  Frinavale

如何在UpdatePanel中配置GridView命令字段以触发整页更新

  •  5
  • Frinavale  · 技术社区  · 15 年前

    我的页面上有两个用户控件。 一个用于搜索,另一个用于编辑(以及其他一些东西)。

    提供搜索功能的用户控件使用GridView显示搜索结果。此网格视图有一个用于编辑的命令字段(showeditbutton=“true”)。

    我想将GridView放置到一个更新面板中,以便在搜索结果中顺利分页。

    问题是,当用户单击编辑链接(commandfield)时,我需要执行整页回发,以便可以隐藏搜索用户控件并显示编辑用户控件。

    编辑: 我需要进行整页回发的原因是因为编辑用户控件在我的GridView所在的更新面板之外。它不仅在更新面板之外,而且在完全不同的用户控件中。

    我不知道如何将commandfield作为整页回发触发器添加到updatepanel。postbacktrigger(用于指示控件导致整页回发)将controlID作为参数;但是commandbutton没有ID…,您可以看到为什么我对此有问题。

    更新我尝试解决问题的其他内容: 我采取了一种新的方法来解决这个问题。

    在我的新方法中,我使用模板字段而不是命令字段。我在TemplateField中放置了一个LinkButton控件并给它命名。在GridView的RowDataBound事件期间,我检索到LinkButton控件并将其添加到UpdatePanel的触发器中。

    这是UpdatePanel和GridView的ASP标记

    <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,为LinkButton创建一个PostbackTrigger,并将其添加到UpdatePanel的触发器中。 这意味着将为GridView中的每个“编辑”链接按钮创建一个PostbackTrigger。 编辑: 这并没有为每个“编辑”链接按钮创建一个PostbackTrigger,因为GridView中所有链接按钮的ID都相同。

    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
    

    我使用rowcommand而不是为了编辑而处理gridview的rowediting事件。

    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
    

    这种新方法根本不起作用,因为GridView中的所有链接按钮都具有相同的ID。

    在阅读了有关 UpdatePanel.Triggers Property 我认为触发器只能声明性地定义。这意味着我在VB代码中所做的任何事情都无法工作。

    任何建议都将不胜感激。

    谢谢,

    -弗林尼

    6 回复  |  直到 9 年前
        1
  •  8
  •   sepho    15 年前

    要将控件注册为回发的触发器,请使用ScriptManager的RegisterPostBackControl方法。

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

    对于动态添加到其他控件中的控件,此方法也适用。 我的网格视图也有同样的问题。我已经注册了模板字段行的LinkButton和ImageButton。

    我使用rowcreated gridview的处理程序而不是rowdatabound处理程序: rowcreated的重点是解析gridview行的定义和创建gridview行的控制结构, rowdatabound的重点是将数据绑定到在row created中创建的行控件。 在init和postback情况下,也会自动调用rowcreated,但只有在调用databind时才调用rowdatabind。 代码

    <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);
            }
        }
    
        2
  •  2
  •   Frinavale    15 年前

    为了解决这个问题,我实现了一个自定义分页控件,该控件位于GridView的上方,但仍位于UpdatePanel中。使用分页控件时,将异步更新GridView。我将GridView设置为UpdatePanel的PostbackTrigger。现在,GridView中的每个控件都会导致整页回发。这意味着编辑控件将导致整页回发,但排序也是如此。

    我觉得这里有点失败,但至少我有一个半工作的解决方案。

    我仍然有兴趣听到任何人提出的解决方案,可能会对解决问题有所帮助。

    -弗林尼

        3
  •  2
  •   Brandon Culley    9 年前

    这篇文章对我很有用,它使用了rowcreated和scriptmanager link text

    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); 
    } 
    
    } 
    
        4
  •  1
  •   Christian Specht    13 年前

    您也可以在 Page_load 在回发中。我认为,每一个回撤后他们都会重新调整,这会起作用。

        5
  •  0
  •   Poker Villain    14 年前

    除非在页面加载之前添加触发器,否则不能使用RegisterPostBackControl或通过向Triggers集合添加新的PostBackTrigger向更新面板添加触发器。

    必须在OnInitComplete或OnInit中完成

        6
  •  0
  •   Taylor Brown    12 年前

    我张贴的解决方案从布兰登库利提供的链接,我只是使用它,这是正确的方式做到这一点,并工作得很漂亮。他的链接指向代码的C版本,我正在发布我用于VB的版本。这里是 the link 再次以防有人错过。

    当在GridView中创建行(rowCreated事件)时,请在该行中找到链接按钮,并通过脚本管理器将其注册为回发控件-这将允许它在单击时执行完整回发:

    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