日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

关于GridView手动绑定的一段代码,一切尽在不言中

發布時間:2023/11/27 生活经验 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于GridView手动绑定的一段代码,一切尽在不言中 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?????? 為GridView綁定主鍵的方法,在前臺的DataGrid標簽中加?? DataKeyNames="ID"???

后臺獲取ID:? int? id=int.parse(this.GridView.DataKeys[e.RowIndex].Value.Tostring());

如果DataKeyNames綁定了多個列取法:int? id=int.parse(this.GridView.DataKeys[e.RowIndex].Value["ID"].Tostring());

?

*****注意GridView在執行添刪改時如果是提交性質按鈕一定會觸發GridView_RowCommand( , )事件

?? ? ? ? ? ?在此事件下通過e.CommandName屬性便可知道是按了添刪改的那個按鈕

??下面是GridView實現全選和取消全選的功能實現:

?? ? ? ? ? ??為GridView1添加一個模板列,給其頭模板添<HeaderTemplate>加一個Html控件CheckBox,添加

?? onClick事件代碼如下

?

?? ? ?<HeaderTemplate>
?? ? ? <input id="chkSelectAll" type="checkbox" value="全選" οnclick="SelectAll(this)" />
?? ? </HeaderTemplate>

?? ? ? ? 給其項模板<ItemTemplate>添加服務端控件CheckBox。在頁面的頭標簽<head>中添加如下JS方法實現:??

?? ? ? ?<head runat="server">
?? ? ? ? ?<title>無標題頁</title>
?? ? ? ? ?<script type="text/javascript">
?? ? ? ? ? ? function SelectAll(oCheck)
?? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? var oTable = document.getElementById("GridView1");
?? ? ? ? ? ? ? ? if (oTable)
?? ? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? var oInputs = oTable.getElementsByTagName("input");
?? ? ? ? ? ? ? ? ? ? for (var i = 0; i < oInputs.length; i++)
?? ? ? ? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ? ?if (oInputs[i].type == "checkbox")
?? ? ? ? ? ? ? ? ? ? ? ? ? ?{
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? oInputs[i].checked = oCheck.checked;
?? ? ? ? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ? ? ?}
?? ? ? ? ? ? ?}
?? ? ? ? ? </script>
?? ? ? ? </head>

?

?

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

?

public partial class GridViewDemo_Default3 : System.Web.UI.Page

