通过sql select语句显示id和名称

时间:2011-12-29 12:48:17

标签: .net sql vb.net

  • 我有2个下拉列表,1个标签& 1个文本框。

  • 选择'产品类别'@ 1st ddl,2nd ddl显示所有产品类别。

  • 问题是,如何在第二个ddl的加载/选择中显示产品类别id @标签,并命名@文本框?

  • 我有以下代码:

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

设置一下:

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

所以当你尝试dropdownlist2.selectedvalue然后你会得到id,当你尝试dropdownlist2.selecteditem.text然后它会给你产品类别名称..

希望它能解决您的问题

答案 2 :(得分:0)

首先将DataTextField设置到您的下拉列表display text(CategoryName),将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

相关问题