[英]How to insert text and value from checkbox in datalist into sql server
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 插入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" 我的问题是“如何从复选框将ID和名称(文本和值)插入数据库SQL Server”
i think the problem is around function InsertActivationDetail(). 我认为问题出在函数InsertActivationDetail()周围。 because i have red underline on it.
因为我上面有红色下划线。 Would you mind to help me please ?
你介意帮我吗?
FYI = i'm a newbie in here also programmer. 仅供参考=我也是程序员的新手。
Try this. 尝试这个。 Modified your Method a little.
稍微修改了您的方法。 Your are not getting the CheckBox within your 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();
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.