如何根据复选框ID所代表的SQL Server列名填充复选框ID
本文关键字:ID 复选框 Server SQL 填充 何根 | 更新日期: 2023-09-27 18:11:17
SQL Server表:
Name Type1 Type2 Type3 Type4
-----------------------------------------------------
Lance X X X
John X X
Mike X X X
ASP.net: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Auto Check" OnClick="Button1_Click" />
<asp:CheckBox ID="Type1" runat="server" /> Type1
<asp:CheckBox ID="Type2" runat="server" /> Type2
<asp:CheckBox ID="Type3" runat="server" /> Type3
<asp:CheckBox ID="Type4" runat="server" /> Type4
c#: public void Button1_Click(object sender, EventArgs e)
{
string strQuery = @"SELECT * FROM [Db1].[dbo].[Table1] ";
strTxt = txtName.Text;
strQuery += " WHERE [Name] = '" + strTxt + "'";
using (SqlConnection sc = new SqlConnection(gloString))
{
try
{
sc.Open();
SqlCommand scd = new SqlCommand(strQuery);
scd.Connection = sc;
SqlDataReader sdr = new SqlDataReader();
sdr = scd.ExecuteReader();
while (sdr.Read())
{
//use a foreach by column and check them?
}
}
catch (SqlException)
{
}
finally
{
sc.Close();
}
}
}
我知道我可以使用if语句来选中相应列的任何框,并使用X
。
相反,我如何创建一个foreach
或for
语句,它采用列名并填充对应于列名的复选框ID,所以我不必使用很多if
语句。
试试这个:
for (int i = 0; i < sdr.FieldCount; i++)
{
bool val = Convert.ToBoolean(dr[i]);
string colName = sdr.GetName(i);
if (colName != "Name" && val)
{
CheckBox myChkBox = (CheckBox)Page.FindControl(colName);
myChkBox.checked = val;
}
}
在要绑定的列的复选框上添加数据绑定,这样更快。
checkboxtype1.DataBindings.Add("Checked",Bindingsource~whatever source,"ColumnName");
如果存储为1或0您将需要这个:
Binding bind = new Binding("Checked", bindingSource5, "Type1");
bind.Format += (s,e) => {
e.Value = (int)e.Value == 1;
};
CheckBoxType1.DataBindings.Add(bind);
或:
//Retrieve column schema into a DataTable.
schemaTable = myReader.GetSchemaTable();
//For each field in the table...
foreach (DataRow myField in schemaTable.Rows){
//For each property of the field...
foreach (DataColumn myProperty in schemaTable.Columns)
{
CheckboxTyp1.Checked = (bool) myField[myProperty]);
}
CheckboxType1是您的checkboxID,然后绑定简单地绑定到列名。类型1如果你有四个复选框CheckboxType1, CheckboxType2, CheckboxType3, CheckboxType4然后将每个绑定到相应的列,如果你在数据网格中有一个对象列表,并且你的复选框在网格中,那么有一种不同的方式来绑定它,但类似。
将4个复选框绑定到您的结果:
checkboxtype1.DataBindings.Add("Checked",Bindingsource~whatever source,"ColumnNameType1");
checkboxtype2.DataBindings.Add("Checked",Bindingsource~whatever source,"ColumnNameType2");
checkboxtype3.DataBindings.Add("Checked",Bindingsource~whatever source,"ColumnNameType3");
等等。从你的代码问题,它看起来像你想要一行数据。name Type1 Type2 Type3 Type4
checkboxtype1可以是CheckBox1或ChkBillyBob,其名称无关。
[MyCheckBox].DataBindings.Add("Checked" ~ where Checked is a Property of the Control as a string name - Checkbox in this case has property Checked.
[MyCheckBox].DataBindings.Add("Checked", BindingSource ~ where binding source is your reader ; you should not need to iterate it unless you are expecting more than one result back. the while(sdr.Read()) can go just use the DataReader returned.
[MyCheckBox].DataBindings.Add("Checked",Bindingsource~whatever source,"ColumnNameType1" ~ ColumnNameType1 is the name of your column as a string hence it is in quotes'.
就像我说的,如果你有一个对象列表,它的工作原理有点不同,但非常相似。