2014-03-14 44 views
0

我有這張名爲FACILITIES的表。我已經編寫了代碼,以便當爲第一個下拉列表選擇會議時,第二個下拉列表將顯示屬於FAC_TYPE列數據的FAC_CODE列數據。如何根據我選擇的下拉列表中的選項重定向到某些頁面?

問題是我選擇FAC_CODE屬於任何FAC_TYPE的任何選項,它總是會重定向到number1.aspx。我想這樣做,如果我選擇會議的FAC_TYPE和任何選項的FAC_CODE它會去number1.aspx。如果我選擇FAC_TYPE的教程,並選擇FAC_CODE的任何選項,它將轉到number2.aspx。如果我選擇FAC_TYPE的Lecture和FAC_CODE的任何選項,它將轉到number2.aspx。

我的第一個下拉列表名稱是FAC_TYPE字段,稱爲ddlFacilityType,我的第二個下拉列表名稱是FAC_CODE字段,稱爲ddlFacility。兩者都有自動回覆打勾。那麼如何在ddlFacility_SelectedIndexChanged事件中進行編碼呢?顛簸此線程

enter image description here 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["projectConnectionString"].ToString(); // connection string 
      SqlConnection con = new SqlConnection(constr); 
      con.Open(); 


      SqlCommand com = new SqlCommand("select distinct FAC_TYPE from FACILITIES", 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["projectConnectionString"].ToString(); // connection string 
     SqlConnection con = new SqlConnection(constr); 
     con.Open(); 


     SqlCommand com = new SqlCommand("select distinct FAC_CODE from FACILITIES 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) 
    { 
     Response.Redirect("number1.aspx"); 
    } 
} 

回答

1

你可以做這樣的事情在ddlFacility_SelectedIndexChanged處理程序。

protected void ddlFacility_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if(ddlFacilityType.SelectedItem.ToString() == "Meeting") 
    { 
     Response.Redirect("number1.aspx"); 
    } 
    else if(ddlFacilityType.SelectedItem.ToString() == "Tutorial" || ddlFacilityType.SelectedItem.ToString()=="Lecture") 
    { 
     Response.Redirect("number2.aspx"); 
    } 
} 

您也可以通過下拉列表的SelectedIndex屬性做同樣的事情,在這種情況下,代碼會像

if(ddlFacilityType.SelectedIndex == 1) //Meeting is at index 1 of the ddlFacilityType dropdown 
{ 
    Response.Redirect("number1.aspx"); 
} 
相關問題