Setting UL to be visible from code-behind not working
This is weird. It's got to be something stupid.
I have the following in my page (it's located in a user control in my page)
<div id="notifications" class="notificationsContainer" runat="server">
<ul id="notificationsList" visible="false" runat="server">&l开发者_运维知识库t;/ul>
</div>
And the last thing that's run in the .ascx.cs's Page_Load is this line of code:
if (!string.IsNullOrEmpty(notificationsList.InnerHtml))
notificationsList.Visible = true;
it IS hitting the line notificationsList.Visible = true; but for whatever strange reason, the <ul> is still not visible. I've stepped through this and I don't see any reason why such as other code conflicting, etc.
Here's the full method that is last to be called in Page_Load:
private void SetAndShowNotifications()
{
notifications.Visible = false;
SetItemRemovedNotification();
if (!string.IsNullOrEmpty(notificationsList.InnerHtml))
notificationsList.Visible = true;
}
I just can't see it by stepping through as to why the hell this would not stick!
I think you're setting the div's Visible to false and trying to set its child element's Visible to true...
notifications.Visible = false;
...
if (!string.IsNullOrEmpty(notificationsList.InnerHtml))
notificationsList.Visible = true;
Try replacing notifications.Visible = false with notificationsList.Visible = false.
You're hiding the "notifications" div on this line:
notifications.Visible = false;
Then trying to show the "notificationsList" ul afterwards:
notificationsList.Visible = true;
As long as the "notifications" div is hidden, it doesn't matter whether your "notificationsList" is visible or not since it's a child element. Change the logic inside the if statement to look like this:
if (!string.IsNullOrEmpty(notificationsList.InnerHtml))
{
notifications.Visible = true;
notificationsList.Visible = true;
}
It depends where do you set the visibility. Here's a working example with a link button which shows the ul
when the link is clicked:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/C#" runat="server">
protected void ShowClick(object sender, EventArgs e)
{
notificationsList.Visible = true;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="notifications" class="notificationsContainer" runat="server">
<ul id="notificationsList" visible="false" runat="server">Hello</ul>
</div>
<asp:LinkButton ID="btnShow" runat="server" Text="Show" OnClick="ShowClick" />
</form>
</body>
</html>
At which point in the page life cycle are you trying to run this code? You should be doing this within OnPreRender, or even page load. Also if the UL isn't working why don't you switch it out for a placeholder which doesn't produce any markup and up would you the same result.
精彩评论