iT邦幫忙

0

[ASP.NET]GridView裡的 checkbox 怎麼做(template)

  • 分享至 

  • xImage
  •  

目標:撈值到grid,再讀取打勾的row
https://ithelp.ithome.com.tw/upload/images/20240221/20106764gb9nn0Jrf3.png

add GridView
新增checkbox 轉成template
https://ithelp.ithome.com.tw/upload/images/20240221/20106764aORh59794n.png

塞資料

 protected void BtnQry_Click(object sender, EventArgs e)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("t1");
            dt.Columns.Add("t2");
            dt.Columns.Add("t3");

            dt.Rows.Add();
            dt.Rows[0][0] = "test0";
            dt.Rows[0][1] = "test1";
            dt.Rows[0][2] = "test2";

            dt.Rows.Add();
            dt.Rows[1][0] = "test10";
            dt.Rows[1][1] = "test11";
            dt.Rows[1][2] = "test12";

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

讀讀看

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            string xx = "";
           
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                 CheckBox cb  = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); ;//拿check
                if (cb.Checked)
                {                
                    xx = GridView1.Rows[i].Cells[1].Text;//拿cell裡的值
                    Response.Write(i.ToString()+xx+"--");
                }
            }
        }

試一下設定chekbox

        protected void BtnSelAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); ; 
                cb.Checked = true;                                
            } 
        }
        protected void BtnCancelSelAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); ;
                cb.Checked = false;
            }
        }

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言