本文介绍了从基于数据库的复选框列表选择获取错误获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个复选框列表,其中一个复选框是手机,另一个是笔记本电脑。

如果我在手机上查看它将把所有手机从DB带到我的DAtList

如果我在笔记本电脑上查看它会将所有手机从DB带到我的DAtList

这里ID为代码

.aspx

< asp:CheckBoxList ID =CheckBoxList1runat =serverAutoPostBack =TrueOnSelectedIndexChanged =CheckBoxList1_SelectedIndexChanged>

< asp:ListItem Value =0> Mobiles

< asp:ListItem Value =1>笔记本电脑



.aspx.cs

protected void CheckBoxList1_SelectedIndexChanged(object发件人,EventArgs e)

{



List< string> GetList = new List< string>();

foreach(CheckBoxList1.Items中的ListItem项目)

{

if(item.Selected)

{

GetList.Add(item.Value);

}

}

string str =select * from ProductDetails where Producttype = @ textInput;

SqlCommand cmd = new SqlCommand(str,con);

cmd.Parameters.Add( new SqlParameter(@ textInput,0));

for(int d = 0; d< GetList.Count; d ++)

{

string text = GetList [d];

cmd.Parameters [@ textInput]。Value = text;

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

if(count == 0)

{



dc = ds.Clone();

count ++;

}

for(int i = 0;我< ds.Tables [0] .Rows.Count; i ++)

{

if(ds.Tables [0] .Rows [i] .ItemArray [0] .ToString()!=NULL)

dc.Tables [0] .ImportRow(ds.Tables [0] .Rows [i]);

}

DataList1.DataSource = ds。表[0] .DefaultView;

DataList1.DataBind();

}

}

}

Hi I have 1 checkbox list in that one check box is mobiles and another one is Laptops.
If I check on mobile It wil brings all mobile from DB to My DAtList
If I check on laptop It wil brings all mobile from DB to My DAtList
Here id the code
.aspx
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
<asp:ListItem Value="0">Mobiles
<asp:ListItem Value="1">Laptop

.aspx.cs
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{

List<string> GetList = new List<string>();
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
GetList.Add(item.Value);
}
}
string str = "select * from ProductDetails where Producttype=@textInput";
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.Add(new SqlParameter("@textInput", 0));
for (int d = 0; d < GetList.Count; d++)
{
string text = GetList[d];
cmd.Parameters["@textInput"].Value = text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (count == 0)
{

dc = ds.Clone();
count++;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i].ItemArray[0].ToString() != "NULL")
dc.Tables[0].ImportRow(ds.Tables[0].Rows[i]);
}
DataList1.DataSource = ds.Tables[0].DefaultView;
DataList1.DataBind();
}
}
}

推荐答案


这篇关于从基于数据库的复选框列表选择获取错误获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 16:37