我正在尝试创建一个网格视图表,在该表中,我希望从复选框列表中显示一些选定的数据。THe问题是,每当我从复选框列表中选择一些数据时,网格视图将只显示顶部复选框中的数据,而不是绑定到列表中每个框的多个不同数据,即使我有多个复选框显示数据?
选中新框时,Det gridview确实会添加新行,但是新行会从上一行复制数据。
网格视图和复选框列表都连接到SQLdatabase,其中数据来自:
码
public partial class Visual : System.Web.UI.Page
{
String con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
public void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dt.Columns.Add("SensorID");
dt.Columns.Add("BatteryLife");
dt.Columns.Add("YearInUsage");
dt.Columns.Add("NumberOfUsage");
dt.Columns.Add("Occupations");
dt.Columns.Add("Placement");
dt.Columns.Add("Zip");
dt.Columns.Add("City");
List<DataListe> dl = new List<DataListe>();
foreach (ListItem item in CheckBoxList1.Items)
{
SqlConnection sc = new SqlConnection(con);
sc.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Statistic WHERE SensorID='" + CheckBoxList1.SelectedValue + "'", sc);
SqlDataReader reader = cmd.ExecuteReader();
if (item.Selected)
{
while (reader.Read())
{
DataListe dali = new DataListe();
string si = (string)reader["SensorID"];
dali.SensorID = si;
string bl = (string)reader["BatteryLife"];
dali.BatteryLife = bl;
string yu = (string)reader["YearInUsage"];
dali.YearInUsage = yu;
int nu = (int)reader["NumberOfUsage"];
dali.NumberOfUsage = nu;
string oc = (string)reader["Occupations"];
dali.Occupations = oc;
string pl = (string)reader["Placement"];
dali.Placement = pl;
int zi = (int)reader["Zip"];
dali.Zip = zi;
string ci = (string)reader["City"];
dali.City = ci;
dl.Add(dali);
}
}
sc.Close();
}
GridView1.DataSourceID = null;
GridView1.DataSource = dl;
GridView1.DataBind();
return;
}我希望在选中复选框列表中的新框时,网格视图会添加一个新行,并保持前一行不变。该行应包含与SQLdatabase提供的复选框列表中的项相关联的信息。
但是,当选中一个新框时,网格视图确实会添加一个新行,但是它会从已经显示的行中复制数据。
发布于 2019-06-21 12:54:19
代码中的问题是使用CheckboxList1.SelectedValue.SelectedValue of a CheckBoxList应该是什么?您认为无论这个值是什么,在遍历Item集合时,它都会改变吗?
您需要使用Item.Value代替
// Opening the connection just one time before starting the loop
using(SqlConnection sc = new SqlConnection(con))
{
sc.Open();
string cmdText = "SELECT * FROM Statistic WHERE SensorID=@id";
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
// Moved the actual query inside the check for the item.Selected
// so you are calling the db only for the items that you want to use
SqlCommand cmd = new SqlCommand(cmdText, sc);
cmd.Parameters.Add("@id", SqlDbType.NVarChar).Value = item.Value;
using(SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
DataListe dali = new DataListe();
dali.SensorID = (string)reader["SensorID"];
dali.BatteryLife = (string)reader["BatteryLife"];
dali.YearInUsage = (string)reader["YearInUsage"];
dali.NumberOfUsage = (int)reader["NumberOfUsage"];
dali.Occupations = (string)reader["Occupations"];
dali.Placement = (string)reader["Placement"];
dali.Zip = (int)reader["Zip"];
dali.City = (string)reader["City"];
dl.Add(dali);
}
} // At this point the reader is closed and disposed by the ending using block
}
} // At this point the connection is closed and disposed by the ending using block其他要注意的是:当你不再需要连接和读取器这样的一次性对象时,应该将它们处理掉。using语句确保即使在例外情况下也是如此。另一个非常重要的问题是当您想要将查询文本传递给数据库引擎时使用参数。将字符串连接在一起是一种发现Sql注入和可能的解析错误的麻烦的可靠方法。
发布于 2019-06-21 12:30:38
您的问题是您的代码是选中的索引更改事件复选框。每次检查另一个条目时,这段代码就会触发。由于在数据集运行之前没有清除数据集,所以它会遍历整个复选框列表并添加任何选中的项。所以,当你检查第一个,它把它放在数据集中。检查第二个,它将第一个和第二个放在数据集中,等等。
将代码移至复选框离开事件或丢失焦点事件。只有当你离开控制区时它才会开火。
https://stackoverflow.com/questions/56703362
复制相似问题