【发布时间】:2014-11-19 17:11:06
【问题描述】:
我有使用datalist 的表单并使用checkboxes 完成。如下所示
<asp:DataList ID="DataListTest" runat="server" OnPreRender="PreTes">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Label ID="lblHeader" runat="server"></asp:Label> <!-- Telephone, Bussines System -->
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblsubheader" runat="server" /></td> <!-- Oracle, EPM, CRM (Muncul di Form) -->
</tr>
<tr>
<td>
<asp:HiddenField ID="idJenisAccess" runat="server" Value='<%# Eval("id_jenis_access") %>' />
<asp:HiddenField ID="subhd" runat="server" Value='<%# Eval("sub_jenis") %>' /> <!-- Oracle, EPM, CRM (nama sub jenis) -->
<asp:HiddenField ID="id_access" runat="server" Value='<%# Eval("id_access") %>' /><!-- Output = ACT5, ACBS1 -->
<asp:HiddenField ID="hd" runat="server" Value='<%# Eval("nama_jenis") %>' /><!-- Output = Telephone, Bussines System -->
<asp:HiddenField ID="hdstatus" runat="server" Value='<%# Eval("status") %>' /> <!-- Output = 1 or 0 -->
</td>
</tr>
<tr>
<td>
<asp:CheckBox ID="cbCountryName" runat="server" Text='<%# Eval("nama_access") %>' /> <!-- Output = Local, Handphone, Project -->
<asp:TextBox ID="testme" runat="server" Text="" Visible="false" /> <!-- Output = Textbox for all -->
</td>
</tr>
<tr>
<td><asp:Label ID="lblReason" runat="server" Visible="false" text="Reason : "/>
<asp:TextBox ID="txtReason" runat="server" Visible="false" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblDescription" runat="server" Visible="false" text="Description : "/>
<asp:TextBox ID="txtDescription" runat="server" Text="" Visible="false" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
表格后面的代码,从数据库中获取数据如下所示。
private void ShowDataList()
{
conn.Open();
string sql = "Select access.id_access as 'id_access', access.status as 'status' , access.nama_access, jenis_access.id_jenis_access as '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.nama_jenis_access";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
dt = new DataTable();
adp.Fill(dt);
DataListTest.DataSource = dt;
DataListTest.DataBind();
}
这是我的PreRender Datalist,我称之为PreTest。将数据显示为checkboxes也是我的代码。
protected void PreTes(object sender, EventArgs e)
{
string temp = "";
string subtemp ="";
foreach (DataListItem item in DataListTest.Items)
{
Label objLabel = item.FindControl("lblHeader") as Label; /* Output = Telepon, Business System (nama jenis access) */
Label subjenis = item.FindControl("lblsubheader") as Label; /* Output = Oracle, CRM, EPM (nama sub jenis access) */
Label lblreason = (Label)item.FindControl("lblReason"); /* Output = Reason : (label reaseon) */
Label lbldescription = (Label)item.FindControl("lblDescription"); /* Output = Description : (label Description) */
TextBox txtOther = item.FindControl("testme") as TextBox; /* Output = textbox except reason and description */
TextBox txtReason = item.FindControl("txtReason") as TextBox; /* Output = textbox on Reason */
TextBox txtDescription = item.FindControl("txtDescription") as TextBox; /* Output = textbox on Description */
CheckBox objName = item.FindControl("cbCountryName") as CheckBox; /* Output = Local, Internet, Email (nama access) */
HiddenField objHD = item.FindControl("hd") as HiddenField; /* Output = Telpon, Business System (if deleted, nama jenis will be show up every single access name) */
HiddenField subobjHD = item.FindControl("subhd") as HiddenField;
HiddenField id_access = (HiddenField)item.FindControl("id_access");
HiddenField id_jenis = (HiddenField)item.FindControl("idJenisAccess");
HiddenField status = (HiddenField)item.FindControl("hdstatus");
if (temp != objHD.Value)
{
if (objHD.Value=="Others" && id_access.Value == "ACT5")
{
lblreason.Visible = true;
txtReason.Visible = true;
lbldescription.Visible = true;
txtDescription.Visible = true;
}
temp = objHD.Value;
objLabel.Text = temp + "<br/>";
}
if (subtemp != subobjHD.Value)
{
subtemp = subobjHD.Value;
subjenis.Text = subtemp+"<br/>";
}
if (status.Value == "1")
{
txtOther.Visible = true;
}
}
}
毕竟,我想将表单中的数据保存到数据库中。我的意思是,在用户检查了一些复选框之后。我想全部插入数据库。这是我插入数据的代码
private void InsertActivationDetail()
{
//Mengambil nilai idActivation
int MaxActivationId = GetGenerateActivationID();
//
foreach (DataListItem objitem in DataListTest.Items)
{
/* To get data from the form , then insert into database */
CheckBox cbCountryName = (CheckBox)objitem.FindControl("cbCountryName");
HiddenField id_access = (HiddenField)objitem.FindControl("id_access");
HiddenField id_jenis_access = (HiddenField)objitem.FindControl("idJenisAccess");
TextBox txtOther = objitem.FindControl("testme") as TextBox;
TextBox txtreason = objitem.FindControl("txtReason") as TextBox;
TextBox txtdescription = objitem.FindControl("txtDescription") as TextBox;
/* Get data end, then Inserting the data (below) */
/*Check if datalist is null or not*/
if (objitem !=null)
{
/*if not nul, then check the checkboxes, is null or not*/
if (cbCountryName.Checked == true)
{
/*if not null, then insert the data */
conn.Open();
sql = "INSERT INTO detil_activation (id_activation_access, id_jenis_access, id_access, " +
"keterangan, reason, description) " +
"VALUES ('" +
MaxActivationId + "', '"+ id_jenis_access.Value +"', '" + id_access.Value + "','"+
txtOther.Text +"','"+txtreason.Text+"','"+txtdescription.Text+"')";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
/* Inserting data is done */
}
}
}
}
我的问题是,
如何将用户选择的所有checkboxes 发送到另一个网络表单?
例子:
当用户选中一些复选框并单击提交时,我会将所有在 checkbox 上选中的数据(如 id_access 、 id_jenis 和文本框 (ID ="testme") 上的文本)插入到数据库中。
当用户单击按钮提交和单击提交按钮时,所有过程都将发生,我希望选中 checkboxes 的所有数据都显示在另一页上(页面名称 =“SendingActivation”)。
like "你选择:id_access id_jenis (enter) id_access id_jenis (enter) id_access id_jenis (和选择的数据一样多)。
仅供参考:我是这里的新手,也是 c# 编程,所以请逐步帮助我
【问题讨论】:
-
你想要其他页面上的所有数据,比如复选框和文本框的值,还是只需要选中的复选框?
-
可以根据id_jenis_access和id_access从数据库中获取数据吗?
-
是的,我可以得到 id_jenis_acces 和 id_access 的数据基础,我想展示它。示例:用户检查 2 个类别中的 3 个访问权限(jenis_access 的名称)假设有 2 个类别。互联网和电话。互联网类别有 3 种访问权限,比如 VPN、电子邮件、电子申请。类别电话有 2 种访问权限,比如本地、本地。在类别 Internet 上,用户检查电子邮件和类别电话都已检查。在第 2 页应该“你选择电子邮件,本地,跨本地”这样。明白吗?
标签: c# sql asp.net checkbox datalist