如何将选中的复选框保存到数据库中? [英] how to save check box selected items to Database?
本文介绍了如何将选中的复选框保存到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< asp:DataList ID dlFav " runat 服务器" RepeatColumns 4"
=" 垂直" ondeletecommand =" >
< ItemTemplate >
< 字体 =" 红色" < > asp:Label ID lblUserId" runat =" 服务器" 文本 " )%> ' > < /asp:Label > < br / >
< 字体 =" 红色" < ">> < asp:Label ID =" lblProductID" runat =" 服务器" 文本 " )%> ' > < /asp:Label > < br / >
< 字体 =" 红色" < ">> < asp:Label ID =" lblProductName" runat =" 服务器" 文本 " )%> ' > < /asp:Label > < br / >>
< 字体 =" 红色" < ">> < asp:Label ID =" runat =" 服务器" 文本 " 价格")> < /asp:Label > < br / >
< 字体 =" 红色" < > asp:Image ID ImageUrl" runat =" 服务器" ImageUrl " )%> ' > < /asp:Image >
< br >
< asp:CheckBox ID =" runat 服务器" 文本 选择"
=" 选择" / >
< br >
< /ItemTemplate >
< /asp:DataList >
< 表格 > ;
< tr >
< td >
< asp:Button ID =" runat 服务器" 文本 添加" / < /td >
< /tr >
< /table >
这是我的源代码.在复选框中单击我想向数据库中添加数据."可以解决任何一个问题..在要将复选框选中并单击添加"按钮时,我想向数据库中添加乘法数据.我想将数据保存到数据库中. .
解决方案
访问此链接
http://stackoverflow.com/questions/919514/save-checkbox-目标值:单击时的值到数据库 [ ^ ]
布尔数据;
如果(CheckBox1.Checked) { 数据=真; } 其他 数据=假; 您可以保存在数据库中的// 数据值您可以通过在gridview的rowdatabound中定义复选框,然后将复选框的值传递给查询来简单地做到这一点.
CheckBox chk1 =(CheckBox)GridView1.FooterRow.FindControl(" ); CheckBox chk2 =(CheckBox)GridView1.FooterRow.FindControl(" ); 字符串 chk1,chk2; 如果((chk1.Checked == true )&&(chk2.Checked == 是)) { chk1 = " ; chk2 = " ; } 其他 { 如果(chk1.已检查== true ) { chk1 = " ; chk2 = " ; } 其他 如果(chk2.Checked == 真) { chk1 = " ; chk2 = " ; } }
并将这些值用于您的插入查询
用于复选框插入
选择强制转换(' "+ chk1 +"' + ' ,' + " + chk2 +' varchar ( 100 )) as db_column 来自表名
<asp:DataList ID="dlFav" runat="server" RepeatColumns="4"
RepeatDirection="Vertical" ondeletecommand="dlFav_DeleteCommand">
<ItemTemplate>
<font color="red">Added User:</font><asp:Label ID="lblUserId" runat="server" Text='<%# Eval("AddedUserID") %>'></asp:Label><br />
<font color="red">ProductID:</font><asp:Label ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label><br />
<font color="red">ProductName:</font><asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label><br />
<font color="red">Price:</font><asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Label><br />
<font color="red">Image Url:</font><asp:Image ID="ImageUrl" runat="server" ImageUrl='<%#Eval("ImageUrl") %>'></asp:Image>
<br />
<asp:CheckBox ID="chkDl" runat="server" Text="Select"
CommandName="Select" />
<br />
</ItemTemplate>
</asp:DataList>
<table>
<tr>
<td>
<asp:Button ID="btnadd" runat="server" Text="Add" />
</td>
</tr>
</table>
hi this is my source code.in checkbox click i want to add data to database.,.can any one solve..i want to add multiply data to database while selcting checkbox and while clicking add button i want to save data to database..
解决方案
Visit This Link
http://stackoverflow.com/questions/919514/save-checkbox-value-to-a-database-when-clicked[^]
bool data;
if (CheckBox1.Checked) { data=True; } else data=False; //data value u can save in database
you can simply do it by defining your checkbox in rowdatabound of gridview and then pass the checkbox value to query.
CheckBox chk1 = (CheckBox)GridView1.FooterRow.FindControl("chk1"); CheckBox chk2 = (CheckBox)GridView1.FooterRow.FindControl("chk2"); string chk1,chk2; if ((chk1.Checked == true) && (chk2.Checked == true)) { chk1 = "1"; chk2 = "1"; } else { if (chk1 .Checked == true) { chk1 = "1"; chk2= "0"; } else if (chk2.Checked == true) { chk1 = "0"; chk2= "1"; } }
and use these values into ur insert query
for checkbox insertion
select cast('" + chk1+ "' + ',' + '" + chk2+ "' as varchar(100)) as db_column from tablename
这篇关于如何将选中的复选框保存到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文