繁体   English   中英

如何从数据列表中的复选框将文本和值插入SQL Server

[英]How to insert text and value from checkbox in datalist into sql server

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

表格

 <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();
}

将数据库中的数据显示到数据列表中并将其显示为复选框的代码

 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()周围。 因为我上面有红色下划线。 你介意帮我吗?

仅供参考=我也是程序员的新手。

尝试这个。 稍微修改了您的方法。 您没有在数据列表中获取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();
                }
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM