string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.AllowSorting = true;
BindData();
SetGrid();
//ViewState["style"] = "0";
}
}
private void BindData()
{
SqlConnection MyCon = new SqlConnection(ConStr);
string QueryStr = "SELECT customerid,CompanyName,ContactName,Address FROM customers";
SqlDataAdapter Da = new SqlDataAdapter(QueryStr,MyCon);
DataSet Ds = new DataSet();
Da.Fill(Ds,"Customers");
GridView1.DataSource = Ds.Tables[0];
GridView1.DataKeyNames = new string []{"customerid"};
GridView1.DataBind();
}
private void SetGrid()
{
GridView1.AllowPaging = true;
//GridView1.PageSize = 15;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.EditRowStyle.BackColor = Color.Black;
BindData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow Row = GridView1.Rows[e.NewSelectedIndex];
Response.Write("<script>alert('你選擇了ID為" + Row.Cells[3].Text + "的行');</script>");
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
Response.Write("<script>alert('你切換到了第" + (GridView1.PageIndex+1) + "頁');</script>");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow Row = GridView1.SelectedRow;
Row.BackColor = Color.Crimson;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
//防止非法的輸入,預防腳本攻擊
string CustomerId = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString());
string CompanyName = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
string ContactName = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString());
string Address = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString());
SqlConnection Con = new SqlConnection(ConStr);
try
{
string UpdateStr = "UPDATE customers SET companyname='" + CompanyName + "',contactname='" + ContactName + "',address='" + Address + "' WHERE customerid='" + ID + "'";
SqlCommand Cmd = new SqlCommand(UpdateStr, Con);
//盡可能晚的打開連接,盡早的關閉連接
Con.Open();
Cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
BindData();
}
catch (Exception ex)
{
Response.Write("<script>alert('編輯出錯,請重新填寫');</script>");
GridView1.EditIndex = -1;
BindData();
}
//要及時的關閉打開的連接,提高程序的性能
finally
{
Con.Dispose();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string QueryStr = "DELETE FROM customers WHERE customerid='" + ID + "'";
SqlConnection Con = new SqlConnection(ConStr);
SqlCommand Cmd = new SqlCommand(QueryStr,Con);
try
{
Con.Open();
Cmd.ExecuteNonQuery();
BindData();
Response.Write("<script>alert('成功刪除');</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('刪除有誤,請檢查該表是否與其他表有約束');</script>");
}
finally
{
Con.Dispose();
}
}
//****************************************************************************************************************
//當它寫為“return confirm();”的時候,后邊的任何客戶端代碼都不可能執行,
//因此你注冊時設計處理不可能執行。有些所謂的“示例”代碼給你這樣寫的時候,你要注意,
//它應該並不為按鈕注冊事件處理方法(注冊了就很可笑了,因為根本無用),而是通過設置按鈕的CommandName來讓gridview處理。
//這種寫法下,按鈕僅僅是提供命令名稱和參數。
//如果你要讓后邊的代碼執行,應該寫:
//b.Attributes["onclick"] = "if(!confirm('你真的要刪除該條記錄么?'))return false;";
//*****************************************************************************************************************
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
//這種寫法不管你點擊的是什么,后面的代碼都不會執行。
//((Button)e.Row.Cells[2].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('確定要刪除"" + e.Row.Cells[3].Text + ""嗎?')");
//正確的寫法
((Button)e.Row.Cells[2].Controls[0]).Attributes["onclick"] = "if(!confirm('你真的要刪除" + e.Row.Cells[3].Text + "這條記錄么?'))return false;";
}
}