UpdateMethod的UpdatePanel中的GridView后刷新 [英] Refresh of GridView after UpdateMethod in UpdatePanel
问题描述
我已经安装一个UpdatePanel内的GridView。 GridView控件有绑定到Gridview1_SelectedIndexChanged方法的选择CommandField中。我想在GridView被选中之后的行,但它从来不会刷新。我尝试了好几种不同的方案,并没有似乎工作。
- 我已经设置的UpdateMode为条件,并在UpdatePanel的始终,并试图在code后面强制更新UpdatePanel的。
- 我已经转换了CommandField中为一个TemplateField一个按钮
下面是消毒code:
< ASP:的UpdatePanel ID =UpdatePanel1=服务器>
<&的ContentTemplate GT;
< ASP:GridView控件ID =GridView1
=服务器
AllowPaging =真
AllowSorting =真
的AutoGenerateColumns =FALSE
的DataSourceID =ObjectDataSource1
OnSelectedIndexChanged =GridView1_SelectedIndexChanged
PagerSettings - 可见=真的EnableViewState =假>
<柱体和GT;
< ASP:CommandField中按钮类型=形象
SelectImageUrl =〜/图片/ icon.gif
ShowSelectButton =真/>
< ASP:BoundField的数据字段=ID的HeaderText =IDSORTEX pression =ID/>
< ASP:BoundField的数据字段=标题的HeaderText =标题
SORTEX pression =标题/>
< /专栏>
< / ASP:GridView的>
< /&的ContentTemplate GT;
<&触发器GT;
< ASP:AsyncPostBackTrigger控件ID =GridView1
事件名称=的SelectedIndexChanged/>
< /触发器>
< / ASP:的UpdatePanel>
数据源看起来像这样...
< ASP:ObjectDataSource控件ID =ObjectDataSource1=服务器
DataObjectTypeName =myNamespace.Item
InsertMethod =myInsertMethod
SelectMethod =mySelectMethod
类型名=myNamespace.ItemMgr
UpdateMethod =myUpdateMethod>
< / ASP:ObjectDataSource控件>
我想我看到你的问题。尝试添加的DataKeyNames paramater到GridView与要作用于行的ID。接下来删除触发器部分,你将不再需要他们为你在做什么。既然你是想采取行动的东西改变CommandField中来的其他选项,如删除您当前没有使用。下一步修改ObjectDataSource控件在myNamespace.ItemMgr接受来自GridView控件的ID(的DataKeyNames paramater),并执行要执行的任务来定义DeleteMethod。该方法返回后,它会从定义的SelectMethod刷新GridView的。
< ASP:的UpdatePanel ID =UpdatePanel1=服务器>
<&的ContentTemplate GT;
< ASP:GridView控件ID =GridView1
=服务器
AllowPaging =真
AllowSorting =真
的AutoGenerateColumns =FALSE
的DataSourceID =ObjectDataSource1
PagerSettings - 可见=真的EnableViewState =假
的DataKeyNames =ID>
<柱体和GT;
< ASP:CommandField中DeleteImageUrl =/图像/ icon.gif
DeleteText =一些文本
的ShowDeleteButton =真
按钮类型=图像/>
< ASP:BoundField的数据字段=ID的HeaderText =IDSORTEX pression =ID/>
< ASP:BoundField的数据字段=标题的HeaderText =标题
SORTEX pression =标题/>
< /专栏>
< / ASP:GridView的>
< /&的ContentTemplate GT;
< / ASP:的UpdatePanel>< ASP:ObjectDataSource控件ID =ObjectDataSource1=服务器
DeleteMethod =myDeleteMethodSelectMethod =mySelectMethod
类型名=myNamespace.ItemMgr>
< / ASP:ObjectDataSource控件>
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>
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>
这篇关于UpdateMethod的UpdatePanel中的GridView后刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!