<br />
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="添加" />
<br />
<br />
</div>
</form>
</body>
</html>
default.aspx.cs頁面代碼:
代碼如下:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using GPS_Web.ywpages.DAL;
using System.Data;
namespace GPS_Web
{
/// <summary>
/// 參考的網(wǎng)址:http://blog.csdn.net/wanglei_samrtfish/article/details/8070480
/// </summary>
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridViewBind();
}
}
private void GridViewBind()
{
string sql = "select Num,TableName,TextName from dbo.GroupType_Demo";
try
{ //綁定數(shù)據(jù)源
GridView1.DataSource = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql).Tables[0];
GridView1.DataBind();
}
catch (Exception ex){}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{ //gridview編輯項索引等于單擊行的索引
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
/// <summary>
/// 修改事件
/// </summary>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{ //取出編號
int Num = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblNum")).Text.ToString());
//獲取修改的值的內(nèi)容
string TableName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTableName")).Text;
string TextName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTextName")).Text;
//更新記錄
string sql = string.Format("update dbo.GroupType_Demo set TableName='{0}',TextName='{1}' where Num={2}",TableName,TextName,Num);
try
{
int i = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql);
if (i > 0)
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('修改成功!')</script>");
}
else
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('修改失??!')</script>");
}
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('操作無效!')</script>");
}
GridView1.EditIndex = -1;
GridViewBind();
}
/// <summary>
/// 刪除事件
/// </summary>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//取出編號
int Num = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblNum")).Text.ToString());
//更新記錄
string sql = string.Format("delete dbo.GroupType_Demo where num={0}", Num);
try
{
int i = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql);
if (i > 0)
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('刪除成功!')</script>");
}
else
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('刪除失?。?)</script>");
}
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('操作無效!')</script>");
} www.gxlcms.com
GridView1.EditIndex = -1;
GridViewBind();
}
/// <summary>
/// 添加按鈕
/// </summary>
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("~/Default_Add.aspx");
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//行號
int i = e.NewSelectedIndex;
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
btnAdd.Text = "你選中了第" + (i+1) + "行。";
}
}
}
執(zhí)行后的頁面效果:
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com