用asp.net 的 DataSource 如何操作数据库
发布网友
发布时间:2022-04-27 12:07
我来回答
共1个回答
热心网友
时间:2022-04-27 13:36
DataGrid 有相应的事件的,不过我使用的是vs.net2005,没有DataGrid 了,我把gridview的告诉你吧,你可以仿照处理一下。
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//当使用系统提供的命令字段时,会默认调用相应的事件
GridView1.EditIndex = e.NewEditIndex;
BindDataRss();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindDataRss();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//更新事件,需要将最新的数据保存到资料库中
DateTime updateDate;
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
string url = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
//string createDate1 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
string createDate = ((Calendar)(GridView1.Rows[e.RowIndex].Cells[4].Controls[1])).SelectedDate.ToShortDateString();
if (createDate == "")
{
updateDate = DateTime.Now;
}
else
{
updateDate = Convert.ToDateTime(createDate);
}
//Response.Write(ID + NAME + RUL + createDate);
//更新方法:
if (UpdateDataRss(ID, name, url, updateDate) > 0)
{
Response.Write("<script>alert('更新成功');</script>");
GridView1.EditIndex = -1;
BindDataRss();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//当使用cmdfiled时,就需要默认调用这个事件的.根据ID调用方法删除数据库
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
//删除方法:
if(DeleteDataRss(ID))
{
Response.Write("<script>alert('删除成功');</script>");
GridView1.EditIndex = -1;
BindDataRss();
}
}
参考资料:我的网站