Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

hiding html li using c#

Posted on 2011-02-18
8
Medium Priority
?
3,286 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 61

Expert Comment

by:HainKurt
ID: 34929712
try

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

Expert Comment

by:HainKurt
ID: 34929731
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
ID: 34930127
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 61

Expert Comment

by:HainKurt
ID: 34930198
ok, try this

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

or

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

Expert Comment

by:HainKurt
ID: 34930207
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 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 34930209
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 75

Expert Comment

by:käµfm³d 👽
ID: 34930219
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
ID: 34954491
this worked great and is what I was looking for--thank you
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

916 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