2011-12-29 108 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