c#-UpdatePanel中的UpdateMethod之后刷新GridView
作者:互联网
我已经在UpdatePanel中设置了GridView. GridView具有与Gridview1_SelectedIndexChanged方法绑定的SELECT CommandField.我希望在选择行后刷新GridView,但是从不刷新.我尝试了几种不同的方案,但似乎都没有用.
>我在UpdatePanel上将UpdateMode设置为“ Conditional”和“ Always”,并尝试在后面的代码中强制对UpdatePanel进行更新.
>我已经通过按钮将CommandField转换为templatefield
这是经过清理的代码:
<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>
数据源看起来像这样…
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="myNamespace.Item"
InsertMethod="myInsertMethod"
SelectMethod="mySelectMethod"
TypeName="myNamespace.ItemMgr"
UpdateMethod="myUpdateMethod">
</asp:ObjectDataSource>
解决方法:
我想我看到你的问题了.尝试将DataKeyNames参数添加到GridView中,并添加要操作的行的ID.接下来,删除“触发器”部分,因为您不需要做什么操作.由于您要执行某些操作,因此将CommandField更改为其他选项之一,例如“删除”(您当前未使用).接下来,修改ObjectDataSource以在myNamespace.ItemMgr中定义一个DeleteMethod,该方法从GridView接受ID(DataKeyNames参数)并执行您想要执行的任务.方法返回后,它将从定义的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>
标签:gridview,asp-net-ajax,c 来源: https://codeday.me/bug/20191108/2004428.html