2011-03-17 65 views
0

我有兩個下拉列表中的Web窗體..他們兩個我用下面的代碼來綁定與sql..but每當我試圖結合第二個下拉與同method..it的給錯誤列表.. 我使用的代碼:在兩個下拉列表中同時訪問數據的問題

protected void Page_Load(object sender, EventArgs e) 
    { 

     if (!IsPostBack) 
     { 

      ddlPropertyType.AppendDataBoundItems = true; 

      String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; 

      String strQuery = "select ID, PropertyName from PropertyType"; 

      SqlConnection con = new SqlConnection(strConnString); 

      SqlCommand cmd = new SqlCommand(); 

      cmd.CommandType = CommandType.Text; 

      cmd.CommandText = strQuery; 

      cmd.Connection = con; 

      try 
      { 

       con.Open(); 

       ddlPropertyType.DataSource = cmd.ExecuteReader(); 

       ddlPropertyType.DataTextField = "PropertyName"; 

       ddlPropertyType.DataValueField = "ID"; 

       ddlPropertyType.DataBind(); 

      } 

      catch (Exception ex) 
      { 
       throw ex; 
      } 

      finally 
      { 

       con.Close(); 

       con.Dispose(); 

      } 

     } 


    } 

回答

0

你想與他們之間的依賴關係創建兩個dropdownlists。 您需要在「父母」下拉列表中啓用AutoPostBack,向其添加已更改的事件,並在事件中加載您的「子女」下拉列表。

您在這裏有一個很好的例子:http://www.aspsnippets.com/Articles/Creating-Cascading-DropDownLists-in-ASP.Net.aspx

+0

上午取數據來自兩個tables..say – Prasad 2011-03-17 11:08:02

+0

我從兩個表中獲取數據..如果我在第一個ddl中選擇狀態,我需要第二個ddl中的所有城市名稱..如何在代碼同時獲取兩個表時使用此代碼.. – Prasad 2011-03-17 11:09:19

0

ü可以使用SQL數據庫直接綁定下拉.......沒有必要做cding

+0

我需要在第二個下拉列表的基礎上第一..我試圖直接綁定,但它給錯誤爲「輸入字符串格式不正確」 – Prasad 2011-03-17 11:05:19

+0

你只是檢查下拉列表的自動回發屬性自動回發屬性true – r12 2011-03-17 11:06:03

+0

其他明智的使用填寫方法 – r12 2011-03-17 11:09:36