您的位置:首页 > 其它

GridView基本的操作 编辑 更新 取消 删除

2011-11-22 16:09 726 查看
1.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ST_Admin_proman : ST_DataBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["admin"] != null)
{
Response.Redirect("ST_contraller.aspx?cname=noadmin");
}
if (!IsPostBack)
{
ShowProdut();
}
}

protected void ShowProdut()
{
DataSet ds = new DataSet();
string st_sql = "SELECT *  FROM ST_tProduct order by ST_ID desc";
ds = GetDataSet(st_sql, "Product");
GridView1.DataSource = ds;
GridView1.DataBind();

}

protected void Grid_Del(object sender, GridViewDeleteEventArgs e)
{
int index = e.RowIndex;
GridViewRow row = GridView1.Rows[index];
string row_cell = row.Cells[0].Text.Trim();
string st_sql = "delete FROM ST_tProduct Where ST_ID='" + row_cell + "'";
try
{
Exesql(st_sql);
ShowMsg.Text = "删除产品成功";
ShowMsg.Style["color"] = "Green";

}
catch (Exception ex)
{
ShowMsg.Text = "删除产品失败,原因:" + ex.Message;
ShowMsg.Style["color"] = "red";
}
finally
{
ShowProdut();
}

}
protected void Grid_Change(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
ShowProdut();
}

protected void Grid_Edit(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
ShowProdut();
}

protected void Grid_Cancel(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
ShowProdut();
}

protected void Grid_Updating(object sender, GridViewUpdateEventArgs e)
{
int index = e.RowIndex;
GridViewRow row = GridView1.Rows[index];

string st_sql;
st_sql = "update ST_tProduct set st_productname= '" + ((TextBox)(row.Cells[1].Controls[0])).Text + "',st_productprice= '" + ((TextBox)(row.Cells[2].Controls[0])).Text + "',st_productpic='" + ((TextBox)(row.Cells[3].Controls[0])).Text + "' where ST_ID='" + row.Cells[0].Text.Trim() + "'";
try
{
Exesql(st_sql);
GridView1.EditIndex = -1;
ShowMsg.Text = "更新产品成功" + st_sql;
ShowMsg.Style["color"] = "Green";

}
catch (Exception ex)
{
ShowMsg.Text = "更新产品失败,原因:" + ex.Message;
ShowMsg.Style["color"] = "red";
}
finally
{
ShowProdut();
}

}
}

2.html



<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:600px">
<table>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AllowPaging="true" AutoGenerateColumns="false"
OnRowDeleting="Grid_Del" OnPageIndexChanging="Grid_Change" OnRowEditing="Grid_Edit" OnRowUpdating="Grid_Updating" OnRowCancelingEdit="Grid_Cancel"
PagerSettings-Mode="NextPreviousFirstLast" PagerSettings-NextPageText="下一页" PagerSettings-PreviousPageText="上一页" Width="600">
<Columns>
<asp:BoundField DataField="st_id" HeaderText="产品编号" ReadOnly="true" />
<asp:BoundField DataField="st_productname" HeaderText="产品名称" />
<asp:BoundField DataField="st_productprice" HeaderText="产品价格" />
<asp:BoundField DataField="st_productpic" HeaderText="产品图片" />
<asp:BoundField DataField="st_productclass" HeaderText="产品类型" ReadOnly="true" />
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowCancelButton="true"  HeaderText="修改"  />
<asp:ButtonField Text="删除" ButtonType="Button" HeaderText="删除" CommandName="delete" />

</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td><asp:Label ID="ShowMsg" runat="server"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: