Asp.Net中DropDownList的使用以及条件筛选

private void Init()
    {
        string sql = "select cbdisplayvalue,cbvalue from table where cbid=";
        int typecustomer = 4;
        int banzu = 5;
        int pinzhong = 1;
        string sql_customer = sql + typecustomer;
        string sql_banzu = sql + banzu;
        string sql_pinzhong = sql + pinzhong;
        DbCommand dcmd_customer = dbsqlserver.GetSqlStringCommand(sql_customer);
        this.DropDownList1.DataSource = dbsqlserver.ExecuteDataSet(dcmd_customer).Tables[0];
        this.DropDownList1.DataTextField = "cbdisplayvalue";
        this.DropDownList1.DataValueField = "cbvalue";
        this.DropDownList1.DataBind();
        DbCommand dcmd_banzu = dbsqlserver.GetSqlStringCommand(sql_banzu);
        this.DropDownList3.DataSource = dbsqlserver.ExecuteDataSet(dcmd_banzu).Tables[0];
        this.DropDownList3.DataTextField = "cbdisplayvalue";
        this.DropDownList3.DataValueField = "cbvalue";
        this.DropDownList3.DataBind();
        DbCommand dcmd_pinzhong = dbsqlserver.GetSqlStringCommand(sql_pinzhong);
        this.DropDownList2.DataSource = dbsqlserver.ExecuteDataSet(dcmd_pinzhong).Tables[0];
        this.DropDownList2.DataTextField = "cbdisplayvalue";
        this.DropDownList2.DataValueField = "cbvalue";
        this.DropDownList2.DataBind();
        this.DropDownList1.Items.Insert(0, new ListItem("--全部**--"));
        this.DropDownList2.Items.Insert(0, new ListItem("--全部**--"));
        this.DropDownList3.Items.Insert(0, new ListItem("--全部**--"));
    }

当在DropDownList选择是诸如“全部**”,“全部**”,“全部**”时候获取对应数据:

        string customername = this.DropDownList1.Text == "--全部**--" ? "null" : this.DropDownList1.SelectedValue.ToString().Trim();
        string banzu = this.DropDownList3.Text == "--全部**--" ? "null" : this.DropDownList3.SelectedValue.ToString().Trim();
        string pingzhong = this.DropDownList2.Text == "--全部**--" ? "null" : this.DropDownList2.SelectedItem.ToString().Trim();
        string starttime = this.TextBox1.Text.Trim();
        string endtime = this.TextBox2.Text.Trim();
        if (starttime == string.Empty || endtime == string.Empty)
        {
            Response.Write("<script>alert('提示:请选择需要查询的时间范围.')</script>");
            this.TextBox1.Focus();

            return;
        }
        string sql = "select  *  from table where (initdate>='" + starttime + "' and initdate<='" + endtime + "') 
and kehumingcheng=isnull("
+ customername + ",kehumingcheng) and zhijuanpingzhong=isnull('" + pingzhong + "',zhijuanpingzhong)
and shengchanbianzu=isnull('"
+ banzu + "',shengchanbianzu) order by shengchanbianzu"; sql = sql.Replace("'null'", "null");
DbCommand dcmd = dbsqlserver.GetSqlStringCommand(sql);
 DataSet ds = dbsqlserver.ExecuteDataSet(dcmd);
 GridNonData(ds, this.GridView1);

而GridNonData方法的作用就当没有数据返回时候依然保持表头的现实并提示“暂无数据”:

    public void GridNonData(DataSet ds, GridView gv)
    {
        gv.DataSource = ds.Tables[0];
        gv.DataBind();
        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gv.DataSource = ds;
            gv.DataBind();

            int columnCount = gv.Rows[0].Cells.Count;
            gv.Rows[0].Cells.Clear();
            gv.Rows[0].Cells.Add(new TableCell());
            gv.Rows[0].Cells[0].ColumnSpan = columnCount;
            gv.Rows[0].Cells[0].Text = "暂无数据";

        }
    }

对于通过上面的方面来获取想要的数据以外还可以通过Hashtable来拼接where后面查询条件。
private string GetSql(string xiaoshouid, string wuliaoid, string selkey)
{
string init_sql = "select distinct(rollid) from table where state='n' and ";
System.Text.StringBuilder sb
= new StringBuilder();
System.Collections.Hashtable hs
= new System.Collections.Hashtable();
hs[
"aa"] = aa;
hs[
"bb"] = bb;
hs[
"cc"] = cc== string.Empty ? selkey : selkey + "%";
sb.Append(init_sql);
foreach (DictionaryEntry de in hs)
{
if (string.IsNullOrEmpty(de.Value.ToString()) == true)
{
continue;
}
else
{
if (de.Key.ToString() == "rollid")
{
sb.Append(de.Key);
sb.Append(
" like ");
sb.Append(
"'");
sb.Append(de.Value);
sb.Append(
"'");
sb.Append(
" and ");
}
else
{
sb.Append(de.Key);
sb.Append(
"=");
sb.Append(
"'");
sb.Append(de.Value);
sb.Append(
"'");
sb.Append(
" and ");
}
}
}
string sbs = sb.ToString();
sbs
= sbs.Remove(sbs.LastIndexOf("and"));
return sbs;

}
posted @ 2011-03-01 09:42  Jon.Zhiwei@hotmail.com  Views(1896)  Comments(0)    收藏  举报