2011-12-29 29 views
0
  • 我有2個下拉列表,1個標籤& 1個文本框。通過sql select語句顯示ID和名稱

  • 選擇「產品類別」@ 1st ddl,2nd ddl顯示所有產品類別。

  • 問題是,我怎麼可能顯示產品類別id @標籤,並在加載/選擇第二個ddl時顯示文本框名稱?

  • 我有以下代碼:

 

Public Sub FilterProductCategory() 
    Dim myConn As New SqlConnection 
    Dim myCmd As New SqlCommand 
    Dim dtrReader As SqlDataReader 


    myConn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 
    myCmd = myConn.CreateCommand 
    myCmd.CommandText = "SELECT product_category_id, product_category_name FROM ProductCategory ORDER BY product_category_name" 

    Try 
     myConn.Open() 
     dtrReader = myCmd.ExecuteReader() 

     If dtrReader.HasRows Then 
      DropDownList2.Items.Clear() 
      DropDownList2.DataSource = dtrReader 
      DropDownList2.DataValueField = "product_category_name" 
      DropDownList2.DataBind() 
     End If 
     dtrReader.Close() 
     myConn.Close() 
    Catch 
    End Try 
End Sub 

回答

0

剛剛成立的:

DropDownList2.DataTextField = "product_category_name" 
DropDownList2.DataValueField = "product_category_id" 

所以當你嘗試dropdownlist2.selectedvalue那麼你將得到ID,當ü嘗試dropdownlist2.selecteditem.text那麼它會給你的產品類別名稱..

希望它會解決你的問題

+0

以上3條建議現在工作正常。現在我的問題是,只有在選擇@ ddl2之後,這些更改纔會反映出來。 負載時,它沒有。目前該標識顯示在標籤和文本框中。 任何想法? – brainsfrying 2011-12-29 13:26:41

0

希望這有助於,聽起來很簡單。我假設您的標籤和文本框的ID與下拉列表的格式相同。如果不是,請替換您自己的ID。此代碼也完全運行在服務器端,這意味着您將刷新整個頁面來完成這個相當簡單的任務。如果這不是問題,那麼通過一切手段使用這種方法,否則我會建議在客戶端使用JavaScript來做到這一點。

Protected Sub DropDownList2_SelectedIndexChanged(Sender as Object, e as EventArgs) Handles DropDownList2.SelectedIndexChanged 
Label1.Text = DropDownList2.SelectedValue 
TextBox1.Text = DropDownList2.SelectedItem.Text 
End Sub 
+0

上述三點建議,現在工作得很好。現在我的問題是,只有在選擇@ ddl2之後,這些更改纔會反映出來。 負載時,它沒有。目前該標識顯示在標籤和文本框中。 任何想法? – brainsfrying 2011-12-29 13:26:11

0

首套DataTextField到您的下拉列表display text(類別名稱)和DataValueField到您的下拉列表SelectedValue(categoryID)。

DropDownList2.DataTextField = "product_category_name"; 
DropDownList2.DataValueField = "product_category_id"; 

和DropDownList中的SelectIndexChanged Event使用SelectedValue屬性來訪問category ID AMD SelectedItem.Text訪問Categoryname

Protected Sub DropDownList2_SelectedIndexChanged(Sender as Object, e as EventArgs) Handles DropDownList2.SelectedIndexChanged 
Label1.Text = DropDownList2.SelectedValue 
TextBox1.Text = DropDownList2.SelectedItem.Text 
End Sub 

檢查DropDownList Class有關這些屬性的信息等。

檢查這個例子:

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

     private void BindList() 
     { 
      SqlConnection con = new SqlConnection("server=.\\sqlexpress;initial catalog=test;integrated security=true;"); 
      SqlDataReader reader; 
      SqlCommand command = con.CreateCommand(); 
      command.CommandText = "Select * from UserTable order by UserName"; 
      try 
      { 
       con.Open(); 
       reader = command.ExecuteReader(); 
       if (reader.HasRows) 
       { 
        DropDownList1.DataSource = reader; 
        DropDownList1.DataValueField = "userid"; 
        DropDownList1.DataTextField = "UserName"; 
        DropDownList1.DataBind(); 
       } 

       reader.Close(); 
       con.Close(); 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 

     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      Label1.Text = DropDownList1.SelectedValue; 
      TextBox1.Text = DropDownList1.SelectedItem.Text; 
     } 

輸出///

enter image description here

+0

以上3條建議現在工作正常。現在我的問題是,只有在選擇@ ddl2之後,這些更改纔會反映出來。 負載時,它沒有。目前該標識顯示在標籤和文本框中。 任何想法? – brainsfrying 2011-12-29 13:25:52

+0

檢查您指定的字段名稱應與表列名稱匹配。如果一切正常,則在此處共享您的字段數據類型..或設置字段數據類型..請參閱datalist文檔鏈接以瞭解此內容,正如我告訴您的答案.. – 2011-12-29 14:17:01