如何将选定的复选框文本保存到数据库中
假设我们有10个复选框,然后选择了5个.我希望将5个chechbox文本存储在字符串中,并以(,)分隔,然后将该字符串传递给sqlquery以在数据库中插入.....请帮助我..... .... thanx
suppose we have 10 checkbox thn 5 is selected.i want that 5 chechbox text store in string by seperated (,) thn that string pass to sqlquery for insertiojn in database.....please help me.........thanx
在这里,我尝试了一些符合您要求的代码.请尝试一次...
Hi,
Here I tried some code for your requirement.try this once...
<asp:checkboxlist id="CheckBoxList1" runat="server" xmlns:asp="#unknown">
<asp:listitem text=" checkbox1" value=" checkbox1"></asp:listitem>
<asp:listitem text=" checkbox2" value=" checkbox2"></asp:listitem>
<asp:listitem text=" checkbox3" value=" checkbox3"></asp:listitem>
<asp:listitem text=" checkbox4" value=" checkbox4"></asp:listitem>
<asp:listitem text=" checkbox5" value=" checkbox5"></asp:listitem>
<asp:listitem text=" checkbox6" value=" checkbox6"></asp:listitem>
<asp:listitem text=" checkbox7" value=" checkbox7"></asp:listitem>
<asp:listitem text=" checkbox8" value=" checkbox8"></asp:listitem>
<asp:listitem text=" checkbox9" value=" checkbox9"></asp:listitem>
<asp:listitem text=" checkbox10" value=" checkbox10"></asp:listitem>
</asp:checkboxlist><br />
<asp:button id="Button2" runat="server" text="submit" onclick="Button2_Click" xmlns:asp="#unknown" /><br />
<div id="resdiv" runat="server"></div>
文件后面的代码包含以下代码
And the code behind file contains following code
protected void Button2_Click(object sender, EventArgs e)
{
string str = string.Empty;
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
if (str.Length > 0)
{
str = str + "," + CheckBoxList1.Items[i].Text;
}
else
{
str = str + CheckBoxList1.Items[i].Text;
}
}
}
resdiv.InnerHtml = "The Result String is :" + str;
}
在这里,您可以使用该str变量将其存储到数据库中.
希望您知道如何将该值存储在数据库中
最好的
Here you can use that str variable to store into database.
I hope you know how to store that value in database
All the Best
真的是个坏主意.不要保存UI.具有单独的数据层和与UI的数据绑定.不要将字符串与some一起保存,而要保存逻辑数据.特别是,将10个2状态复选框映射到10位位集,该位集最好用16位整数(数据库类型"SMALLINT")表示,每个复选框1位,未使用6位.
如果复选框集中的每个位具有不同的语义,请在由enum
类型表示的位集上创建一个映射,并为每个成员使用语义名称.在C#中将如下所示:
Really bad idea. Don''t save UI. Have a separate data layer and data binding with UI. Don''t save strings with some , save logical data. In particular, 10 2-state check boxes maps to a 10-bit bit set, which is best represented with 16-bit integer (database type "SMALLINT"), one bit per check box, 6 bits remain unused.
If each bit in check box set makes separate semantic sense, create a mapping on a bit set represented by anenum
type with semantic names for each member. In C# it will look like:
enum OptionSet : System.Int16 {
None = 0,
CaseSensitive = 1 << 0, // 1
Backward = 1 << 1, // 2
UseRegEx = 1 << 2, // 4
Selection = 1 << 3, // 8
//...
}
将其存储在强制转换为SMALLINT
的dababase类型中,将其大小写回OptionSet
以进行语义操作(业务逻辑),并使用循环和位操作逐位映射到复选框集.我相信您知道如何设置,清除和测试一下...
Store this thing in dababase type casted to SMALLINT
, type case it back to OptionSet
for semantic operation (business logic), map to the set of check boxes bit by bit using cycle and bit operations. I trust you know how to set, clear and test a bit…