[英]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.