我遇到在CodeBehind上使用ObjectDataSource实现可编辑GridView的问题;我想知道我需要在我的代码后面添加哪些缺失的部分(事件):
ASPX代码:
<asp:GridView ID="grdPlayer" runat="server" AutoGenerateColumns="False"
AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
onrowediting="grdPlayer_RowEditing"
onrowcancelingedit="grdPlayer_RowCancelingEdit"
onrowupdating="grdPlayer_RowUpdating">
<Columns>
<asp:BoundField DataField="ID" Visible="false"/>
<asp:BoundField DataField="FirstName" HeaderText="First Name"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="Age" HeaderText="Age"
SortExpression="Age" />
</Columns>
</asp:GridView>
ASPX.CS代码背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
ObjectDataSource dataSource = new ObjectDataSource();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dataSource.TypeName = "Cricket.CricketBL";
dataSource.UpdateMethod = "UpdatePlayer";
Parameter p1 = new Parameter("ID", DbType.Int32);
Parameter p2 = new Parameter("FName", DbType.String);
Parameter p3 = new Parameter("LName", DbType.String);
Parameter p4 = new Parameter("Age", DbType.Int32);
dataSource.UpdateParameters.Add(p1);
dataSource.UpdateParameters.Add(p2);
dataSource.UpdateParameters.Add(p3);
dataSource.UpdateParameters.Add(p4);
bindGridView();
}
}
#region Grid Events
protected void grdPlayer_RowEditing(object sender, GridViewEditEventArgs e)
{
grdPlayer.EditIndex = e.NewEditIndex;
}
protected void grdPlayer_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
grdPlayer.EditIndex = -1;
bindGridView();
}
protected void grdPlayer_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdPlayer.EditIndex = -1;
bindGridView();
}
#endregion
void bindGridView()
{
dataSource.SelectMethod = "GetAllPlayer";
dataSource.TypeName = "Cricket.CricketBL";
grdPlayer.DataSource = dataSource;
grdPlayer.DataBind();
}
}
答案 0 :(得分:0)
Zee的,
您的方法仅适用于学习阶段。对于最佳实践或在生产阶段,我们应始终在应用程序的多个层之间进行明确分离。它们主要是数据访问层(DAL),业务逻辑层(BLL)和表示层(在Web页面中)。这种概念通常被称为持久性无知。正如here
所解释的那样调用存储库类和两个构造函数的CRUD方法可以将业务逻辑类与您选择的任何后端数据存储一起使用。业务逻辑类不需要知道它所调用的类如何持久化数据。 (这通常被称为持久性无知。)
我强烈建议您完成此awesome tutorial并继续深造,您将受到启发!
答案 1 :(得分:0)
Zee的,
回答你的4个问题。
1和4:
将“onRowUpdated”添加到.aspx文件中的gridview属性。比如onRowUpdated =“grdPlayer_RowUpdated”。添加您在此处更新的代码。
2和3:
为什么不使用TemplateField而不是使用BoundField。它将具有EditItemTemplate和ItemTemplate,如下所示:
<asp:TemplateField HeaderText="FirstName">
<EditItemTemplate>
<asp:TextBox ID="FName" runat="server" Text='<%# Bind("FirstName")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<EditItemTemplate>
<asp:TextBox ID="LName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<EditItemTemplate>
<asp:TextBox ID="Age" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
在“grdPlayer_RowUpdating”方法中,添加以下内容:
int index = grdPlayer.EditIndex;
GridViewRow row = grdPlayer.Rows[index];
Int64 ID = (Int64)grdPlayer.DataKeys[index].Value;
TextBox FirstName = (TextBox)row.FindControl("FName");
TextBox Lastname = (TextBox)row.FindControl("LName");
TextBox Age = (TextBox)row.FindControl("Age");
e.NewValues["FirstName"] = FirstName.Text;
e.NewValues["LastName"] = Lastname.Text;
e.NewValues["Age"] = Age.Text;
我希望这会有所帮助。