oJM86o
oJM86o

Reputation: 2118

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

<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:

<input type="submit" name="ctl00$MainContent$btnSaveLineItems" value="Save" id="MainContent_btnSaveLineItems" title="Save changes?" style="border-color:#4B6C9E;border-style:Solid;display:none;" />

Upvotes: 12

Views: 55010

Answers (4)

Ayush Kubde
Ayush Kubde

Reputation: 1

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.

Upvotes: 0

user5588439
user5588439

Reputation: 21

this works :

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

to add the style

and

gv.Style.Remove("top");

to remove the style

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460158

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. The easiest would be to put your Button in another UpdatePanel and add the DropDownList as 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>

Upvotes: 16

Pankaj
Pankaj

Reputation: 10105

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

Upvotes: 1

Related Questions