如何将datalist中checkbox的text和value插入sql server

时间:2014-09-18 04:24:52

标签: c# asp.net sql-server checkbox datalist

我的复选框填充了数据列表。像这样的代码

表格

 <asp:DataList ID="DataListTest" runat="server" OnPreRender="PreTes">
        <ItemTemplate>
    <table cellpadding="0" cellspacing="0">
        <tr>
        <td>
        <asp:Label ID="lblHeader" runat="server"></asp:Label>                                        
        </td>

        </tr>
        <tr>
        <td><asp:Label ID="lblsubheader" runat="server" /></td>
        </tr>

        <tr>
        <td>
        <asp:HiddenField ID="subhd" runat="server" Value='<%# Eval("sub_category") %>' />
        </td>
        </tr>

        <tr>
        <td>
        <asp:CheckBox ID="cbNameAccess" runat="server"  Text='<%# Eval("name_access") %>' />
        <asp:HiddenField ID="hd" runat="server" Value='<%# Eval("name_category") %>' />
        </td>
        </tr>
    </table>
        </ItemTemplate>
    </asp:DataList>

从数据库中获取数据的代码

private void ShowDataList()
{
    conn.Open();
    string sql = "Select access.id_access, access.nama_access, jenis_access.id_jenis_access ,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);
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    dt = new DataTable();
    adp.Fill(dt);
    DataListTest.DataSource = dt;
    DataListTest.DataBind();
}

将数据库中的数据显示为datalist并将其显示为Checkbox

的代码
 protected void PreTes(object sender, EventArgs e)
{
    string temp = ""; 
    string subtemp ="";
    foreach (DataList item in DataListTest.Items)
    {
        Label objLabel = item.FindControl("lblHeader") as Label;
        Label subjenis = item.FindControl("lblsubheader") as Label;
        CheckBox objName = item.FindControl("cbCountryName") as CheckBox;
        HiddenField objHD = item.FindControl("hd") as HiddenField;
        HiddenField subobjHD = item.FindControl("subhd") as HiddenField;

        if (temp != objHD.Value)
        {                                    
             temp = objHD.Value;
             objLabel.Text = temp + "<br/>";                                   
        }
        if (subtemp != subobjHD.Value)
        {
            subtemp = subobjHD.Value;
            subjenis.Text = subtemp+"<br/>";
        }

    }
}

插入sql的代码

private void InsertActivationDetail()
{
    // get idActivation
    int MaxActivationId = GetGenerateActivationID();
    //

    foreach (DataList objitem in DataListTest.Items)
    {
        if (objitem.Selected)
        {
            conn.Open();
            sql = "INSERT INTO detil_activation (id_activation_access, id_jenis_access, id_access, " +
                  "others_all, others_nama_access, call_back_to, reason_any_number, description_other_jenis_access, " +
                  "reason_others_jenis_access) VALUES ('" +
                  MaxActivationId + "', (select id_jenis_access from access where id_access = '" +
                  objitem.Value + "'), '" + objitem.Value + "',NULL,NULL,NULL,NULL,NULL,NULL)";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
}

我的问题是“如何将复选框中的id和名称(文本和值)插入数据库sql server”

我认为问题在于函数InsertActivationDetail()。因为我有红色下划线。你介意帮我吗?

FYI =我是这里的新手也是程序员。

1 个答案:

答案 0 :(得分:0)

试试这个。稍微修改了你的方法。您没有在DataList中获取CheckBox。

private void InsertActivationDetail()
{
    // get idActivation
    int MaxActivationId = GetGenerateActivationID();
    //

    foreach (DataListItem objitem in DataListTest.Items)
    {
        CheckBox cbNameAccess = (CheckBox)objitem.FindControl("cbNameAccess");
            if (cbNameAccess != null)
            {
                if (cbNameAccess.Checked==true)
                {
                    conn.Open();
                    sql = "INSERT INTO detil_activation (id_activation_access, id_jenis_access, id_access, " +
                          "others_all, others_nama_access, call_back_to, reason_any_number, description_other_jenis_access, " +
                          "reason_others_jenis_access) VALUES ('" +
                          MaxActivationId + "', (select id_jenis_access from access where id_access = '" +
                          cbNameAccess.Text + "'), '" + cbNameAccess.Text + "',NULL,NULL,NULL,NULL,NULL,NULL)";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
        }
    }
}