从下拉控件填充gridview(C#)

时间:2016-04-04 06:08:59

标签: c# asp.net gridview

我在asp网页上工作,其中包含学期名称的下拉列表,并根据此下拉列表中的所选项目,将显示级别和课程的网格视图。

  

问题是网格视图永远不会根据下拉选择

而改变

因此,当我选择一个学期名称时,让我们说"Fall",gridview会显示所有学期" Fall & Spring & Summer"及其水平和课程。

这是我的代码:

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
      gvSemester.DataSource = GetData(string.Format("select COURSE_SEMESTER from COURSE GROUP BY  COURSE_SEMESTER"));

        gvSemester.DataBind();
    }
}

private static DataTable GetData(string query)
{
    string constr = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
    using (OracleConnection con = new OracleConnection(constr))
    {
        using (OracleCommand cmd = new OracleCommand())
        {
            cmd.CommandText = query;
            using (OracleDataAdapter sda = new OracleDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}

protected void Show_Hide_LevelsGrid(object sender, EventArgs e)
{
    ImageButton imgShowHide = (sender as ImageButton);
    GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);
    if (imgShowHide.CommandArgument == "Show")
    {
        row.FindControl("pnlLevels").Visible = true;
        imgShowHide.CommandArgument = "Hide";
        imgShowHide.ImageUrl = "~/image/minus.png";
        string semesterId = gvSemester.DataKeys[row.RowIndex].Value.ToString();// semester
        GridView gvLevel = row.FindControl("gvLevel") as GridView;
        BindLevels(semesterId, gvLevel);
    }
    else
    {
        row.FindControl("pnlLevels").Visible = false;
        imgShowHide.CommandArgument = "Show";
        imgShowHide.ImageUrl = "~/image/plus.png";
    }
}

private void BindLevels(string semesterId, GridView gvLevel)
{

    gvLevel.ToolTip = semesterId;
    gvLevel.DataSource = GetData(string.Format("SELECT COURSE_LEVEL from COURSE  where COURSE_SEMESTER= '" + semesterId + "' GROUP BY  COURSE_LEVEL ORDER BY COURSE_LEVEL")); //was COURSE_SEMESTER=Check it shows the selected semester levels for all
    gvLevel.DataBind(); 
}



protected void Show_Hide_CoursesGrid(object sender, EventArgs e)
{
    ImageButton imgShowHide = (sender as ImageButton);
    GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);
    if (imgShowHide.CommandArgument == "Show")
    {
        row.FindControl("pnlCourses").Visible = true;
        imgShowHide.CommandArgument = "Hide";
        imgShowHide.ImageUrl = "~/image/minus.png";
        string levelId = (row.NamingContainer as GridView).DataKeys[row.RowIndex].Value.ToString();//level
        GridView gvCourse = row.FindControl("gvCourse") as GridView;//..
        BindCourses(levelId, gvCourse);//..
    }
    else
    {
        row.FindControl("pnlCourses").Visible = false;
        imgShowHide.CommandArgument = "Show";
        imgShowHide.ImageUrl = "~/image/plus.png";
    }
}

private void BindCourses(string levelId, GridView gvCourse)
{

    gvCourse.ToolTip = levelId;
    gvCourse.DataSource = GetData(string.Format("select * from COURSE where COURSE_LEVEL='{0}'", levelId));
    gvCourse.DataBind();
}


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }



}

3 个答案:

答案 0 :(得分:1)

您可以设置下拉列表AutoPostBack = True

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    BindLevels();
}

答案 1 :(得分:0)

使用下拉列表SelectedIndexChanged事件填充您的gridview,并在SQL查询中应用where where。

答案 2 :(得分:0)

为"级别和课程添加更新面板"网格。

在下拉列表更改事件中,您将更新网格。

UpdatePanelId.Update();