2014-08-31 50 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(); 
}