我知道这是非常基本的,几乎是基本的,很难找到相关的例子。
我有一个从数据库查询的gridview表,并且使用VS17我正在尝试添加一个编辑列。我使用设计器布局生成了以下代码,但是当我单击编辑按钮时,该行变为可编辑但更新语句不会更新数据库。单击更新后,页面将刷新,表格将消失。
我哪里错了?另外,我知道我的前任在列名中使用的拼写错误。
这是cs文件:
<%@ Control Language="C#" AutoEventWireup="true" Inherits="CMSWebParts_Custom_Intranet_Data_inventorycountsheetcategorydatagridedit" CodeFile="~/CMSWebParts/Custom/Intranet/Data/inventorycountsheetcategorydatagridedit.ascx.cs" %>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="PartCatagoriesID" DataMember="DefaultView" DataSourceID="SqlDataSource1" EnableModelValidation="False">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="CategroyDescription" HeaderText="CategroyDescription" SortExpression="CategroyDescription" />
<asp:BoundField DataField="PartCatagoriesID" HeaderText="PartCatagoriesID" InsertVisible="False" ReadOnly="True" SortExpression="PartCatagoriesID" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Form_IntranetSharpTransit_PartCategories] WHERE [PartCatagoriesID] = ?" EnableViewState="False" InsertCommand="INSERT INTO [Form_IntranetSharpTransit_PartCategories] ([CategoryName], [CategroyDescription], [PartCatagoriesID]) VALUES (?, ?, ?)" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT [CategoryName], [CategroyDescription], [PartCatagoriesID] FROM [Form_IntranetSharpTransit_PartCategories]" UpdateCommand="UPDATE [Form_IntranetSharpTransit_PartCategories] SET [CategoryName] = ?, [CategroyDescription] = ? WHERE [PartCatagoriesID] = ?">
<DeleteParameters>
<asp:Parameter Name="PartCatagoriesID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="CategroyDescription" Type="String" />
<asp:Parameter Name="PartCatagoriesID" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="CategroyDescription" Type="String" />
<asp:Parameter Name="PartCatagoriesID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
这是ascx文件:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.PortalControls;
using CMS.GlobalHelper;
using CMS.TreeEngine;
using CMS.CMSHelper;
using CMS.ExtendedControls;
public partial class CMSWebParts_Custom_Intranet_Data_inventorycountsheetcategorydatagridedit: CMSAbstractWebPart
{
/// <summary>
/// Enables or disables resolving of inline controls.
/// </summary>
public bool ResolveDynamicControls
{
get
{
return ValidationHelper.GetBoolean(this.GetValue("ResolveDynamicControls"), true);
}
set
{
this.SetValue("ResolveDynamicControls", value);
}
}
/// <summary>
/// Enables or disables HTML encoding of text.
/// </summary>
public bool EncodeText
{
get
{
return ValidationHelper.GetBoolean(this.GetValue("EncodeText"), false);
}
set
{
this.SetValue("EncodeText", value);
}
}
/// <summary>
/// Content loaded event handler.
/// </summary>
public override void OnContentLoaded()
{
base.OnContentLoaded();
SetupControl();
}
/// <summary>
/// Initializes the control properties.
/// </summary>
protected void SetupControl()
{
if (this.StopProcessing)
{
// Do not process
}
else
{
}
}
/// <summary>
/// Reloads the control data.
/// </summary>
public override void ReloadData()
{
base.ReloadData();
SetupControl();
}
}
非常基本,我知道,但这不是我的强项,我似乎无法找到适用的示例或文档。任何帮助将不胜感激!
答案 0 :(得分:0)
您可能需要将以下属性添加到网格视图中,然后在后面的代码中实现它们。如果您使用VS17,它将帮助您做到这一点。在网格视图中,首先输入OnRowEditing
,OnRowUpdating
。这应该让你开始。您可能也想要实现OnRowCancelingEdit
方法。