C# 在 UpdatePanel 中的 UpdateMethod 之后刷新 GridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/528924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:53:54  来源:igfitidea点击:

Refresh of GridView after UpdateMethod in UpdatePanel

c#gridviewasp.net-ajax

提问by BrianG

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.

我在 UpdatePanel 中设置了一个 GridView。GridView 有一个绑定到 Gridview1_SelectedIndexChanged 方法的 SELECT CommandField。我希望 GridView 在选择一行后刷新,但它永远不会。我尝试了几种不同的场景,但似乎都不起作用。

  • 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
  • 我在 UpdatePanel 上将 UpdateMode 设置为“Conditional”和“Always”,并试图在后面的代码中强制更新 UpdatePanel。
  • 我已将 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>

采纳答案by pdavis

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.

我想我看到了你的问题。尝试使用您要操作的行的 ID 将 DataKeyNames 参数添加到 GridView。接下来删除触发器部分,因为您在执行的操作中不需要它们。由于您想要执行某些操作,请将 CommandField 更改为其他选项之一,例如您当前未使用的 Delete。接下来修改您的 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>

回答by Barbaros Alp

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

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