[英]ASP.NET Configure update button in GridView
我在VS2005上使用C#ASP.NET。
我有一個gridview表,但是當我右鍵單擊Smart選項卡時,它沒有選擇Enable Editing 。
因此,我使用以下代碼手動添加了編輯按鈕:
AutoGenerateEditButton="True"
編輯按鈕已成功出現在我的gridview上,如下所示:
當我單擊“ 編輯”按鈕時,頁面將刷新,並且該行現在可以編輯:
但是,當我按下更新按鈕時,我遇到了錯誤:
Updating is not supported by data source 'SqlDataSource1' unless UpdateCommand is specified.
http://i.stack.imgur.com/W97K0.png
我不知道如何輸入或配置UpdateCommand
因為我沒有看到Update
按鈕的任何后台代碼。
需要經驗豐富的幫助。 先感謝您。
編輯:在SqlDataSource1
添加了INSERT查詢,但是當我按下“ Update
按鈕時仍然遇到了同樣的錯誤。
您需要重新配置SqlDataSource1
控件,您可以在其中添加對INSERT
, DELETE
, UPDATE
以及SELECT
。
看一下本教程。
在為gridview配置select語句時配置sqldatasource,有一個選項為“advanced”。單擊該選項,然后單擊“generate update,insert nad delete statements”。
例如,試試這個......
首先創建一個處理更新記錄的方法。
private void UpdateOrAddNewRecord(string parametervalue1, string parametervalue2) { using (openconn()) { string sqlStatement = string.Empty; sqlStatement = "Update TableName set Name1 =@Name1 where Name2@Name2"; try { // SqlCommand cmd = new SqlCommand("storedprocedureName", con); //cmd.CommandType = CommandType.StoredProcedure; SqlCommand cmd = new SqlCommand(sqlStatement, con); cmd.Parameters.AddWithValue("Name2", parametervalue2); cmd.Parameters.AddWithValue("@Name1",parametervalue1); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert/Update Error:"; msg += ex.Message; throw new Exception(msg); } finally { closeconn(); } }
現在創建行更新方法..
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string ParameterValue1 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text; string ParameterValue2 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text; //Name UpdateOrAddNewRecord(ParameterValue1, ParameterValue2); // call update method GridView1.EditIndex = -1; BindGridView(); Label2.Visible = true; Label2.Text = "Row Updated"; }
創建行取消事件..
protected void GridView1_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e){GridView1.EditIndex = -1; // swicth回到默認模式BindGridView(); }
創建行編輯...
protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e){GridView1.EditIndex = e.NewEditIndex; BindGridView(); }
還有很多其他方式以不同的方式進行同樣的活動。 這是最基本的方式。 無論如何,如果你發現它有用,請將其標記為你的答案,否則讓我知道......
In your code I think you have not handled the event for "Update".
Have a look at the below example hope it might help you,
check for the "UpdateCommand".
Also write a Update event in C# to update.
<asp:DetailsView ID="ManageProducts" runat="server" AllowPaging="True"
AutoGenerateRows="False" DataKeyNames="ProductID"
DataSourceID="ManageProductsDataSource" EnableViewState="False">
<Fields>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued"
SortExpression="Discontinued" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="ManageProductsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
DeleteCommand=
"DELETE FROM [Products] WHERE [ProductID] = @ProductID"
InsertCommand=
"INSERT INTO [Products] ([ProductName], [UnitPrice], [Discontinued])
VALUES (@ProductName, @UnitPrice, @Discontinued)"
SelectCommand=
"SELECT [ProductID], [ProductName], [UnitPrice], [Discontinued]
FROM [Products]"
UpdateCommand=
"UPDATE [Products] SET [ProductName] = @ProductName,
[UnitPrice] = @UnitPrice, [Discontinued] = @Discontinued
WHERE [ProductID] = @ProductID">
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="UnitPrice" Type="Decimal" />
<asp:Parameter Name="Discontinued" Type="Boolean" />
<asp:Parameter Name="ProductID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="UnitPrice" Type="Decimal" />
<asp:Parameter Name="Discontinued" Type="Boolean" />
</InsertParameters>
</asp:SqlDataSource>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.