如何查找表,以便我的下拉列表将显示我想要的数据

时间:2014-04-19 19:24:48

标签: c# asp.net sql drop-down-menu lookup-tables

我有两个名为facility和Facilitytype_lookup表的表,它们使用fac_type列字段相互关联。在第一个下拉列表中,我使用设施表上的fac_type字段填充数据,如果选择了fac_type字段值,例如选择LT,则属于该fac_type的fac_code将显示在第二个下拉列表中,即L屏幕截图中的.337和L.338。问题是我希望第一个下拉列表名称完整。对于MR MR作为会议室,DR作为讨论室,LT为leture Theatre。这就是为什么我用名为fac_name的列字段创建另一个名为Facilitytype_lookup的表。如何使用列字段fac_name ??

使第一个下拉列表名称为全名

enter image description here

enter image description here

enter image description here

enter image description here

public partial class MainMenu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //string constr = ConfigurationManager.ConnectionStrings["serverConnectionString"].ToString(); // connection string
            string constr = ConfigurationManager.ConnectionStrings["projectConnectionString"].ToString();
            SqlConnection con = new SqlConnection(constr);
            con.Open();


            SqlCommand com = new SqlCommand("select distinct FAC_TYPE from FACILITY", con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);  // fill dataset

            ddlFacilityType.DataTextField = ds.Tables[0].Columns["FAC_TYPE"].ToString(); // text field name of table dispalyed in dropdown
            // to retrive specific  textfield name 
            ddlFacilityType.DataSource = ds.Tables[0];      //assigning datasource to the dropdownlist
            ddlFacilityType.DataBind();  //binding dropdownlist

            ddlFacilityType.Items.Insert(0, new ListItem(" Select type", "0"));
        }

        ddlFacility.Items.Insert(0, new ListItem(" Select room", "0"));
    }


    protected void ddlFacilityType_SelectedIndexChanged(object sender, EventArgs e)
    {
        //string constr = ConfigurationManager.ConnectionStrings["serverConnectionString"].ToString(); // connection string
        string constr = ConfigurationManager.ConnectionStrings["projectConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();


        SqlCommand com = new SqlCommand("select distinct FAC_CODE from FACILITY where FAC_TYPE='" + ddlFacilityType.SelectedValue.ToString() + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);  // fill dataset

        ddlFacility.DataTextField = ds.Tables[0].Columns["FAC_CODE"].ToString(); // text field name of table dispalyed in dropdown
        // to retrive specific  textfield name 
        ddlFacility.DataSource = ds.Tables[0];      //assigning datasource to the dropdownlist
        ddlFacility.DataBind();  //binding dropdownlist

        ddlFacility.Items.Insert(0, new ListItem(" Select room", "0"));
    }

    protected void ddlFacility_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["roomvalue"] = ddlFacility.SelectedValue;

        if (ddlFacilityType.Text == "MR")
        {
            Response.Redirect("number1.aspx");
        }
        else if (ddlFacilityType.Text == "DR")
        {
            Response.Redirect("number1.aspx");
        }
        else
            Response.Redirect("number2.aspx");       
    }
}

已编辑,第一个下拉列表将显示fac_name值,但在第一个下拉列表中选择任何选项后,第二个下拉列表不会显示任何内容

public partial class MainMenu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //string constr = ConfigurationManager.ConnectionStrings["serverConnectionString"].ToString(); // connection string
            string constr = ConfigurationManager.ConnectionStrings["projectConnectionString"].ToString();
            SqlConnection con = new SqlConnection(constr);
            con.Open();


            SqlCommand com = new SqlCommand("select distinct facility.FAC_TYPE, facilitytype_lookup.fac_name from FACILITY inner join facilitytype_lookup ON facility.fac_type=facilitytype_lookup.fac_type", con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);  // fill dataset

            ddlFacilityType.DataTextField = ds.Tables[0].Columns["FAC_name"].ToString(); // text field name of table dispalyed in dropdown

            // to retrive specific  textfield name 
            ddlFacilityType.DataSource = ds.Tables[0];      //assigning datasource to the dropdownlist
            ddlFacilityType.DataBind();  //binding dropdownlist

            ddlFacilityType.Items.Insert(0, new ListItem(" Select type", "0"));
        }

        ddlFacility.Items.Insert(0, new ListItem(" Select room", "0"));
    }


    protected void ddlFacilityType_SelectedIndexChanged(object sender, EventArgs e)
    {
        //string constr = ConfigurationManager.ConnectionStrings["serverConnectionString"].ToString(); // connection string
        string constr = ConfigurationManager.ConnectionStrings["projectConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();


        SqlCommand com = new SqlCommand("select distinct FAC_CODE from FACILITY where FAC_TYPE='" + ddlFacilityType.SelectedValue.ToString() + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);  // fill dataset

        ddlFacility.DataTextField = ds.Tables[0].Columns["FAC_CODE"].ToString(); // text field name of table dispalyed in dropdown
        // to retrive specific  textfield name 
        ddlFacility.DataSource = ds.Tables[0];      //assigning datasource to the dropdownlist
        ddlFacility.DataBind();  //binding dropdownlist

        ddlFacility.Items.Insert(0, new ListItem(" Select room", "0"));
    }

    protected void ddlFacility_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["roomvalue"] = ddlFacility.SelectedValue;

        if (ddlFacilityType.Text == "MR")
        {
            Response.Redirect("number1.aspx");
        }
        else if (ddlFacilityType.Text == "CR")
        {
            Response.Redirect("number1.aspx");
        }
        else if (ddlFacilityType.Text == "DR")
        {
            Response.Redirect("number1.aspx");
        }
        else
            Response.Redirect("number2.aspx");

    }
}

0 个答案:

没有答案