如何在点击asp.net?

时间:2017-03-04 17:22:20

标签: c# asp.net sql-server

Hello Everyone我一直在开发一个项目,其中我有4个下拉列表,分别是ddlpackage,ddlcategory,ddlsubcategory,ddlpackageprice和一个名为Show的按钮 这是前端的图像:Image of Front end 以下是数据库Image of tables from which i have retreived data to show in dropdownlist中表格的图像 现在我的问题是当从ddlpackage中选择一个选项并点击show我刚刚获得ddlcategory上的选项然后再次点击show button im获取ddlsubcategory上的选项然后我必须再次点击show button to获取ddlpackage price的选项。我想单击show button显示所有选项,并选择dropdownlist ddlpackage这是我的C#代码:

private String strConnection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;Integrated Security=True;User Instance=True";

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindPackageDropdown();
    }
}
protected void BindPackageDropdown()
{
    //conenction path for database
    SqlConnection con = new SqlConnection(strConnection);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from package", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    ddlpackage.DataSource = ds;
    ddlpackage.DataTextField = "packname";
    ddlpackage.DataValueField = "packid";
    ddlpackage.DataBind();
    ddlpackage.Items.Insert(0, new ListItem("--Select--", "0"));
    ddlcategory.Items.Insert(0, new ListItem("--Select--", "0"));
}
protected void ddlpackage_SelectedIndexChanged(object sender, EventArgs e)
{
    int packageid = Convert.ToInt32(ddlpackage.SelectedValue);
    SqlConnection con = new SqlConnection(strConnection);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from category where Cat_id=" + packageid, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    ddlcategory.DataSource = ds;
    ddlcategory.DataTextField = "Cat_name";
    ddlcategory.DataValueField = "Cat_id";
    ddlcategory.DataBind();
    ddlcategory.Items.Insert(0, new ListItem("--Select--", "0"));
}


protected void ddlcategory_SelectedIndexChanged(object sender, EventArgs e)
{
    int categoryid = Convert.ToInt32(ddlcategory.SelectedValue);
    SqlConnection con = new SqlConnection(strConnection);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from subcategory where catid=" + categoryid, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    ddlsubcategory.DataSource = ds;
    ddlsubcategory.DataTextField = "subcatname";
    ddlsubcategory.DataValueField = "subcatid";
    ddlsubcategory.DataBind();
    ddlsubcategory.Items.Insert(0, new ListItem("--Select--", "0"));
}
protected void ddlsubcategory_SelectedIndexChanged(object sender, EventArgs e)
{

    int subcategoryid = Convert.ToInt32(ddlsubcategory.SelectedValue);
    SqlConnection con = new SqlConnection(strConnection);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from package where packid=" + subcategoryid, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    ddlpackageprice.DataSource = ds;
    ddlpackageprice.DataTextField = "packageprice";
    ddlpackageprice.DataValueField = "packageprice";
    ddlpackageprice.DataBind();
    ddlpackageprice.Items.Insert(0, new ListItem("--Select--", "0"));
}

请帮助抱歉我的英语不好......谢谢!

1 个答案:

答案 0 :(得分:0)

您无需再次点击“显示”按钮。当页面加载第一次你正在通过加载第一个下拉列表做正确的事情时,这里是你的代码已经这样做了。 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindPackageDropdown(); } }

现在您只需要确保在每个下拉列表中,每当您更改下拉列表中的选择时,相应的事件将被解雇,例如:ddlsubcategory_SelectedIndexChanged等