0

我正在使用 SQL Server 2008 R2 Express、C# ASP.NET 4。

我在使用此 T-SQL(包括数据)创建的 sql 服务器上有一个表:

CREATE DATABASE [someDatabase]
GO
USE [someDatabase]

CREATE TABLE someTable (someCode [int] NOT NULL IDENTITY(1,1) PRIMARY KEY, someDescription [nvarchar](50) NULL);
INSERT INTO [dbo].[someTable] (someDescription) VALUES (''),('row 2, 1st non empty line'),('3'),(''),('5th row');
SELECT * FROM [dbo].[someTable]  

我还有一个 Default.aspx 文件,其中包含一个带有上表 ConnectionString 的 Gridview,包括编辑和删除选项(模板) - 后面没有代码:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="someCode" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="someCode" HeaderText="someCode" 
                InsertVisible="False" ReadOnly="True" SortExpression="someCode" />
            <asp:BoundField DataField="someDescription" HeaderText="someDescription" 
                SortExpression="someDescription" />
        </Columns>
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:someDatabaseConnectionString %>" 
        DeleteCommand="DELETE FROM [someTable] WHERE [someCode] = @original_someCode AND (([someDescription] = @original_someDescription) OR ([someDescription] IS NULL AND @original_someDescription IS NULL))" 
        InsertCommand="INSERT INTO [someTable] ([someDescription]) VALUES (@someDescription)" 
        OldValuesParameterFormatString="original_{0}" 
        SelectCommand="SELECT * FROM [someTable]" 
        UpdateCommand="UPDATE [someTable] SET [someDescription] = @someDescription WHERE [someCode] = @original_someCode AND (([someDescription] = @original_someDescription) OR ([someDescription] IS NULL AND @original_someDescription IS NULL))">
        <DeleteParameters>
            <asp:Parameter Name="original_someCode" Type="Int32" />
            <asp:Parameter Name="original_someDescription" Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="someDescription" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="someDescription" Type="String" />
            <asp:Parameter Name="original_someCode" Type="Int32" />
            <asp:Parameter Name="original_someDescription" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

看起来 GridView 很好地绑定了数据库的表。但是,我在使用乐观并发时遇到了问题。然后我既不能删除没有描述的行,也不能在编辑模式下更新那些行。

GridView 在浏览器(IE9、Chrome 18)中看起来不错,但是当我尝试删除没有描述的行时,它不起作用。当我单击无描述行之一的编辑时,我有机会输入一个新值,但是,当我单击更新时,什么也没有发生。

编辑/删除填充了一些描述的行没有问题。使用乐观并发时,编辑和删除也适用于空描述。

GridView出现故障正常吗?有解决方法吗?

GridView 模板处于编辑或删除行模式时是否可以访问其验证工具?

4

1 回答 1

0

我没有在你上面的代码中看到OnRowDeleting="GridView1_RowDeleting",OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating"事件。为了在 GridView 中添加编辑/更新和删除,'<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />'您必须使用这些事件。

像这样的东西:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
  GridView1.EditIndex = e.NewEditIndex;
  BindData();
}

访问此链接:添加、编辑、更新、删除 gridview

于 2012-04-20T07:43:51.273 回答