Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

css

asp.net

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

like image 560
oJM86o Avatar asked Jun 20 '12 18:06

oJM86o


People also ask

How do you set display none in code behind?

Add("display", "none"); This will allow the div to render to the client but will assign it the display:none style so it doesn't appear. The sample from Steve which sets the server side visible property to false also hides the div. Additionally it keeps the div from rendering to the client.

How to hide div in code behind c#?

@SalGad not if you want to hide it using C# , the runat="server" makes it accessible to the CodeBehind where C# runs. @SalGad @Bazzz is right. You need to specify runat="server" if you want to do it in C#, as without it your C# code-behind will not "know" of your <div> element.

How do you change code behind style?

You can apply CSS style from code behind by accessing the properties of the control with the help of its Id. Most of the CSS styles can be applied using the Font property and its sub properties of the Label control. However you can use ForeColor and BackColor directly from the Label control.

What does <% %> mean in ASP?

<%$ %> is an ASP.NET expression, used to bind configuration or resource file data during runtime.


1 Answers

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>
like image 131
Tim Schmelter Avatar answered Oct 15 '22 16:10

Tim Schmelter