C# Show / Hide Panels

Joined: 11/28/2008

For a few months I've been working a *very* little bit with ASP.NET (specifically C# but a little VB). I learn best by examples but I can't find exactly what I want to do online so I'm hoping you guys can help me.

The idea is pretty simple, I want a FAQ page with the questions on it and when you click on a question link the answer then displays right under it. Click the link again (or click a different link) and the answer hides.

As far as I can tell the best way to do this is with panels...I know I need 3 things:
1. Panel Code
2. Link Button Code
3. Script Code

I have 1 and 2 but I need help with 3. It would be some kind of "onclick" type function but I'm a bit hazy as to how to tell it how to make one specific panel visible and close all others.

Also hazy as to how to make a multi-purpose function that could control all the panels without hard coding for each panel.

Not sure if this makes sense but any help would be appreciated.

As a bonus I would like one extra link button that shows/hides all answers with one click.

Thanks for any help!
Chris

Joined: 11/28/2008
hmm I was trying to find a

hmm I was trying to find a link to the Visual Web Desinger learning video.

One way you could do this is out of a database.

three fields in a data base
1. ID
2. Name
3. Information.

basically you show a list of the "name" coloum. when you click on the name. the below it the information will show up. I have done this a couple of times with alot of information.

ID is the primer for what to show

I hope that this will help, but try the Free learning videos, they will help with this example.

Joined: 11/28/2008
One way you could do
QUOTE
One way you could do this is out of a database.

I was trying to find a way to do it strictly "on page" with panels and without a dbase because I dont have access to one.

Thanks,
Chris

Joined: 11/28/2008
Hi guys,try out

Hi guys,

try out http://skmfaqs.net,

Gavin

Joined: 11/28/2008
Thanks Gavin...right idea but

Thanks Gavin...right idea but it uses a dbase too. I was trying to do it strictly with text and panels if there was a way.

QUOTE(i386 @ May 4 2007, 05:10 PM)
Hi guys,

try out http://skmfaqs.net,

Gavin

Joined: 11/28/2008
So you want to do this with

So you want to do this with CSV file?

Joined: 11/28/2008
nhaas @ May 5 2007,
QUOTE(nhaas @ May 5 2007, 12:45 AM)
So you want to do this with CSV file?

Sorry guys, I'm sure I'm not being as clear as I could be.
What I really wanted to do was to NOT use any outside file...something like this

CODE
Psuedo Code:

FAQ Question 1
<panel1>
answer 1
<endpanel>

FAQ Question 2
<panel2>
answer 2
<endpane2>

etc...

Then have some directive that said:
if FAQ Question 1 link is clicked, show panel 1 and hide all other panels, click again hide panel
if FAQ Question 2 link is clicked, show panel 2 and hide all other panels, click again hide panel
if SHOW ALL link is clicked, show all panels, clicked again hide all panels.

There would be less than 20 questions total.

Hope that is a little clearer.

If I had to I guess I could use a CSV file. Was just trying to put it all together in one (I know, not separating code from content but...) /whistle.gif" style="vertical-align:middle" emoid=":whist:" border="0" alt="whistle.gif" />

I'm sure I could hardcode a bunch of if's but I did want it to be dynamic in the sense that if I added a new FAQ question I didn't have to hard code it.

Clear as mud? Probably. /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Thanks,
Chris

Joined: 11/28/2008
Hi Chris,Ok, I think I

Hi Chris,

Ok, I think I understand, what we need do is find a control in ASP.net which is close enough to your needs,

Lets use a ListBox control, it has the right structure but the output is a <select ....>

<asp:ListBox>
<asp:ListItem Text="Why is?" Value="Long Text"></asp:ListItem>
</asp:ListBox>

[thinking]
Now inherit it, override the rendering bit, add in our javascript to hide/show each FAQ. Probably need to change the properties Text to Question and Value to Answer. That's the general idea.[/thinking]

Actually I'm surprise someone hasn't written a control already for this...

let's see what I do ..

Joined: 11/28/2008
Ok, wrote an FAQ control, it

Ok, wrote an FAQ control, it works like this....

You can add the javascript in the Template part to hide/show each FAQ

protected void Page_Load(object sender, EventArgs e)
{
Faq1.Items.Add(new FAQItem("How many muppets in a show?",
"I don't know"));
Faq1.Items.Add(new FAQItem("How many muppets in a show?",
"I don't know"));
Faq1.Items.Add(new FAQItem("How many muppets in a show?",
"I don't know"));
Faq1.DataBind();
}

<form id="form1" runat="server">
<cc1:Faq ID="Faq1" runat="server">
<Template>
<p>
<strong>Q:<%# Container.Question %></strong><br />
A:<%# Container.Answer %><br /></p>
</Template>
<Items>
//-- This part I can't get working yet...
</Items>
</cc1:Faq>

</form>

Gavin :-)

Joined: 11/28/2008
It's been a while but for the

It's been a while but for the sake of completelness (and to help out others who may be looking for this). I have this problem figured out.

I don't think I ever completely stated the problem very well so I'm going to state the problem and solution here:

THE PROBLEM:
I had a page of FAQ questions and answers and I wanted to have a show/hide feature for each section. This was all to be in one page with no DB or CSV assistance. My initial thinking was to do this with .NET panels somehow.

THE SOLUTION:
The project actually got put on hold so I moved on and forgot about it...until recently when I need the function again on another page. So I started digging again. The solution I found was actually a JavaScript solution instead of a .NET solution as you can see below:

CODE
<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'none')
          e.style.display = 'block';
       else
          e.style.display = 'none';
    }
//-->
</script>

Then here's the link code:

CODE
<a href="#" onclick="toggle_visibility('sports');">show / hide products</a>

And finally the DIV that shows or hides:

CODE
<div id="sports" style="display:none">
          <p>Content to go here. </p>
</div>

In the last section the code style="display:none" makes the section hidden by default. Omit this to show the section.

Hope this helps some one. Giving credit where due, I found this code (and modified it) at http://blog.movalog.com/a/javascript-toggle-visibility/

Thanks,
Chris