简体   繁体   English

显示无/删除样式 asp.net 后面的代码不起作用

[英]display none / remove style for asp.net code behind not working

I've got a button on my page in code behind I do this:我在代码后面的页面上有一个按钮,我这样做:

btnSaveLineItems.Style.Add("display", "none");

But later I want to show that button so I tried this:但后来我想显示那个按钮所以我试了一下:

btnSaveLineItems.Style.Clear();

This doesnt appear to reshow the button... The html markup in the beginning has a "style=display:none;"这似乎没有重新显示按钮...开头的 html 标记有一个“style=display:none;” in the beginning of the page.. and it maintains that style even though I try to remove it?在页面的开头..即使我试图删除它它仍然保持这种风格?

When my page first starts up I have this:当我的页面第一次启动时,我有这个:

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;" />

Then an event happens (selected index changed event of a drop down box) where I then do this:然后发生一个事件(下拉框的选定索引更改事件),然后我执行此操作:

btnSaveLineItems.Style["display"] = "";

I've also tried:我也试过:

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;" />

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 UpdatePanel in an asynchronous postback by default.是的,默认情况下,您只能在异步回发中更新当前UpdatePanel的内容。 The easiest would be to put your Button in another UpdatePanel and add the DropDownList as AsyncPostBackTrigger :最简单的方法是将您的 Button 放在另一个UpdatePanel ,并将DropDownList添加为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>

this works :这有效:

gv.Style.Add(HtmlTextWriterStyle.Top, "-44px");

to add the style添加样式

and

gv.Style.Remove("top");

to remove the style删除样式

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

You can Just add the class d-none by getting it class name and then remove the class d-none where you want to show the button.您可以通过获取 class 名称来添加 class d-none,然后删除要显示按钮的 class d-none。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM