Pony
Pony

Reputation: 401

How to redirect to certain pages depending on what option that I select for dropdownlist?

I have this table called FACILITIES. I already code it so that when for etc when meeting is selected for first dropdownlist, second dropdownlist will display the FAC_CODE column data that belongs to the FAC_TYPE column data.

The problem is whatever option that i choose for the FAC_CODE which belongs to any FAC_TYPE, it will always redirect to number1.aspx. I wan to make it so that, if I choose Meeting for the FAC_TYPE and whatever option for the FAC_CODE it will go to number1.aspx. If I choose Tutorial for the FAC_TYPE and whatever option for the FAC_CODE it will go to number2.aspx. If I choose Lecture for the FAC_TYPE and whatever option for the FAC_CODE it will go to number2.aspx.

My first dropdownlist name which is the FAC_TYPE field is called ddlFacilityType and my second dropdownlist name which is the FAC_CODE field is called ddlFacility. Both have autopostback ticked. So how to code in the ddlFacility_SelectedIndexChanged event? bumps up for this thread

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");
    }
}

Upvotes: 0

Views: 106

Answers (1)

stranded
stranded

Reputation: 322

You can do something like this in on ddlFacility_SelectedIndexChanged handler.

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");
    }
}

You can also do the same thing by the SelectedIndex property of the dropdownlist, in that case, the code would be something like

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

Upvotes: 1

Related Questions