DropDownList在从数据库中得到数据源绑定后,计划为其添加一个"全部"或"不限"之类的项,添加方法现知的有两种:
1:在脚本中直接添加:<asp:ListItem Value="0">全部</asp:ListItem>,然后在DropDownList中添加一个属性: AppendDataBoundItems="True" ,如下面的代码所示:
<asp:DropDownList ID="ddlDepartment" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlDepartment_SelectedIndexChanged" DataTextField="DeptName"
AppendDataBoundItems="True" DataValueField="DeptID">
<asp:ListItem Value="0">全部</asp:ListItem>
</asp:DropDownList>
2:在后台代码中通过CS文件添加:
ddlDepartment.Items.Insert(0, new ListItem("--所有--", "0"));
******************************************
添加以后,在使用的时候,可以这样写:
If(ddlDepartment.SelectedValue!="0")
{
deptId=ddlDepartment.SelectedValue;
}
--SQL语句的写法:WHERE @deptId IS NULL OR @deptId="" OR deptID=@deptId
3、我的实践
SQLHelper sqlh = new SQLHelper();
protected void Page_Load(object sender, EventArgs e)
{
//大棚下拉列表数据填充绑定
if (!IsPostBack)
{
string dpsql = "SELECT * FROM [GHInfo]";
DataTable dpdt = new DataTable();
dpdt = sqlh.GetDataTable(dpsql);
dapeng.DataSource = dpdt;
dapeng.DataValueField = dpdt.Columns[0].ToString();
dapeng.DataTextField = dpdt.Columns[2].ToString();
dapeng.DataBind();
ListItem li = new ListItem("---查看全部---","00");
dapeng.Items.Insert(0, li );
string sql = "SELECT t1.ID, t2.UserName, t1.Address, t3.Name, t1.Tem, t1.Hum, t1.Co2 FROM GHInfo AS t1 LEFT OUTER JOIN UserInfo AS t2 ON t1.OwnerID = t2.ID LEFT OUTER JOIN Plants AS t3 ON t1.PlantID = t3.ID ";
DataTable dt = sqlh.GetDataTable(sql);
DataList1.DataSource = dt;
DataList1.DataBind();
protected void Page_Load(object sender, EventArgs e)
{
//大棚下拉列表数据填充绑定
if (!IsPostBack)
{
string dpsql = "SELECT * FROM [GHInfo]";
DataTable dpdt = new DataTable();
dpdt = sqlh.GetDataTable(dpsql);
dapeng.DataSource = dpdt;
dapeng.DataValueField = dpdt.Columns[0].ToString();
dapeng.DataTextField = dpdt.Columns[2].ToString();
dapeng.DataBind();
ListItem li = new ListItem("---查看全部---","00");
dapeng.Items.Insert(0, li );
string sql = "SELECT t1.ID, t2.UserName, t1.Address, t3.Name, t1.Tem, t1.Hum, t1.Co2 FROM GHInfo AS t1 LEFT OUTER JOIN UserInfo AS t2 ON t1.OwnerID = t2.ID LEFT OUTER JOIN Plants AS t3 ON t1.PlantID = t3.ID ";
DataTable dt = sqlh.GetDataTable(sql);
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
protected void dapeng_SelectedIndexChanged(object sender, EventArgs e)
{
{
string id = dapeng.SelectedValue.ToString();
string sql;
Label1.Text = id;
if (id != "00")
{
sql = "SELECT t1.ID, t2.UserName, t1.Address, t3.Name, t1.Tem, t1.Hum, t1.Co2 FROM GHInfo AS t1 LEFT OUTER JOIN UserInfo AS t2 ON t1.OwnerID = t2.ID LEFT OUTER JOIN Plants AS t3 ON t1.PlantID = t3.ID WHERE(t1.ID = '" + id + "')";
}
else
{
sql = "SELECT t1.ID, t2.UserName, t1.Address, t3.Name, t1.Tem, t1.Hum, t1.Co2 FROM GHInfo AS t1 LEFT OUTER JOIN UserInfo AS t2 ON t1.OwnerID = t2.ID LEFT OUTER JOIN Plants AS t3 ON t1.PlantID = t3.ID";
}
DataTable dt = sqlh.GetDataTable(sql);
DataList1.DataSource = dt;
string sql;
Label1.Text = id;
if (id != "00")
{
sql = "SELECT t1.ID, t2.UserName, t1.Address, t3.Name, t1.Tem, t1.Hum, t1.Co2 FROM GHInfo AS t1 LEFT OUTER JOIN UserInfo AS t2 ON t1.OwnerID = t2.ID LEFT OUTER JOIN Plants AS t3 ON t1.PlantID = t3.ID WHERE(t1.ID = '" + id + "')";
}
else
{
sql = "SELECT t1.ID, t2.UserName, t1.Address, t3.Name, t1.Tem, t1.Hum, t1.Co2 FROM GHInfo AS t1 LEFT OUTER JOIN UserInfo AS t2 ON t1.OwnerID = t2.ID LEFT OUTER JOIN Plants AS t3 ON t1.PlantID = t3.ID";
}
DataTable dt = sqlh.GetDataTable(sql);
DataList1.DataSource = dt;
DataList1.DataBind();
}
}