GridView中的编辑和删除后台怎么写?我用的是ASP.编辑包括更新和取消,删除用来删除课程的。我要代码

 我来答
zhubin_wyn
2011-10-31 · TA获得超过175个赞
知道小有建树答主
回答量:216
采纳率:100%
帮助的人:259万
展开全部
很简单的啦,这样吧,我给你一个GridView增删改查的例子,你参考参考:
public UserInfoManager info = new UserInfoManager();

/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadInfo();
}
}

/// <summary>
/// 加载用户信息的方法
/// </summary>
private void LoadInfo()
{
List<UserInfo> users =new List<UserInfo>();
foreach (UserInfo user in info.GetAllUserInfo())
{
if (user.Gender == 1)
{
user.DepartId.sex = "男";
}
else
{
user.DepartId.sex = "女";
}
users.Add(user);
}
gdvUserInfo.DataSource =users;
gdvUserInfo.DataBind();
}

/// <summary>
/// 分页事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdvUserInfo.PageIndex = e.NewPageIndex;
LoadInfo();
}

/// <summary>
/// 删除信息事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id =gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();
if (info.DeleteUserInfoById(id) > 0)
{
this.lblMessage.Text = "删除成功!";
LoadInfo();
}
else
{
this.lblMessage.Text = "删除失败!";
LoadInfo();
}
}

/// <summary>
/// 编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvUserInfo.EditIndex = e.NewEditIndex;
LoadInfo();
}

/// <summary>
/// 取消编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gdvUserInfo.EditIndex = -1;
LoadInfo();
}

/// <summary>
/// 更新事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userId = gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();
TextBox txtUserId = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserId");
TextBox txtUserName = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserName");
TextBox txtPassWord = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtPassWord");
if (txtUserName != null && txtPassWord != null)
{
try
{
UserInfo user = info.GetUserInfoById(userId);
user.UserName = txtUserName.Text.ToString();
user.Password = txtPassWord.Text.ToString();
if (info.ModifyUserInfo(user) > 0)
{
this.lblMessage.Text = "修改成功!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
else
{
this.lblMessage.Text = "修改失败!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
}
catch (Exception)
{

this.lblMessage.Text = "修改失败!";
gdvUserInfo.EditIndex = -1;
LoadInfo();
}
}
}

/// <summary>
///
/// 光棒事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gdvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < gdvUserInfo.Rows.Count; i++)
{
if (gdvUserInfo.Rows[i].RowType == DataControlRowType.DataRow)
{
gdvUserInfo.Rows[i].Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#9cf'");
gdvUserInfo.Rows[i].Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
a409329879
推荐于2016-11-11 · 超过23用户采纳过TA的回答
知道答主
回答量:83
采纳率:0%
帮助的人:50.4万
展开全部
string myConnectionString = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
SqlConnection myConnection = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
myConnection.ConnectionString = myConnectionString;
if (!IsPostBack)
{
T();
}
}
public void T() //绑定
{
myConnection.Open();
string sql = "Select * From Test";
SqlDataAdapter da = new SqlDataAdapter(sql, myConnection);
DataSet ds = new DataSet();
da.Fill(ds, "Test");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
myConnection.Close();
}
public void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) //更新
{
myConnection.Open();
GridViewRow row = GridView1.Rows[e.RowIndex];
int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
string userName = ((TextBox)row.FindControl("UserName")).Text.ToString();
double Money = Convert.ToDouble(((TextBox)row.FindControl("Money")).Text.ToString());
SqlDataReader dr;
string sql = "Update Test Set UserName=‘" + userName + "‘,Money=" + Money + " Where ID=" + ID + "";
SqlCommand myCommand = new SqlCommand(sql, myConnection);
dr = myCommand.ExecuteReader();
GridView1.EditIndex = -1;
myConnection.Close();
T();
}
public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) //删除
{
myConnection.Open();
GridViewRow row = GridView1.Rows[e.RowIndex];
int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
SqlDataReader dr;
string sql = "Delete From Test Where ID=" + ID + "";
SqlCommand myCommand = new SqlCommand(sql, myConnection);
dr = myCommand.ExecuteReader();
GridView1.EditIndex = -1;
myConnection.Close();
T();
}
public void GridView1_RowEditing(object sender, GridViewEditEventArgs e) //编辑
{
GridView1.EditIndex = e.NewEditIndex;
T();
}
public void GridView1_CancelingEdit(object sender, GridViewCancelEditEventArgs e) //取消
{
GridView1.EditIndex = -1;
T();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) //分页
{
GridView1.PageIndex = e.NewPageIndex;
T();
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友22442ffa4
2011-10-31
知道答主
回答量:7
采纳率:0%
帮助的人:7.1万
展开全部
你的编辑是要直接在GridView编辑还是另外做的编辑页面?

GridView.DataKeys[e.RowIndex].Value 来获取主键
多个主键使用GridView.DataKeys[e.RowIndex].Values[0]这样来获取主键

其他的就不用多说了吧
记住不管是删除还是编辑以后,重新绑定GridView。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式