Solved

hiding html li using c#

Posted on 2011-02-18
8
2,824 Views
Last Modified: 2013-12-17
I have a number of list items that are being used in a drop down menu.  Everything works fine but now I have to make 1 element visible or invisible based on a session variable.  The following is the element I am working with:
 <li id="hope" runat="server">
                <a href="tx-lonestar-landing.aspx">Cypress Lonestar Initiative</a>
                    <div>
                        <ul>
                            <li><a href="tx-lonestar1.aspx">Expanded Coverage</a></li>
                            <li><a href="tx-lonestar2.aspx">New Pricing Options</a></li>
                            <li><a href="tx-lonestar4.aspx">Enhanced Customer Service</a></li>
                            <li><a href="tx-lonestar5.aspx">New Incentives</a></li>
                        </ul>
                    </div>
                </li>
in the code behind, I put:
 protected System.Web.UI.HtmlControls.HtmlGenericControl hope;
        protected void Page_Load(object sender, EventArgs e)
        {
            hope.Visible = false;  
        }
I keep getting an error that I need to use the NEW keyword.  I don't see anyway to use it.  What is the code to do this.
0
Comment
Question by:fredstov
  • 4
  • 2
  • 2
8 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
try

hope.style.add("display","none");
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
remove this line

protected System.Web.UI.HtmlControls.HtmlGenericControl hope;

your code should be ok just with

hope.Visible = false;
0
 

Author Comment

by:fredstov
Comment Utility
Sorry, neither one of these items worked.  I did forget 1 item.  The li control is in a master page.  I have tried the items in the master page and in the main page and i get an error that 'hope does not exits in current context'.
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
ok, try this

Master.FindControl("hope").Visible = false;

or

Master.FindControl("hope").style.add("display","none");
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
if it is in a contol in master then find container first:

Master.FindControl("ContentID").FindControl("hope").Visible = false;

or

Master.FindControl("ContentID").FindControl("hope").style.add("display","none");
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Would something like this modification to your markup work? The "if" condition is rather psedocode, but should give you the idea.
<% if (Session["sessionKey"] == true) { %>
    <li id="hope" runat="server">
        <a href="tx-lonestar-landing.aspx">Cypress Lonestar Initiative</a>
        <div>
            <ul>
                <li><a href="tx-lonestar1.aspx">Expanded Coverage</a></li>
                <li><a href="tx-lonestar2.aspx">New Pricing Options</a></li>
                <li><a href="tx-lonestar4.aspx">Enhanced Customer Service</a></li>
                <li><a href="tx-lonestar5.aspx">New Incentives</a></li>
            </ul>
        </div>
    </li>
<% } %>

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
P.S.

I forgot to, but you can remove the 'runat="server"' at that point, unless you need it for something else.
0
 

Author Closing Comment

by:fredstov
Comment Utility
this worked great and is what I was looking for--thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now