如何从表中捕获数据(动态到数据库)

时间:2014-09-24 03:40:35

标签: c# sql checkbox

我有动态表到数据库并通过复选框实现。 这就是我的HTML:

<table width="800px" style="margin-right: 42px" border="0">   
<tr>
<asp:Table CssClass="left" ID="left" runat="server" GridLines="Horizontal" />              
<asp:Table CssClass="right" ID="right" runat="server" GridLines="Horizontal" />
<asp:Table CssClass="other" ID="other" runat="server" GridLines="Horizontal"></asp:Table>
</tr>
</table>

这是我的代码,用于显示或从数据库中获取数据,也可以动态地将表打印到数据库(tr和td)。

private void ShowDataList()
{
    conn.Open();
    string sql = "Select access.id_access as 'id_access',access.status, access.nama_access, jenis_access.id_jenis_access as 'idJenisAccess' , " +
                 "jenis_access.nama_jenis_access as 'nama_jenis', sub_jenis.nama_sub_jenis as 'sub_jenis',sub_jenis.id_sub_jenis "+
                 "FROM access LEFT JOIN detil_access ON access.id_access = detil_access.id_access "+
                 "LEFT JOIN jenis_access ON detil_access.id_jenis_access = jenis_access.id_jenis_access "+
                 "LEFT JOIN sub_jenis ON detil_access.id_sub_jenis = sub_jenis.id_sub_jenis "+
                 "ORDER BY jenis_access.id_jenis_access";
    SqlCommand cmd = new SqlCommand(sql, conn);
    SqlDataReader dr = cmd.ExecuteReader();
    TableRow tr = new TableRow();
    TableCell tc = new TableCell();
    string jenis = string.Empty;
    string subjenis = string.Empty;       
    while (dr.Read())
    {
        if (jenis == string.Empty || dr["nama_jenis"].ToString() != jenis)
        {
            tc.CssClass = "cb";
            jenis = dr["nama_jenis"].ToString();
            tr = new TableRow(); //cetak <tr> kosongan
            tc = new TableCell(); // cetak <td> kosongan
            tc.ColumnSpan = 3; // menggabungkan 3 kolom jadi 1
            tc.Text = dr["nama_jenis"].ToString();
            tr.Cells.Add(tc); // masukin <td> ke <tr>               
            if (dr["nama_jenis"].ToString() == "Business System")
            {
                right.Rows.Add(tr); // jika nama jenis == Business System maka taruh di tabel kanan
            }
            else if (dr["nama_jenis"].ToString() == "Others")
            {
                other.Rows.Add(tr);// jika nama jenis == Other taruh di table other                    
            }
            else
            {
                left.Rows.Add(tr); // sisanya kiri
            }
        }
        if ((subjenis == string.Empty || subjenis != dr["sub_jenis"].ToString()) && dr["nama_jenis"].ToString() == "Business System") // sub jenisnya
        {
            subjenis = dr["sub_jenis"].ToString(); 
            tr = new TableRow();
            tc = new TableCell();
            tc.ColumnSpan = 3;
            tc.Text = dr["sub_jenis"].ToString();
            tr.Cells.Add(tc);
            right.Rows.Add(tr);
        }

        // bikin kolom untuk check box nya
        tr = new TableRow();
        tc = new TableCell();            
        CheckBox cb = new CheckBox();
        tc.CssClass = "cb";
        //cb.AutoPostBack = true;
        cb.CheckedChanged += new EventHandler(this.CheckBox1_CheckedChanged);
        cb.ID = dr["id_access"].ToString() + "," + dr["id_sub_jenis"].ToString();
        tc.Controls.Add(cb);
        tr.Cells.Add(tc); // satu <td> di masukin ke <tr> (td yang 1)

        //bikin kolom untuk nama aksesnya
        tc = new TableCell();
        tc.Text = dr["nama_access"].ToString();

        if (dr["status"].ToString() == "1") // kalo status 1 maka langsung di add, tapi kalo bukan 1 di colspan
        {
            tr.Cells.Add(tc);
            tc = new TableCell();
            TextBox tb = new TextBox();
            tb.ID = dr["id_access"].ToString() + "," + dr["id_sub_jenis"].ToString();
            tc.Controls.Add(tb); // satu <td> dimasukin ke <tr> (td yang ke 2)
            tr.Cells.Add(tc);
            tr.CssClass = "cb";
            //satu <td> di masukin ke <tr> (td yang ke 3) --> makanya di form sebelah kiri bisa ada 3 <td> dlm 1 <tr>
            //1 <tr> = 1 baris.
            // 1 baris ada 3 kolom.
            //beda baris maka = new <tr>
        }
        else
        {
            tc.ColumnSpan = 2; // di colspan (untuk yang gak punya teksbox)
            tr.Cells.Add(tc);
        }
        if (dr["nama_jenis"].ToString() == "Business System")
        {
            right.Rows.Add(tr);
        }
        else if (dr["nama_jenis"].ToString() == "Others")
        {
            other.Rows.Add(tr);
        }
        else
        {
            left.Rows.Add(tr);
        }
    }
    conn.Close();
}

这是复选框代码。

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox cbs = (CheckBox)sender;        
    if (cbs.Checked)
    {
        bt.Add(cbs.ID);
    }
    else
    {
        bt.RemoveAll(p => p == cbs.Text);
    }
}

我的问题是,如果我的表格如此,如何获取表格中的值或数据? 例如,我想捕获已选中复选框的ID。 如果我选中复选框,那么我必须抓住它的ID;每个复选框都有2个id:id_access和idJenisAccess。如果我选中2个复选框,则必须是4个id。所以,我的意思是如何从我的桌子上抓住身份证? (我更喜欢做一个新功能)

1 个答案:

答案 0 :(得分:0)

我假设在表格中填入数据nad复选框后,您会在按钮点击时获得复选框的ID,而不是尝试此

foreach (TableRow tr in left.Rows)
    {
        foreach (TableCell tc in tr.Cells)
        {
            foreach (Control ctrl in tc.Controls)
            {
                CheckBox chk = ctrl as CheckBox;
                if (chk != null)
                {
                    if (chk.Checked)
                    {
                        string id = chk.ID;//Checkbox id
                        string text = chk.Text;//Checkbox Text
                        YourSaveMethodHere();
                    }
                }
            }


        }
    }

如果你想获得所需的结果后只想获得复选框而不是中断循环。