【问题标题】:cannot see the label content when a item is selected from dynamically added dropdownlist从动态添加的下拉列表中选择项目时看不到标签内容
【发布时间】:2016-02-17 04:39:40
【问题描述】:

当我从此下拉列表 (DDL1) 中选择任何项目时,我有一个下拉列表 (DDL1),导致创建另一个下拉列表 (DDL2),其中包含一些项目。当我从 DDL1 中选择其他项目时,项目将更改在 DDL2 中,这发生在 DDL1 中选择的每个不同项目上。

当我从 DDL2 中选择一个项目时,必须显示标签内容,最初我正在制作标签 invisibe 并且在代码中我将可见性更改为 true 并向其添加内容。但是当我从 DDL2 中选择一个项目时,标签内容不显示。

这是我的代码

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
     if (DropDownList1.SelectedValue == "Abe Books")
    {
        DropDownSeller.Visible = true;
        lnkUsdBooks.Visible = true;
        lnkUsdBooks.Text = "usedbooks@abe.com";
        lnkUsdBooks.NavigateUrl = "mailto:usedbook@abe.com";
        DropDownSeller.Visible = true;
        DropDownSeller.Items.Remove("Chacha Choudary");
        DropDownSeller.Items.Remove("SpiderMan");
        DropDownSeller.Items.Remove("Amar chitra Katha");
        DropDownSeller.Items.Remove("Chandamama");
        DropDownSeller.Items.Remove("Mahabharata");
        DropDownSeller.Items.Add("Amar chitra Katha");
        DropDownSeller.Items.Add("Chandamama");
        DropDownSeller.Items.Add("Mahabharata");
        DropDownSeller.DataBind();

            if (DropDownSeller.SelectedValue == "Amar chitra Katha")
            {
                lblPrice.Visible = true;
                lblPrice.Text = "$69.99";
            }
            else if (DropDownSeller.SelectedValue == "Chandamama")
            {
                lblPrice.Visible = true;
                lblPrice.Text = "$59.99";
            }
            else if (DropDownSeller.SelectedValue == "Mahabharata")
            {
                lblPrice.Visible = true;
                lblPrice.Text = "$49.99";
            }
            else
            {
                lblPrice.Visible = false;
            }
        }

对此的任何想法表示赞赏

谢谢,

【问题讨论】:

  • "但是当我从 DDL2 中选择一个项目时,标签内容不显示"。那个事件的代码在哪里?
  • 您正在向DropDownSeller 添加项目并检查您刚刚添加的项目是否也被选中?
  • @Bala R 我已将 AutoPostBack 值设置为 true

标签: c# asp.net drop-down-menu


【解决方案1】:

DropDownList1_SelectedIndexChanged 中删除if (!Page.IsPostBack),因为当页面回发时,此条件将为假。因为您的页面正在回发到服务器,这就是它不可见且不显示的原因。

简而言之,您的DropDownList1_SelectedIndexChanged 应该是......

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedValue == "Abe Books")
    {
        DropDownSeller.Visible = true;
        lnkUsdBooks.Visible = true;
        lnkUsdBooks.Text = "usedbooks@abe.com";
        lnkUsdBooks.NavigateUrl = "mailto:usedbook@abe.com";
        DropDownSeller.Visible = true;

        DropDownSeller.Items.Clear(); // it will clear all the items, instead you are removing one by one

        DropDownSeller.Items.Add("Amar chitra Katha");
        DropDownSeller.Items.Add("Chandamama");
        DropDownSeller.Items.Add("Mahabharata");
        DropDownSeller.DataBind(); 
    }
    protected void DropDownSeller_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownSeller.SelectedValue == "Amar chitra Katha")
        {
            lblPrice.Visible = true;
            lblPrice.Text = "$69.99";
        }
        else if (DropDownSeller.SelectedValue == "Chandamama")
        {
            lblPrice.Visible = true;
            lblPrice.Text = "$59.99";
        }
        else if (DropDownSeller.SelectedValue == "Mahabharata")
        {
            lblPrice.Visible = true;
            lblPrice.Text = "$49.99";
        }
        else
        {
            lblPrice.Visible = false;
        } 
    }

【讨论】:

  • 正如我正要写的那样。如果您单步执行您的代码,您将能够看到这一点。
  • 我删除了 (!page.IsPostback),当我从 DDL2 中选择项目时仍然看不到标签内容
  • 我已经编辑了我的答案。现在试试。您必须使用 DropDownSeller Selected Index 更改,因为您希望在 DropDownSeller 上可见
  • @Akthar 谢谢它现在正在工作...:)
猜你喜欢
  • 2020-10-16
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多