前言
自己在學習.NET中常犯的錯誤(持續更新)
下拉框綁值
public void ddlist() { this.DropDownList1.DataTextField = "DeviceName"; this.DropDownList1.DataValueField = "DeviceID"; this.DropDownList1.DataSource = dbl.ddlist(); this.DropDownList1.DataBind(); this.DropDownList1.Items.Insert(0, new ListItem("全部", "0")); }
this.DropDownList1.DataTextField = “DeviceName”;
DataTextField :顯示給用戶看的數據庫列
DataValueField:綁定數據源等于綁定唯一標識列
DataSource:數據源,綁定sql語言可以顯示數據
DataBind:綁定數據才可以顯示出來,是一個函數
Items.Insert(0, new ListItem(“全部”, “0”));
ltems表示集合
insert()兩個參數 (int index,Ltems item)
最后效果:
綁值GridView
public void jiaz() { this.GridView1.DataSource = dbl.show(); this.GridView1.DataBind(); }
DataSource:數據源,綁定sql語言可以顯示數據
DataBind:綁定數據才可以顯示出來,是一個函數
最后效果:
刪除數據
- 點擊刪除
CommandAgument和CommandName 配合一起使用,一般習慣用于刪除
會在RowCommand事件執行
先綁定ID
再綁定CommandName
進入Rowcommand事件里面
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName=="del") { int id = int.Parse(e.CommandArgument.ToString()); bool b1 = dbl.del(id); if (b1) { Response.Write("<script>alert("刪除成功")</script>"); jiaz(); } else { Response.Write("<script>alert("刪除失敗")</script>"); } } }
e.CommandName=="del"
RowCommand無論怎么樣都會先來這里,所以判斷一下是不是要執行刪除操作,根據e.CommandName==“del”
int id = int.Parse(e.CommandArgument.ToString());
刪除執行的SQL語句是要根據ID唯一標識列來進行有目標的
修改
- 點擊修改
修改CommandName為update,為了激發updateing事件
綁定ID,這里是鍵值對
為什么不綁定commandAgument呢,因為上面說了commandAgument,是要去Rowcommand事件配合使用的,我們把CommandName修改成為update,是要去Rowupdating事件
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int id = int.Parse(this.GridView1.DataKeys[e.RowIndex]["DeviceID"].ToString()); bool b1 = dbl.up(id); if (b1) { Response.Write("<script>alert("修改成功")</script>"); jiaz(); } else { Response.Write("<script>alert("修改失敗")</script>"); } }
int id = int.Parse(this.GridView1.DataKeys[e.RowIndex][“DeviceID”].ToString());
修改SQL語句也是要獲取修改的唯一標識列
這個GridView1的DataKeys
[e.RowIndex] [“DeviceID”]:當前行的數據庫標識列
修改賦值到另外一個頁面
Session["ID"] = this.GridView1.DataKeys[e.RowIndex]["BookID"].ToString(); Label Booksname = (Label)this.GridView1.Rows[e. RowIndex].FindControl("Label2"); Session["BookName"] = Booksname.Text; Response.Redirect("add.aspx");
Session[“ID”] = this.GridView1.DataKeys[e.RowIndex][“BookID”].ToString();
找到的ID賦值給session
Label Booksname = (Label)this.GridView1.Rows[e. RowIndex].FindControl(“Label2”);
找當前行的Label2控件
Session[“BookName”] = Booksname.Text;
把找到控件的值文本傳給session
Lable是類型,看Gridview是什么控件就轉換為什么類型
FindControl(找控件)
修改賦值到另外一個頁面綁定值
文本框綁定值
this.TextBox2.Text = Session["BookName"].ToString();
下拉框綁定值
if (Session["BookiS"].ToString().Contains("是")) { this.DropDownList1.SelectedIndex = 0; } else { this.DropDownList1.SelectedIndex = 1; }
判斷session里面是否包含這個值
this.DropDownList1.SelectedIndex = 0;
SelectedIndex = 0 代表 展示的是第一個
換頁不報錯
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GridView1.PageIndex = e.NewPageIndex; //換頁不報錯 jiazGridview(); }
Gridview 換頁不報錯
到此這篇關于ASP.NET學習中常見錯誤總結歸納的文章就介紹到這了,更多相關ASP.NET 常見錯誤內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_46874327/article/details/116932707