title: Show values in a label from chosen item in dropdownlist
body: <p>I have a scrpt that I want to show values in a label after post back when chosing an item from dropdownlist. When I choose an item from dropdownlist, for example "Apple", it will show in the label the price of the Apple. And if I choose in the dropdownlist "Orange" in the label it will show the price of the Orange". Any idea how to do this? Thank you.</p>
  
  <p>this is the code...</p>
  
  <p>ContactDetalis.ascx</p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactDetails.ascx.cs"
      Inherits="UserControl_ContactDetails" EnableViewState="false"  %&gt;
      &lt;style type="text/css"&gt;
          .auto-style1 {
              width: 100%;
          }
          .auto-style2 {
              width: 150px;
          }
          .auto-style3 {
              width: 294px;
          }
          .auto-style4 {
              width: 150px;
              text-align: right;
          }
      &lt;/style&gt;
  &lt;table class="auto-style1"&gt;
      &lt;tr&gt;
          &lt;td class="auto-style4"&gt;
              &lt;asp:Label ID="Label7" runat="server" Text="Klienti:"&gt;&lt;/asp:Label&gt;
          &lt;/td&gt;
  
          &lt;td class="auto-style3"&gt;
              &lt;asp:DropDownList ID="DropDownListKlienti" runat="server"&gt;
              &lt;/asp:DropDownList&gt;
          &lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="auto-style4"&gt;
              &lt;asp:Label ID="Label1" runat="server" Text="Artikulli:"&gt;&lt;/asp:Label&gt;
          &lt;/td&gt;
          &lt;td class="auto-style3"&gt;
              &lt;asp:DropDownList ID="DropDownListArtikulli" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListArtikulli_SelectedIndexChanged"&gt;
              &lt;/asp:DropDownList&gt;
          &lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="auto-style4"&gt;
              &lt;asp:Label ID="Label2" runat="server" Text="Cmimi:"&gt;&lt;/asp:Label&gt;
          &lt;/td&gt;
          &lt;td class="auto-style3"&gt;
              &lt;asp:Label ID="LabelCmimi" runat="server"&gt;&lt;/asp:Label&gt;
              &lt;asp:Label ID="Label3" runat="server" Text="lek"&gt;&lt;/asp:Label&gt;
          &lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="auto-style4"&gt;
              &lt;asp:Label ID="Label4" runat="server" Text="Sasia:"&gt;&lt;/asp:Label&gt;
          &lt;/td&gt;
          &lt;td class="auto-style3"&gt;
              &lt;asp:TextBox ID="TextBoxSasia" runat="server" AutoPostBack="True" OnTextChanged="TextBoxSasia_TextChanged"&gt;&lt;/asp:TextBox&gt;
          &lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="auto-style4"&gt;
              &lt;asp:Label ID="Label5" runat="server" Text="Vlera:"&gt;&lt;/asp:Label&gt;
          &lt;/td&gt;
          &lt;td class="auto-style3"&gt;
              &lt;asp:Label ID="LabelVlera" runat="server"&gt;&lt;/asp:Label&gt;
              &lt;asp:Label ID="Label6" runat="server" Text="lek"&gt;&lt;/asp:Label&gt;
          &lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="auto-style2"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td class="auto-style3"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="auto-style2"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td class="auto-style3"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="auto-style2"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td class="auto-style3"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="auto-style2"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td class="auto-style3"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>ContactDetalis.ascx.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Data;
  
  public partial class UserControl_ContactDetails : System.Web.UI.UserControl
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          if(Page.IsPostBack)
          {
              AddItems();
  
              //Retain the state of controls
              DropDownListKlienti.Text = Request.Form[DropDownListKlienti.UniqueID];
              DropDownListArtikulli.Text = Request.Form[DropDownListArtikulli.UniqueID];
              LabelCmimi.Text = Request.Form[LabelCmimi.UniqueID];
              TextBoxSasia.Text = Request.Form[TextBoxSasia.UniqueID];
              LabelVlera.Text = Request.Form[LabelVlera.UniqueID];
          }
  
         if(!IsPostBack)
          {
              AddItems();
          }
      }
  
      private void AddItems()
      {
          DataTable listaArtikujt = new DataTable();
  
          using (SqlConnection lidhje = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
          {
              try
              {
  
                  SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM [Artikujt]", lidhje);
                  adapter.Fill(listaArtikujt);
  
                  DropDownListArtikulli.DataSource = listaArtikujt;
                  DropDownListArtikulli.DataTextField = "Artikulli";
                  DropDownListArtikulli.DataValueField = "Cmimi";
                  DropDownListArtikulli.DataBind();
  
                  LabelCmimi.Text = DropDownListArtikulli.SelectedValue.ToString();
  
              }
              catch (Exception ex)
              {
                  Response.Write("Gabim:" + ex.ToString());
              }
          }
      }
      protected void DropDownListArtikulli_SelectedIndexChanged(object sender, EventArgs e)
      {
          AddItems();
      }
      protected void TextBoxSasia_TextChanged(object sender, EventArgs e)
      {
          LabelVlera.Text = TextBoxSasia.Text.ToString();
      }
  }
  </code></pre>