C# 显示无/删除样式背后的asp.net 代码不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11125609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
display none / remove style for asp.net code behind not working
提问by oJM86o
I've got a button on my page in code behind I do this:
我的页面上有一个按钮,后面的代码是我这样做的:
btnSaveLineItems.Style.Add("display", "none");
btnSaveLineItems.Style.Add("display", "none");
But later I want to show that button so I tried this:
但后来我想显示那个按钮,所以我尝试了这个:
btnSaveLineItems.Style.Clear();
btnSaveLineItems.Style.Clear();
This doesnt appear to reshow the button... The html markup in the beginning has a "style=display:none;" in the beginning of the page.. and it maintains that style even though I try to remove it?
这似乎没有重新显示按钮......开头的html标记有一个“style=display:none;” 在页面的开头......即使我尝试删除它,它仍然保持这种风格?
When my page first starts up I have this:
当我的页面第一次启动时,我有这个:
btnSaveLineItems.Style["display"] = "none";
btnSaveLineItems.Style["display"] = "none";
This renders like the following in HTML:
这在 HTML 中呈现如下:
<input type="submit" name="ctl00$MainContent$btnSaveLineItems" value="Save" id="MainContent_btnSaveLineItems" title="Save changes?" style="border-color:#4B6C9E;border-style:Solid;display:none;" />
<input type="submit" name="ctl00$MainContent$btnSaveLineItems" value="Save" id="MainContent_btnSaveLineItems" title="Save changes?" style="border-color:#4B6C9E;border-style:Solid;display:none;" />
Then an event happens (selected index changed event of a drop down box) where I then do this:
然后发生一个事件(下拉框的选定索引更改事件),然后我执行以下操作:
btnSaveLineItems.Style["display"] = "";
btnSaveLineItems.Style["display"] = "";
I've also tried:
我也试过:
btnSaveLineItems.Style["display"] = "block";
btnSaveLineItems.Style["display"] = "block";
and both render the same HTML:
并且都呈现相同的 HTML:
<input type="submit" name="ctl00$MainContent$btnSaveLineItems" value="Save" id="MainContent_btnSaveLineItems" title="Save changes?" style="border-color:#4B6C9E;border-style:Solid;display:none;" />
<input type="submit" name="ctl00$MainContent$btnSaveLineItems" value="Save" id="MainContent_btnSaveLineItems" title="Save changes?" style="border-color:#4B6C9E;border-style:Solid;display:none;" />
采纳答案by Tim Schmelter
You can remove that style in this way:
您可以通过以下方式删除该样式:
btnSaveLineItems.Style["display"] = "";
or
或者
btnSaveLineItems.Style.Remove("display");
Edit:
编辑:
That doesnt work for me either...I wonder if it is because of the drop down list box is inside of an update panel and this button is outside of the updatepanel?
这对我也不起作用......我想知道是不是因为下拉列表框在更新面板内而这个按钮在更新面板之外?
Yes, you can only update the content of the current UpdatePanelin an asynchronous postback by default. The easiest would be to put your Button in another UpdatePaneland add the DropDownListas AsyncPostBackTrigger:
是的,UpdatePanel默认情况下,您只能在异步回发中更新当前内容。最简单的方法是将您的 Button 放在另一个中UpdatePanel并添加DropDownListas AsyncPostBackTrigger:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DdlChanged">
<asp:ListItem Text="Item 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Button ID="btnSaveLineItems" Text="click me" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" />
</Triggers>
</asp:UpdatePanel>
回答by Pankaj
btnSaveLineItems.Style["display"] = "block";
回答by user5588439
this works :
这有效:
gv.Style.Add(HtmlTextWriterStyle.Top, "-44px");
to add the style
添加样式
and
和
gv.Style.Remove("top");
to remove the style
删除样式

