三层架构gridview控件中更新的代码
急急急!请各位帮帮忙!
protected void Page_Load(object sender, EventArgs e)
{
this.Bind();
}
private void Bind()
{
string sql = "select * from login order by id asc";
Global.DBAccess.SqlString = sql;
Global.DBAccess.TableName = "login";
this.GridView1.DataSource = Global.DBAccess.getDataTable();
this.GridView1.DataBind();
}
这种是绑定,如果你指的是更新按钮的话
给GridView修改RowCommand事件
而前端
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Button3" CommandName="Add" runat="server" Text="添加" />
<asp:LinkButton ID="Button1" CommandName="Edit" runat="server" Text="修改" />
<asp:LinkButton ID="Button2" CommandName="Delete" OnClientClick="return confirm('确定要删除这个用户吗?');" runat="server" Text="删除" />
</ItemTemplate>
</asp:TemplateField>
GtidView增加模板列
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//try
//{
if (Page.IsPostBack)
{
switch ()
{
case "Add":
Response.Redirect("UserEdit.aspx?Action=Add", true);
//Server.Execute("UserList.aspx?Action=Add");
break;
case "Edit":
GridViewRow drv = ((GridViewRow)(((LinkButton)()).Parent.Parent));//获得选中行的索引值
Response.Redirect("UserEdit.aspx?Action=Edit&ID=" + this.GridView1.Rows[drv.RowIndex].Cells[0].Text, true);
//Server.Execute("UserList.aspx?Action=Add&ID=" + this.GridView1.Rows[drv.RowIndex].Cells[0].Text);
break;
case "Delete":
//这里是删除代码
drv = ((GridViewRow)(((LinkButton)()).Parent.Parent));
login user = new login();
user.Id = int.Parse(this.GridView1.Rows[drv.RowIndex].Cells[0].Text);
user = user.LoadUserById();
if (user == null)
{
Response.Write("<script>alert('该用户已经被删除');</script>");
return;
}
user.deleteUser();
Response.Write("<script>alert('执行成功,请刷新本页看结果');</script>");
break;
}
}