在c#中的页脚模板DropDownlist

时间:2014-08-31 10:57:45

标签: c# asp.net gridview footer

我需要创建 cascading DropDownList in ASP.Net c#

第二个DropDownList中的值取决于在第一个DropDownList中选择的值。

我在 Footer Template

中的 GridView. 中需要此功能

当从第一个DropDownList中选择一个值时,我需要使用 sql3 事件中执行的查询First_DDL_SelectedIndexChanged的输出填充第二个DropDownList。

debug 中,第一个DropDownList中选择的输出值和查询 sql3 的输出都是正确的。

我尝试使用此解决方案但没有成功,因为我没有错误,但即使您选择 Second_DDL 也始终为空> First_DDL

我非常感谢您在解决这个问题时能给我的任何帮助。

这是我的代码:

using System;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Level_Default2 : System.Web.UI.Page
{
    OdbcConnection cn =
       new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);

    DataSet dset;
    DataTable dt = new DataTable();
    DataTable dtCategories = new DataTable();
    DataTable dtSubCategories = new DataTable();
    OdbcDataAdapter dadapter;

    private DataTable RetrieveSubCategories(string TRZ)
    {
        string sql3 = " SELECT ... where TRZ = ?; ";

        using (OdbcConnection cn =
            new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
        {
            cn.Open();
            using (OdbcCommand cmd = new OdbcCommand(sql3, cn))
            {
                dadapter = new OdbcDataAdapter(cmd);
                dadapter.SelectCommand.Parameters.AddWithValue(?, TRZ.SelectedItem.ToString().Substring(0, 3));
                dadapter.Fill(dtSubCategories);
            }
        }
        return dtSubCategories;
    }

    private DataTable RetrieveCategories()
    {
        string sql2 = " SELECT ... ; ";

        using (OdbcConnection cn =
           new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
        {
            cn.Open();
            using (OdbcCommand cmd = new OdbcCommand(sql2, cn))
            {
                dadapter = new OdbcDataAdapter(cmd);
                dadapter.Fill(dtCategories);
            }
        }
        return dtCategories;
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList First_DDL = (DropDownList)e.Row.FindControl("First_DDL");
            First_DDL.DataTextField = "First_DDL";
            First_DDL.DataValueField = "First_DDL";
            First_DDL.DataSource = RetrieveCategories();
            First_DDL.DataBind();

            DropDownList Second_DDL = (DropDownList)e.Row.FindControl("Second_DDL");
            Second_DDL.DataTextField = "Second_DDL";
            Second_DDL.DataValueField = "Second_DDL";
            Second_DDL.DataSource = dtSubCategories;
            Second_DDL.DataBind();
        }
    }

    protected void First_DDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList TRZ = (DropDownList)sender;
        GridViewRow row = (GridViewRow)TRZ.NamingContainer;
        RetrieveSubCategories(TRZ.SelectedItem.ToString().Substring(0, 3)); //rebind second ddl

        Response.Write(TRZ.SelectedItem.ToString().Substring(0, 3));
    }

    public DataTable GridViewBind()
    {
        using (OdbcConnection cn =
            new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
        {
            string sql1 = " SELECT ... ; ";

            using (OdbcDataAdapter command =
                new OdbcDataAdapter(sql1, cn))
            {
                cn.Open();
                dset = new DataSet();
                dset.Clear();
                command.Fill(dset);
                DataTable dt = dset.Tables[0];
                GridView1.DataSource = dt;
                GridView1.DataBind();
                return dt;
            }
        }
    }

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

1 个答案:

答案 0 :(得分:0)

试试这个:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList First_DDL = (DropDownList)e.Row.FindControl("First_DDL");
            First_DDL.DataTextField = "First_DDL";
            First_DDL.DataValueField = "First_DDL";
            First_DDL.DataSource = RetrieveCategories();
            First_DDL.DataBind();    
        }
}

这在您的代码隐藏中:

protected void First_DDL_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList TRZ = (DropDownList)sender;
    GridViewRow row = (GridViewRow)TRZ.NamingContainer;
    DRetrieveSubCategories(TRZ.SelectedValue.Substring(0, 3)); //rebind second ddl

    DropDownList Second_DDL = (DropDownList)row.FindControl("Second_DDL");
    Second_DDL.SelectedIndex = 0;
    Second_DDL.Items.Clear(); 
    Second_DDL.DataTextField = "Second_DDL";
    Second_DDL.DataValueField = "Second_DDL";
    Second_DDL.DataSource = dtSubCategories;
    Second_DDL.DataBind();
}