我在使用 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();
}
}
- 我应该把 UpdateMethod 代码放在哪里(目前在 Page_Load 中)?
- 我应该在后面的代码中实现哪些事件,以便我编辑的网格行使用正确的 NewValues 调用 UpdateMethod?
- 当我单击编辑链接时,它不会在编辑模式下呈现 GridView(文本框等);我必须单击它两次才能这样做;为什么?
- 大问题:单击更新链接不会调用 ObjectDataSource UpdateMethod?