【问题标题】:ASP.NET GridView inside UpdatePanel won't get out of Edit ModeUpdatePanel 中的 ASP.NET GridView 不会退出编辑模式
【发布时间】:2012-02-07 06:12:13
【问题描述】:

我有一个非常标准的 GridView,带有一个带有 SelectCommand/UpdateCommand/DeleteCommand 的 SqlDataSource。

我正在使用 Gridview 的 TemplateField'ed 的内置命令。

CommandName="Update"
CommandName="Edit"
CommandName="Cancel"
CommandName="Delete"

在我将 GridView 放入 UpdatePanel 之前,一切正常。 1. 当我点击“编辑”时,它会进入编辑模式,但当您点击取消或更新时,不会退出编辑模式。 2. 当我点击“删除”时,即使点击“编辑”,我也无法进入编辑模式

这是怎么回事?

示例代码:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <div class="BoxFloat">
            <h4>Aliases</h4>
            <hr />
            <asp:SqlDataSource ID="sqlPersonAlias" runat="server" 
                ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
                ProviderName="<%$ ConnectionStrings:myConnectionString.ProviderName %>" 
                DeleteCommand="DELETE FROM PersonAlias WHERE PersonAliasId=?PersonAliasId"
                UpdateCommand="UPDATE PersonAlias SET AliasName=?AliasName WHERE PersonAliasId=?PersonAliasId"
                SelectCommand="SELECT * FROM PersonAlias E INNER JOIN Person P ON P.PersonId=E.PersonId WHERE Username=?Username">
                <DeleteParameters>
                    <asp:Parameter Name="PersonAliasId" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="PersonAliasId" />
                    <asp:Parameter Name="AliasName" />
                </UpdateParameters>
                <SelectParameters>
                    <asp:QueryStringParameter Name="Username" QueryStringField="Username" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:GridView ID="gridviewPersonAlias" runat="server"
                AutoGenerateColumns="False" 
                DataKeyNames="PersonAliasId" 
                DataSourceID="sqlPersonAlias">
                <Columns>
                    <asp:TemplateField ShowHeader="False">
                        <EditItemTemplate>
                            <asp:ImageButton ID="imagebuttonCancel" ToolTip="Cancel" CommandName="Cancel" ImageUrl="~/images/cancel16.png" runat="server" />
                            &nbsp;
                            <asp:ImageButton ID="imagebuttonUpdate" ToolTip="Apply" OnClientClick="return confirm('Are you sure?');" CommandName="Update" ImageUrl="~/images/apply16.png" runat="server" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:ImageButton ID="imagebuttonDelete" ToolTip="Delete" OnClientClick="return confirm('Are you sure?');" CommandName="Delete" ImageUrl="~/images/delete16.png" runat="server" />
                            &nbsp;
                            <asp:ImageButton ID="imagebuttonEdit" ToolTip="Edit" CommandName="Edit" ImageUrl="~/images/edit16.png" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="AliasName" HeaderText="Alias" SortExpression="AliasName" />
                </Columns>
            </asp:GridView>
            <hr />
            <asp:TextBox ID="textboxPersonAlias" runat="server" />
            <asp:Button ID="buttonPersonAliasAdd" runat="server" Text="Add" onclick="buttonPersonAliasAdd_Click" />
        </div>

    </ContentTemplate>
</asp:UpdatePanel>

单击“更新”时,数据库会更新。看来问题出在客户端。

注意:我使用的是“?”而不是“@”作为参数,因为 SqlDataSource 使用的是 MySq 而不是 MSSQL(我喜欢 VS2010)。

【问题讨论】:

  • 当您单击“取消”和“更新”按钮时,您会看到什么行为?页面是否回传?什么都没有发生吗?尝试使用 FireBug 或 Chrome 的开发人员工具来查看正在发布到服务器的值。你用的是哪个浏览器?
  • 什么也没发生(至少在客户端)。服务器端方法似乎有效(数据库更新)。我使用 Chrome 和 Firefox 进行了测试。
  • 也许您在更新面板之外还有其他一些控件受到影响,或者您禁用了视图状态?

标签: asp.net gridview updatepanel


【解决方案1】:

我认为您需要的是完整的回发或刷新更新面板,因为它很可能是 gridview 没有重新绑定 首先尝试将 sqldatasource 放在更新面板之外。看看有没有用

【讨论】:

  • 当单击更新命令时,数据库似乎可以正确更新。只是GridView不会渲染出EditItemTemplate并返回ti ItemTemplate
【解决方案2】:

我的项目中遇到了同样的问题,并解决了。

这是我的解决方案:

protected void gvPayments_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  //here is the edit code
  //......
  //at the end add these lines:
  gvPayments.EditIndex = -1;
  gvPayments.DataBind();
  e.Cancel = true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2013-02-08
    相关资源
    最近更新 更多