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;" />
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.
@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.
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.
<%$ %> is an ASP.NET expression, used to bind configuration or resource file data during runtime.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With