【问题标题】:Dynamic text box vanishing if another button or link button clicked如果单击另一个按钮或链接按钮,动态文本框会消失
【发布时间】:2012-06-02 07:48:55
【问题描述】:

下面编写的代码显示特定条件的文本框。但是当我单击另一个不相关的按钮或链接时,它会消失。当我在网页上执行其他活动时,我需要它保持可见

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    TextBox new_textbox = new TextBox();

    if (DropDownList1.Text.Equals("OFF"))
    {
        new_textbox.ID = "txt" + 1;

        PlaceHolder1.Controls.Add(new_textbox);
        Label5.Visible = true;
        new_textbox.Visible = true;
    }
    else
    {   
        Label5.Visible = false;
    }        
}

【问题讨论】:

    标签: c# .net visual-studio-2010 webpage


    【解决方案1】:

    这个问题之前在 SO 上被问过: Dynamically added controls in Asp.Net

    您只是在特定情况下添加此控件,特别是在DropDownList1.Text.Equals("OFF") 时。在这种情况下,您能否改为使用刚刚设置为可见的静态控件?

    根据msdn的Add Controls to an ASP.NET Web Page Programmatically

    控件通常在页面运行期间添加到页面中 初始化阶段。有关页面阶段的详细信息,请参阅 ASP.NET 页面 生命周期概述。

    引用链接到ASP.NET Page Life Cycle Overview

    您必须小心动态添加控件,请参阅有关 Dynamic Web Server Controls and View State 的这个 msdn 页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多