views:

939

answers:

2

I have a collapsible panel extender. I have no issues with the extender. however, I have a link that opens the panel and I want another link saying collapse to close it. I want to hide one show one javascript side. The issue is that it only works for the first row but not the others because I am not getting the unique ID or something. I haven't found a legitimate answer yet. I've tried jquery by getting parent elements and I was unsuccessful. What can I do?

Answer:

<asp:TemplateField HeaderText="Lng Descr" SortExpression="LongDescription">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LongDescription") %>' TextMode ="MultiLine" Rows="5" ></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>     
                <table>                  
                    <tr id="expand" style="display:">   
                        <td>    
                            <asp:Label ID="Label3" runat="server" ForeColor="Blue"><u></u></asp:Label>
                        </td>
                    </tr>
                </table>
                <asp:Panel ID="Panel1" runat="server" >
                    <table>
                    <tr>
                    <td>
                        <%#Eval("LongDescription")%>
                    </td>
                    </tr>
                    </table>
                </asp:Panel>
                <ajaxToolkit:CollapsiblePanelExtender ID="cpe" runat="Server" 
                 TargetControlID = "Panel1"
                 CollapsedSize="0"
                ExpandedSize="50"
                Collapsed="true" 
                ExpandControlID="Label3"
                CollapseControlID="Label3"
                AutoCollapse="false" 
                Scrollcontents="false" 
                collapsedText="<u>Expand"
                ExpandDirection="Vertical"
                ExpandedText = "<u>Collapse"
                TextLabelID="Label3" />
                </ItemTemplate>
            </asp:TemplateField>
+2  A: 

This is very easily done with jQuery. In your panel, declare a cssclass, say "panel", and on your label declare a css class, say "toggle". Your jQuery would be:

$(document).ready(function(){
    $(".toggle").toggle(function (){
        $(this).text("Collapse");
        $(this).next(".panel").slideDown("fast");
    },function () {
        $(this).text("Expand");
        $(this).next(".panel").slideUp("fast");
    });
});

You can ditch the ajax toolbox control, too. Of course, you also must declare .panel to display: none; in your CSS. Also note, you may have to get rid of your tables around your labels for this to effectively use the "next" function. You also only need one label that will change its text back and forth:

 <asp:LinkButton ID="view" runat="server" text="Expand" cssclass="toggle"> 
 <!-- You may alternatively use a standard link here or even a <p> tag, like this
 <p class="toggle">Expand</p>
 -->
 <asp:Panel ID="Panel1" runat="server" cssclass="panel">
       <table>
                <tr>
                <td>
                    <%#Eval("LongDescription")%>
                </td>
                </tr>
       </table>
 </asp:Panel>

EDIT

Here's the exact code I used to get this running for you. Note that I would normally pull the script and CSS out and put them in a separate file, but for all intents and purposes, this works (if you are using the 1.3.2 jquery file):

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
    <script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    <style type="text/css">
 .panel    {
  display: none;
 }
</style>

<script type="text/javascript">
$(document).ready(function() {
 $(".toggle").toggle(function() {
  $(this).text("Collapse");
  $(this).next(".panel").slideDown("fast");
 }, function() {
  $(this).text("Expand");
  $(this).next(".panel").slideUp("fast");
 });
});
</script>
</head>
<body>
    <form id="form1" runat="server">
 <p class="toggle" style="cursor:pointer;color:blue"><u>Expand</u></p>
 <asp:Panel ID="Panel1" runat="server" CssClass="panel" >
  <table>
  <tr>
  <td>
   <p>some text</p>
  </td>
  </tr>
  </table>
 </asp:Panel>               
    </form>
</body>
</html>
Jason
I edited the code I have. I have a little knowledge in Jquery but what am I doing wrong? When I click the button, it just submits the page.
Eric
if you are using a link, you have to disable the link's default function. try to use a <p> tag instead of a link, or use a standard link. i don't think you're using this link to send information to your server, right?
Jason
ps: you can disguise your <p> tag as a link by setting the css to `cursor:pointer;` :)
Jason
Instead of the link i used <p class="toggle" style="cursor:pointer;color:blue"><u>Expand</u></p>
Eric
is it working now?
Jason
shouldn't I call those functions somehow?
Eric
no... that's the way jquery works. when the DOM is ready (the whole `$(document).ready` part, it binds your .toggle classes to the "toggle" event. no need to add additional markup! isn't jquery a beautiful thing? :)
Jason
It most definitely is. But why do you think this isn't working too well for me?
Eric
is it still not working? it is possible that you need to put your script and your style rules in your header. also, make sure you're correctly referencing your jquery code file, maybe by using a <script> reference tag in your header: `<script src="js/jquery-1.3.2.js" type="text/javascript"></script>` (after you download the 1.3.2 file: http://www.jquery.com)
Jason
i updated my response... take a look!
Jason
that's most probably what my issue is - the version. I wouldn't have rights to download the latest here. It would have to be a company wide decision.
Eric
any other possible suggestions that you would know of with the current version i do have?
Eric
I love AJAX!! I couldn't download that version but i didn't realize how powerful that Ajax control was. You fought this thing with me so copy and past the code I will put above, and I'll give you the Accepted Answer.
Eric
Eric - I've modified my working version of the code to include a reference to Google's hosted version of the jQuery library. Although I'm glad you got it to work with the Ajax Control, remember there's extra overhead involved with it that is circumvented using jQuery. But again, I'm glad you got it working!
Jason
+1  A: 

I had success with collapsing using the example from this page: http://roshanbh.com.np/2008/03/expandable-collapsible-toggle-pane-jquery.html

I just use

.msg_head {
    cursor: pointer;
}

For the CSS.

And here is what is in my script.

<script type="text/javascript" id="js">
  $(document).ready(function() {

      //toggle the componenet with class msg_body
      $(".msg_head").click(function() {
         $(this).next(".msg_body").slideToggle(600);
      });
  });
</script>

<h2 class="msg_head">Subject<h2>

<div class="msg_body">
    Blah blah blah.
</div>
Woody2143