2014-08-31 49 views
1

我需要创建一个cascading DropDownList in ASP.Net c#c中的页脚模板DropDownlist#

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

我需要这在Footer TemplateGridView.

当从第一个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(); 
     } 
    } 
} 

回答

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