{

?? ?protected void Page_Load(object sender, EventArgs e)

?? ?{

?? ? ? ?if (!Page.IsPostBack)

?? ? ? ?{

?? ? ? ? ? ?ShowCategories();

?? ? ? ? ? ?ShowProducts();

?? ? ? ?}

?? ?}

?

?? ?private void ShowProducts()

?? ?{

?? ? ? ?DataTable dt = NorthWind.DBHelp.GetTable("SELECT Product.產品ID, Product.產品名稱, Supply.公司名稱, Supply.城市, Category.類別名稱, Category.圖片, Product.單位數量, Product.單價, Product.庫存量, Product.中止 FROM Category INNER JOIN Product ON Category.類別ID = Product.類別ID INNER JOIN Supply ON Product.供應商ID = Supply.供應商ID where Product.類別ID="+int.Parse(this.DropDownList1.SelectedValue));

?? ? ? ?this.GridView1.DataSource = dt;

?? ? ? ?this.GridView1.DataKeyNames = new string[]{"產品ID"};//設置數據操作的主鍵列

?? ? ? ?this.GridView1.DataBind();

?? ?}

?

?? ?private void ShowCategories()

?? ?{

?? ? ? ?DataTable dt = NorthWind.DBHelp.GetTable("select * from Category");

?? ? ? ?this.DropDownList1.DataSource = dt;

?? ? ? ?this.DropDownList1.DataTextField = "類別名稱";

?? ? ? ?this.DropDownList1.DataValueField = "類別ID";

?? ? ? ?this.DropDownList1.DataBind();

?? ?}

?? ?protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

?? ?{

?? ? ? ?ShowProducts();

?? ?}

?? ?protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

?? ?{

?

?? ?}

?? ?protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

?? ?{

?? ? ? ?//將當前要編輯的行的索引告訴GridView

?? ? ? ?this.GridView1.EditIndex = e.NewEditIndex;

?? ? ? ?ShowProducts();

?? ?}

?? ?protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

?? ?{

?? ? ? ?//更新數據庫

?? ? ? ?decimal price = decimal.Parse(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")).Text);

?? ? ? ?int productid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());//獲取當前行的主鍵

?? ? ? ?//更新到數據庫

?? ? ? ?NorthWind.DBHelp.ExecuteNoQuery("update Product set 單價="+price+" where 產品ID="+productid);

?? ? ? ?//重新綁定

?? ? ? ?this.GridView1.EditIndex = -1;

?? ? ? ?ShowProducts();

?? ?}

?? ?protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

?? ?{

?? ? ? ?this.GridView1.EditIndex = -1;

?? ? ? ?ShowProducts();

?? ?}

?? ?protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

?? ?{

?? ? ? ?//刪除數據

?? ? ? ?int productid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());//獲取當前行的主鍵

?? ? ? ?//更新到數據庫

?? ? ? ?NorthWind.DBHelp.ExecuteNoQuery("delete from Product where 產品ID=" + productid);

?? ? ? ?//重新綁定

?? ? ? ?this.GridView1.EditIndex = -1;

?? ? ? ?ShowProducts();

?? ?}

?? ?protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

?? ?{

?? ? ? ?//當我們對數據源中的每一行進行數據綁定時觸發的事件

?? ? ? ?if (e.Row.RowType == DataControlRowType.DataRow)//判斷綁定時候該行的狀態時數據行還是其他類型的模板

?? ? ? ?{

?? ? ? ?//如果要把ID列綁到GridView中為連續不斷的ID,可先添加綁定列ID,取消其DataFiled的綁定,在該事件加下面代碼:

?? ? ? ? ? ? ?//e.Row.Cells[0].Text=(e.Row.RowIndex+1).ToString();

?? ? ? ? ? ?//添加客戶端確認

?? ? ? ? ? ?LinkButton lnkDelete = (LinkButton)e.Row.Cells[10].FindControl("lnkDelete");

?? ? ? ? ? ?lnkDelete.Attributes.Add("onclick", "return confirm('你確認刪除嗎?')");

?? ? ? ? ? ?//光棒效果 和 ?鼠標小手效果

?? ? ? ? ? ?e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#ff66cc';this.style.cursor='pointer';");

?? ? ? ? ? ?e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;this.style.cursor='default';");

?? ? ? ?}

?? ?}

?? ?protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)

?? ?{

?? ? ? ?//獲取全選的復選框

?? ? ? ?CheckBox chkSelectAll = (CheckBox)sender;

?? ? ? ?//獲取每一行的復選框

?? ? ? ?for (int i = 0; i < this.GridView1.Rows.Count; i++)

?? ? ? ?{

?? ? ? ? ? ?CheckBox chkSelect = (CheckBox)this.GridView1.Rows[i].Cells[11].FindControl("chkSelect");

?? ? ? ? ? ?//修改狀態

?? ? ? ? ? ?chkSelect.Checked = chkSelectAll.Checked;

?? ? ? ?}

?? ?}

?? ?protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

?? ?{

?? ? ? ?//告訴GridView當前顯示的數據是第幾頁的數據

?? ? ? ?this.GridView1.PageIndex= e.NewPageIndex;

?? ? ? ?ShowProducts();

?? ?}

}

轉載于:https://www.cnblogs.com/yingger/archive/2012/04/21/2462454.html

總結

以上是生活随笔為你收集整理的关于GridView手动绑定的一段代码,一切尽在不言中的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。