Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 通过sql select语句显示id和名称_.net_Sql_Vb.net - Fatal编程技术网

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

.net 通过sql select语句显示id和名称,.net,sql,vb.net,.net,Sql,Vb.net,我有两个下拉列表,一个标签和一个文本框 在第1个ddl选择“产品类别”时,第2个ddl显示所有产品类别 问题是,在加载/选择第二个ddl时,如何在标签上显示产品类别id,在文本框中显示名称 我有以下代码: 希望这有帮助,听起来很简单。我假设标签和文本框的ID与dropdownlist的格式相同。如果没有,请替换您自己的身份证。此外,此代码完全在服务器端运行,这意味着您将刷新整个页面,以完成此相当简单的任务。如果这不是一个问题,那么一定要使用这个方法,否则我建议在客户端使用javascript P

我有两个下拉列表,一个标签和一个文本框

在第1个ddl选择“产品类别”时,第2个ddl显示所有产品类别

问题是,在加载/选择第二个ddl时,如何在标签上显示产品类别id,在文本框中显示名称

我有以下代码:


希望这有帮助,听起来很简单。我假设标签和文本框的ID与dropdownlist的格式相同。如果没有,请替换您自己的身份证。此外,此代码完全在服务器端运行,这意味着您将刷新整个页面,以完成此相当简单的任务。如果这不是一个问题,那么一定要使用这个方法,否则我建议在客户端使用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
只需设置以下内容:

DropDownList2.DataTextField = "product_category_name"
DropDownList2.DataValueField = "product_category_id"
因此,当您尝试dropdownlist2.selectedvalue时,您将获得id;当您尝试dropdownlist2.selecteditem.text时,它将为您提供产品类别名称

希望它能解决您的问题

首先设置dropdownlist显示文本类别名称和dropdownlist SelectedValuecategoryID

DropDownList2.DataTextField = "product_category_name";
DropDownList2.DataValueField = "product_category_id";
在DropDownList的SelectIndexChanged事件中,使用SelectedValue属性访问类别ID,使用SelectedItem.Text访问类别名称

使用示例检查有关这些属性的信息

检查此示例:

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;
        }
输出///


以上3个建议现在都很有效。现在我的问题是,只有在选择@ddl2之后,更改才会反映出来。在加载时,它不会。当前,id同时显示在标签和文本框中。有什么想法吗?请检查您指定的字段名是否与表列名匹配。。如果一切正常,则在此处共享字段数据类型..或设置字段数据类型。。正如我在回答中告诉你的那样,请参阅datalist文档链接来理解这一点。上面的3条建议现在很好用。现在我的问题是,只有在选择@ddl2之后,更改才会反映出来。在加载时,它不会。当前,id同时显示在标签和文本框中。有什么想法吗?以上三个建议现在都很好用。现在我的问题是,只有在选择@ddl2之后,更改才会反映出来。在加载时,它不会。当前,id同时显示在标签和文本框中。有什么想法吗?
Protected Sub DropDownList2_SelectedIndexChanged(Sender as Object, e as EventArgs) Handles DropDownList2.SelectedIndexChanged
Label1.Text = DropDownList2.SelectedValue
TextBox1.Text = DropDownList2.SelectedItem.Text
End Sub
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;
        }