Updating a GridView after UpdateMethod in UpdatePanel

I installed the GridView inside the UpdatePanel. GridView has a SELECT CommandField element bound to the Gridview1_SelectedIndexChanged method. I would like the GridView to be updated after row selection, but this never happens. I tried several different scenarios and no one works.

  • I installed UpdateMode in “Conditional” and “Always” on UpdatePanel and tried to force update UpdatePanel in the code.
  • I converted CommandField to a template field using a button

Here is the sanitary 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> 
+3
source share
2 answers

I think I see your problem. Try adding the DataKeyNames paramater parameter to the GridView with the ID of the row you want to act on. Then delete the “Triggers” section as you will not need them for what you are doing. Since you want to act on something, change the CommandField to one of the other options, such as Delete, which you are not currently using. Then modify the ObjectDataSource to define DeleteMethod in myNamespace.ItemMgr, which takes the identifier (DataKeyNames paramater) from the GridView and performs the task that you want to perform. After the method returns, it will update the GridView from the selected SelectMethod method.

  <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> 
+3
source

If you understand correctly, you need to bind the data source to your grid with each postback.

+1
source

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


All Articles