从代码后端显示 div 标签

3
我正在试图从后端代码中显示一个div标签,但我尝试过的方法似乎没有起作用。
在页面加载时,我执行以下操作(隐藏div),这似乎很好。
loginLoader.Attributes.Add("style", "display:none");

但是我似乎无法再次显示它(在这种情况下,需要单击按钮重新显示)。
protected void butSubmit_Click(object sender, EventArgs e)
    {
        try
        {

            loginLoader.Attributes.Add("style", "display:block");
            //etc.

可能建议一下,这个div没有显示?

祝好


"可能重复": https://dev59.com/KW435IYBdhLWcg3wpxyw 和 http://stackoverflow.com/questions/11162567/how-to-show-hidden-div-from-codebehind-c-sharp - Ramesh Rajendran
你的隐藏 div 的代码是否在 if (!IsPostBack) { ... } 块内? - Emre
是的,这没问题,但在 try...catch 块内部它不想隐藏或显示。 - Arianule
2个回答

4

您需要使用 样式(Style),而不是属性(Attribute)

Button1.Style.Add("display", "block");

或者

Button1.Style["display"] = "block";

您认为在这里使用visible属性而不是样式更加合适。
loginLoader.Visible = true;

1

你的“loginLoader”是否已添加到页面控件中? 尝试将“LoginLoader”添加到其他控件中(例如:page.controls.add(loginLoader)),然后通过ctrl + F在页面中找到你的文本。

protected void Button1_Click(object sender, EventArgs e)
        {
            Panel myDiv = new Panel(); //creating dynamic control
            myDiv.Attributes.Add("style", "display:block; width:100px; height:100px; background-color:red;"); //set attrs for visibility
            this.Page.Controls.Add(myDiv); // add to some control (now is Page)
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接