如何在页面加载事件上绑定多个下拉列表

时间:2017-01-15 14:21:44

标签: asp.net

在下面的代码中,我已经绑定了一个下拉列表。如何绑定第二个下拉列表

#region DataBaseObjects
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["prConnectionString"].ConnectionString);
SqlDataAdapter da;
DataSet ds = new DataSet();
#endregion

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
    }

    else
    {
        try
        {
            da = new SqlDataAdapter("select distinct (EmpDepartment) from DepartmentMaster", con);
            da.Fill(ds, "deprt");
            ddldepartment.DataSource = ds.Tables["deprt"];
            ddldepartment.DataTextField = "EmpDepartment";
            ddldepartment.DataValueField = "EmpDepartment";
            ddldepartment.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

是的,我在页面加载事件上绑定了多个下拉列表 在使用其他声明之后我尝试了,抓住

protected void Page_Load(object sender,EventArgs e)         {             if(Page.IsPostBack)             {

        }

        else
        {
            try
            {
                da = new SqlDataAdapter("select distinct (EmpDepartment) from DepartmentMaster", con);
                da.Fill(ds, "deprt");
                ddldepartment.DataSource = ds.Tables["deprt"];
                ddldepartment.DataTextField = "EmpDepartment";
                ddldepartment.DataValueField = "EmpDepartment";
                ddldepartment.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
            try
            {
                da = new SqlDataAdapter("select distinct (EmpDesignation) from DepartmentMaster", con);
                da.Fill(ds, "desig");
                DropDownList2. DataSource = ds.Tables["desig"];
                DropDownList2.DataTextField = "EmpDesignation";
                DropDownList2.DataValueField = "EmpDesignation";
                DropDownList2.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }