簡體   English   中英

在下拉列表中的所選項目中顯示標簽中的值

[英]Show values in a label from chosen item in dropdownlist

我有一個要在從下拉列表中選擇項目時回發后在標簽中顯示值的提示。 當我從下拉列表中選擇一項時,例如“ Apple”,它將在標簽中顯示Apple的價格。 如果我在標簽的下拉列表中選擇“橙色”,它將顯示橙色的價格。您知道如何執行此操作嗎?謝謝。

這是代碼...

ContactDetalis.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactDetails.ascx.cs"
    Inherits="UserControl_ContactDetails" EnableViewState="false"  %>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 150px;
        }
        .auto-style3 {
            width: 294px;
        }
        .auto-style4 {
            width: 150px;
            text-align: right;
        }
    </style>
<table class="auto-style1">
    <tr>
        <td class="auto-style4">
            <asp:Label ID="Label7" runat="server" Text="Klienti:"></asp:Label>
        </td>

        <td class="auto-style3">
            <asp:DropDownList ID="DropDownListKlienti" runat="server">
            </asp:DropDownList>
        </td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style4">
            <asp:Label ID="Label1" runat="server" Text="Artikulli:"></asp:Label>
        </td>
        <td class="auto-style3">
            <asp:DropDownList ID="DropDownListArtikulli" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListArtikulli_SelectedIndexChanged">
            </asp:DropDownList>
        </td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style4">
            <asp:Label ID="Label2" runat="server" Text="Cmimi:"></asp:Label>
        </td>
        <td class="auto-style3">
            <asp:Label ID="LabelCmimi" runat="server"></asp:Label>
            <asp:Label ID="Label3" runat="server" Text="lek"></asp:Label>
        </td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style4">
            <asp:Label ID="Label4" runat="server" Text="Sasia:"></asp:Label>
        </td>
        <td class="auto-style3">
            <asp:TextBox ID="TextBoxSasia" runat="server" AutoPostBack="True" OnTextChanged="TextBoxSasia_TextChanged"></asp:TextBox>
        </td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style4">
            <asp:Label ID="Label5" runat="server" Text="Vlera:"></asp:Label>
        </td>
        <td class="auto-style3">
            <asp:Label ID="LabelVlera" runat="server"></asp:Label>
            <asp:Label ID="Label6" runat="server" Text="lek"></asp:Label>
        </td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style2">&nbsp;</td>
        <td class="auto-style3">&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style2">&nbsp;</td>
        <td class="auto-style3">&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style2">&nbsp;</td>
        <td class="auto-style3">&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style2">&nbsp;</td>
        <td class="auto-style3">&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
</table>

ContactDetalis.ascx.cs

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();
    }
}

刪除AddItems(); 並將其添加到您的select_changed代碼中

protected void DropDownListArtikulli_SelectedIndexChanged(object sender, EventArgs e)
{
    LabelCmimi.Text = DropDownListArtikulli.SelectedValue.ToString();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM