2012-04-20 105 views
0

我使用SQL Server 2008 R2的快遞,C#ASP.NET 4空行/刪除

我有SQL服務器上的表與此T-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] 

我也有含有一個gridview用的ConnectionString上述表Default.aspx文件,包括編輯和刪除選項(模板) - 無代碼後面:

 <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)中看起來很好,但是當我嘗試刪除沒有描述的行時,它不起作用。當我點擊其中一個無描述行的編輯時,我有機會輸入一個新值,但是,當我單擊更新沒有任何反應時。

沒有問題編輯/刪除填充了someDescription的行。當不是使用樂觀併發編輯和刪除工作也爲空描述。

GridView出現故障是否正常?有沒有解決方法?

在GridView模板處於編輯或刪除行模式時,是否可以訪問GridView模板的驗證工具?

回答

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(); 
} 

訪問此鏈接:Add, Edit, update, Delete gridview

+0

應該沒有代碼工作落後。事實上,它不是在使用樂觀併發模式。爲什麼在不使用樂觀併發時一切正常?或者真正的問題,爲什麼GridView在這種模式下功能不全?請記住,在someDescription字段中存在一些文本的情況下,編輯/刪除將以此模式運行。 – Different111222 2012-04-20 08:43:31