问题
i have datalist filled by checkbox. the code like this
the form
<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>
the code to get data from database
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();
}
the code to show data from database into datalist and show it as 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/>";
}
}
}
the code for insert into 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();
}
}
}
my question is "How to insert id and name(text and value) from checkbox into database sql server"
i think the problem is around function InsertActivationDetail(). because i have red underline on it. Would you mind to help me please ?
FYI = i'm a newbie in here also programmer.
回答1:
Try this. Modified your Method a little. Your are not getting the CheckBox within your DataList.
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();
}
}
}
}
}
来源:https://stackoverflow.com/questions/25904150/how-to-insert-text-and-value-from-checkbox-in-datalist-into-sql-server