Refresh of GridView after UpdateMethod in UpdatePa

2020-07-23 08:57发布

问题:

I have setup a GridView inside an UpdatePanel. The GridView has a SELECT CommandField that is tied to Gridview1_SelectedIndexChanged method. I would like the GridView to refresh after a row is selected but it never does. 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.
  • I have converted the CommandField to a templatefield with a button

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>

回答1:

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. 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. 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. After the method returns it will refresh the GridView from the SelectMethod defined.

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


回答2:

If i understand you right, you need to bind the datasource to your grid on each postback.