在下拉列表中显示所选项目的标签中的值

时间:2014-02-01 14:28:49

标签: c# asp.net

我有一个scrpt,我希望在从下拉列表中选择一个项目后回发后显示标签中的值。当我从下拉列表中选择一个项目时,例如“Apple”,它将在标签中显示Apple的价格。如果我在标签中的下拉列表中选择“Orange”,它将显示Orange的价格“。任何想法如何做到这一点?谢谢。

这是代码......

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

1 个答案:

答案 0 :(得分:0)

删除AddItems();并将其添加到您的select_changed代码

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