iT邦幫忙

第 12 屆 iThome 鐵人賽

0
自我挑戰組

回顧再出發~系列 第 11

ASP.NET C# - GridView -ButtonFiled & CommandName

  • 分享至 

  • xImage
  •  

環境
VS2013
ASP.NET 4.0

GridView怎麼做刪除鈕與編輯鈕.
要做之前,我們要先拿到點下去的是哪一列.
也要知道點到的是刪除鈕還是編輯鈕.
https://ithelp.ithome.com.tw/upload/images/20201208/20106764c4XgxEMIiZ.png
https://ithelp.ithome.com.tw/upload/images/20201208/20106764FBwD0ERjUv.png

GridView 新增資料行BoundField

HeaderText : ID
DataField : ID

GridView 新增資料行ButtonFiled

Button Type : Image
CommandName : xxx 這樣就知道點到的是哪一顆按鈕了.
HeaderText : xxx
ImageUrl :

GridView 新增資料行ButtonFiled

Button Type : Image
CommandName : 000 這樣就知道點到的是哪一顆按鈕了.
HeaderText : 000
ImageUrl :

GridView Event

RowCommd :

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {           
            if (e.CommandName == "xxx")
            {
                int index = Convert.ToInt32(e.CommandArgument);//get點到的那一列
                Button1.Text = "你按到xxx了:"+GridView1 .Rows[index].Cells[0].Text.ToString().Trim() ;//取點到那列的某個值.
            }
            if (e.CommandName == "000")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                Button1.Text = "你按到000了:" + GridView1.Rows[index].Cells[0].Text.ToString().Trim(); ;
            }
        }

GV Bind

        protected void Page_Load(object sender, EventArgs e)
        {
            List<OrderItem> dataSource = new OrderItemData().DataSource;
            this.GridView1.DataSource = dataSource;
            this.GridView1.DataBind(); 
        } 

準備測試資料

/// <summary>
/// 訂單項目資料
/// </summary>
public struct OrderItem
{
    /// <summary>
    /// 設定或取得訂單編號
    /// </summary>
    public Guid ID { set; get; }
    /// <summary>
    /// 設定或取得產品ID
    /// </summary>
    public Guid ItemID { set; get; }
    /// <summary>
    /// 設定或取得購買數量
    /// </summary>
    public int Count { set; get; }
    /// <summary>
    /// 設定或取得產品名稱
    /// </summary>
    public string ItemName { set; get; }
} 

/// <summary>
/// 處理訂單項目的資料類別
/// </summary>
public class OrderItemData
{
    /// <summary>
    /// 取得模擬資料來源
    /// </summary>
    public List<OrderItem> DataSource
    {
        get
        {
            Guid itemId_1 = Guid.NewGuid(), itemId_2 = Guid.NewGuid(), itemId_3 = Guid.NewGuid(), itemId_4 = Guid.NewGuid();//隨機建立四個產品Id

            List<OrderItem> result = new List<OrderItem>();//結果
            Guid orderId_1 = new Guid("CB843080-755C-4F42-AE2B-A79DE62F503E");//第一筆訂單編號
            result.Add(new OrderItem() { ID = orderId_1, ItemID = itemId_1, ItemName = "腳踏車", Count = 2 });
            result.Add(new OrderItem() { ID = orderId_1, ItemID = itemId_2, ItemName = "暖氣", Count = 5 });
            result.Add(new OrderItem() { ID = orderId_1, ItemID = itemId_3, ItemName = "肥皂", Count = 10 });

            Guid orderId_2 = new Guid("FE390D81-BDF9-49C8-BD26-C51043FB6F0C");//第二筆訂單編號
            result.Add(new OrderItem() { ID = orderId_2, ItemID = itemId_2, ItemName = "暖氣", Count = 1 });
            result.Add(new OrderItem() { ID = orderId_2, ItemID = itemId_3, ItemName = "肥皂", Count = 7 });
            result.Add(new OrderItem() { ID = orderId_2, ItemID = itemId_4, ItemName = "滑鼠", Count = 3 });

            return result;
        }
    }
}

ref
https://dotblogs.com.tw/antony77/2011/04/28/23806


上一篇
W10 無法存取Server2003網路磁碟
下一篇
LinkServer function 不允許遠端函數參考
系列文
回顧再出發~22
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言