简体   繁体   English

在UpdatePanel中UpdateMethod之后刷新GridView

[英]Refresh of GridView after UpdateMethod in UpdatePanel

I have setup a GridView inside an UpdatePanel. 我在UpdatePanel中设置了一个GridView。 The GridView has a SELECT CommandField that is tied to Gridview1_SelectedIndexChanged method. GridView有一个与Gridview1_SelectedIndexChanged方法绑定的SELECT CommandField。 I would like the GridView to refresh after a row is selected but it never does. 我想在选择一行后刷新GridView,但它永远不会。 I have tried several different scenarios and none seem to work. 我尝试了几种不同的方案,但似乎都没有。

  • I have set UpdateMode to "Conditional" and "Always" on the UpdatePanel and tried to force an update to the UpdatePanel in the code behind. 我在UpdatePanel上将UpdateMode设置为“Conditional”和“Always”,并尝试在后面的代码中强制更新UpdatePanel。
  • I have converted the CommandField to a templatefield with a button 我已经使用按钮将CommandField转换为模板字段

Here is the sanitized code: 这是清理过的代码:

  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
    <asp:GridView ID="GridView1"
         runat="server"
         AllowPaging="True" 
         AllowSorting="True"
         AutoGenerateColumns="False"
         DataSourceID="ObjectDataSource1"
         OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 
         PagerSettings-Visible="true" EnableViewState="False" >
    <Columns>
        <asp:CommandField ButtonType="Image"
             SelectImageUrl="~/images/icon.gif" 
             ShowSelectButton="True" />
        <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
        <asp:BoundField DataField="Title" HeaderText="Title" 
             SortExpression="Title" />
    </Columns>
    </asp:GridView>
   </ContentTemplate>
   <Triggers>
        <asp:AsyncPostBackTrigger ControlID="GridView1" 
            EventName="SelectedIndexChanged" />
   </Triggers>
  </asp:UpdatePanel>

The data source looks something like this... 数据源看起来像这样......

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
     DataObjectTypeName="myNamespace.Item"
     InsertMethod="myInsertMethod" 
     SelectMethod="mySelectMethod" 
     TypeName="myNamespace.ItemMgr"
     UpdateMethod="myUpdateMethod">
</asp:ObjectDataSource>

I think I see your problem. 我想我看到了你的问题。 Try adding a DataKeyNames paramater to the GridView with the ID of the row you want to act on. 尝试将DataKeyNames参数添加到GridView,并使用您要操作的行的ID。 Next remove the Triggers section as you won't need them for what you are doing. 接下来删除“触发器”部分,因为您不需要它们。 Since you are wanting to act on something change the CommandField to one of the other options such as Delete which you aren't currently using. 由于您希望对某些操作进行操作,因此将CommandField更改为其他选项之一,例如您当前未使用的Delete。 Next modify your ObjectDataSource to define a DeleteMethod in your myNamespace.ItemMgr that accepts the Id (DataKeyNames paramater) from the GridView and performs the task you wish to perform. 接下来修改您的ObjectDataSource,在myNamespace.ItemMgr中定义一个DeleteMethod,它接受来自GridView的Id(DataKeyNames参数)并执行您想要执行的任务。 After the method returns it will refresh the GridView from the SelectMethod defined. 方法返回后,它将从定义的SelectMethod刷新GridView。

  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
    <asp:GridView ID="GridView1"
         runat="server"
         AllowPaging="True" 
         AllowSorting="True"
         AutoGenerateColumns="False"
         DataSourceID="ObjectDataSource1"
         PagerSettings-Visible="true" EnableViewState="False"
         DataKeyNames="Id" >
    <Columns>
        <asp:CommandField DeleteImageUrl="/images/icon.gif" 
             DeleteText="Some Text"
             ShowDeleteButton="True" 
             ButtonType="Image" />
        <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
        <asp:BoundField DataField="Title" HeaderText="Title" 
             SortExpression="Title" />
    </Columns>
    </asp:GridView>
   </ContentTemplate>
  </asp:UpdatePanel>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    DeleteMethod="myDeleteMethod" SelectMethod="mySelectMethod" 
    TypeName="myNamespace.ItemMgr">
</asp:ObjectDataSource>

如果我理解你,你需要在每次回发时将数据源绑定到网格。

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

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