简体   繁体   English

DropDownList中的ASP.NET 2 DataValuefield

[英]ASP.NET 2 DataValuefield within DropDownList

I am having a problem in ASP.NET. 我在ASP.NET中遇到问题。 I have a table names Articles with these columns in Database: 我在数据库中的这些列中有一个表名为Articles的表:

Column 1: 第1栏: 在此处输入图片说明

Column 2: 第2栏: 在此处输入图片说明

Column 3: 第3栏: 在此处输入图片说明

I have a dropdownlist like this DropDownList1.DataTextField = "PERSHKRIM"; 我有一个像这样的下拉列表DropDownList1.DataTextField =“ PERSHKRIM”;

What I want to do is when I choose a product from DropDownList for example if I choose dropuct aa in a Label it will show PROD1 from Column 2 and in a second label it will show 111 from Column 3. 我想做的是从DropDownList中选择一个产品,例如,如果我在Label中选择dropuct aaPROD1在第2列中显示PROD1 ,在第二个标签中将在第3列中显示111

My code is this: 我的代码是这样的:

DataTable listaArtikujt = new DataTable();

using (SqlConnection lidhje = new SqlConnection(ConfigurationManager.ConnectionStrings["DEN1ConnectionString"].ConnectionString))
{
    try
    {
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM [Articles]", lidhje);
        adapter.Fill(listaArtikujt);
        DropDownList1.DataSource = listaArtikujt;
        DropDownList1.DataTextField = "PERSHKRIM";
        DropDownList1.DataValueField = "KOD";
        DropDownList1.DataBind();
        Label1.Text = DropDownList1.SelectedValue.ToString();
    }
    catch (Exception ex)
    {
        Response.Write("Error:" + ex.ToString());
    }
}

NOTE again: These columns are in one table names Articles . 再次注意:这些列在一个表名称Articles

I guess, this is what you are looking for: 我想,这就是您要寻找的:

        SqlDataAdapter adapter = new SqlDataAdapter("SELECT PERSHKRIM, KOD + '~' + CMSH AS KC FROM [Articles]", lidhje);
        adapter.Fill(listaArtikujt);
        DropDownList1.DataSource = listaArtikujt;
        DropDownList1.DataTextField = "PERSHKRIM";
        DropDownList1.DataValueField = "KC";
        DropDownList1.DataBind();
        Label1.Text = DropDownList1.SelectedValue.Split('~')[0].ToString();
        Label2.Text = DropDownList1.SelectedValue.Split('~')[1].ToString();

Note: if there will be '~' char in your records, it will cause problems. 注意:如果您的记录中有'〜'字符,则会引起问题。 Choose most appropriate char according to your records. 根据您的记录选择最合适的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM