views:

358

answers:

2

I have 2 sections of my website I am developing, a reference box that has a fixed width and a working box that has the same height as the content. I am trying to make it so the user can swap the content between the two boxes. I have the following type of setup in the aspx page:

<asp:panel id="pnlReference" runat="server" CssClass="referencePanel" >
    <asp:panel id="pnlsection1" runat="server" >
        Content....
    </asp:panel>
</asp:panel>

<asp:linkbutton id="lbtSwapPanels" runat="server" />

<asp:panel id="pnlWorking" runat="server" CssClass="workingPanel" >
    <asp:panel id="pnlSection2" runat="server" >
       Content....
    </asp:panel>
</asp:panel>

What I am trying to have occur is when I press the lbtSwapPanels linkbutton, it moves pnlSection1 into pnlWorking and pnlSection2 into pnlReference. The code I used to do this in the OnClick method was:

    Control pane1, pane2;

    pane1 = pnlWorking.Controls[0];
    pane2 = pnlReference.Controls[0];

    // Remove them from their respective panels
    pnlWorking.Controls.Remove(pane1);
    pnlReference.Controls.Remove(pane2);

    // Add them to the opposite pane
    pnlWorking.Controls.Add(pane2);
    pnlReference.Controls.Add(pane1);

Unfortunately, this does not work. When I click the linkbutton, nothing happens. If I then do something to perform another postback the reference and working panels become empty. I assume this has to do with the change not being saved into Viewstate but I don't know how to get around that.

Does anyone have any ideas on how to do this?



Update: It seems that moving objects around with Jquery is causing issues with asp.net postbacks as well as making my asp.net ajax tabcontainer completely fail to function. After 2 swaps and postbacks, further postbacks cease to function at all. Here's the new code

<div id="referencePane">
  <asp:panel id="pnlsection1" runat="server" >
    Content with tab container
  </asp:panel
</div>
<a href="#" onclick="SwapPanes()">Swap Panes</a>
<div id="workingPane">
  <asp:panel id="pnlsection2" runat="server" >
    Content
  </asp:panel>
</div>

Here's the javascript:

function SwapPanes() {
    var pane1, pane2;    

    pane1 = $("#workingPane").html();
    pane2 = $("#referencePane").html();
    $("#workingPane").empty();
    $("#referencePane").empty();

    // Add them to the opposite pane
    $("#workingPane").prepend(pane2);
    $("#referencePane").prepend(pane1);
}

First postback causes the tabcontainer to fail (javascript exceptions claiming it's trying to create a tab container with teh same ID (only one exists in the original aspx page). Postbacks then proceed to do wierd stuff.

+1  A: 

Have you thought about keeping the controls in the panel but just swapping the position of the panels? You can do this pretty easily in JQuery although you'll be using DIVS instead of ASP.NET panels. This is, again, quite easy since Panels just translate to DIVS in the end. I did this recently for a wizard-style questionnaire (moving from panels to divs) and I was surprised how easy it was.

Update: Note that, when you swap the div positions, you can change the style as well (again, easy in JQuery) so that the user won't have a jarring "why did these two things change position" experience.

The bottom line, really, is that I think you are trying to use a hammer to drive a screw. JQuery is the screwdriver you are looking for and learning it is very much worth your while!

Mark Brittingham
Hrm good point. I originally thought of using JQuery, but then I thought viewstate would get in the way. I'll look more at using that thanks.
KallDrexx
Viewstate is pretty much orthogonal to JQuery. I use JQuery with WebForms and the only issue I ever have is sometimes having to work through the long IDs used by WebForms. This isn't a *big* deal but I am certainly looking forward to the formal release of .NET 4.0 (which gives a way to define your own IDs).
Mark Brittingham
By viewstate interfereing I am more referring to modifications made through JQuery getting reset on an ajax postback (which is used heavily in this section due to the nature of my web application). I'm going to go through some JQuery tutorials and learn it, it will probably be good to know for better optimizations and usage once my application leaves the prototyping stage anyway.
KallDrexx
Unfortunately, using jquery with asp.net controls does not seem to work well. Upon clicking my link, jquery swaps the html between the two div statements (replace <asp:panel> calls with <div> in the original code and you will have what I have now). I'm swapping by storing the html in a var, empty()ing the div, and putting the html in the other div. Unfortunatly, this causes all asp.net controls to cease to function properly, and postbacks cause everything else to mess and fail even more.
KallDrexx
Kall - If the controls within the Div are asp.net controls, this is what I'd expect would happen. Remember that they are part of the "partial class" defined on the page (even though the declarations are hidden). There are two ways around this: you could just swap *positions* and leave the divs otherwise intact or you could use HTML controls (which are pretty straightforward, actually - I use them in a couple of different situations). Question, though, why did you decide to wipe things clean and recreate? Why not just swap positions as I originally suggested? I imagine you had a reason.
Mark Brittingham
Honestly, it's because it's the only way I could think to do it. I'm not the best with CSS/HTML I will admit. I'm not quite sure how to move the bottom div above the top div at runtime, without the moving div collide with data at the top of the screen or with the sidebar.
KallDrexx
Well, from your description, I was under the impression that the Divs were next to each other. If you start with a Div whose position is relative (style="position:relative;") and then place your two Divs inside and make their position absolute (style="position:absolute;") then the position of the two target divs (e.g. left, top) will be *relative* to the parent Div. You'd then just use jquery to change the Left and Top (or whatever) attributes to move them around. I did make some assumptions about how common this knowledge is so please accept my apology for not including more in the answer.
Mark Brittingham
Btw - this is pretty cool stuff when you get it right and it will definitely give you the benefit of some valuable experience. I know when I've bitten off stuff like this in the past there is always some pain associated with it but some real satisfaction when I got it right.
Mark Brittingham
No problem, I wouldn't be attempting to do this if I wasn't up for learning :) I just got a bit frustrated cause this should have been the easiest part of my prototype heh. I guess I should have described the physical layout of the two sections better. The layout is designed where you have a reference section, which is at the top and has a max height of X pixels and is the width of the screen, and a working section that can be any height and width of the screen. I am trying to make it so you can swap the content between the fixed and dynamic height panels.
KallDrexx
I think I get what you are saying now though. I can probably put the properties (including location) into 2 CSSClasses, put both divs into a parent div, and then on the swap link button press I can then swap the classes of the divs so the correct div is put at top with a fixed height. Seems to make sense at least, I will give it a shot when I get off of work. Thanks :)
KallDrexx
Yes! That is really the most straightforward way to approach it. Good luck and let me know how you make out.
Mark Brittingham
Well 2 days of getting frustrated with vertically positioning stuff in CSS, it works well now. It seems to work flawlessly, except for it being ugly due to my lack of CSS experience :). Thanks a lot!
KallDrexx
You are very welcome. If you want to learn more about CSS, check out http://www.alistapart.com (use the search box) and, especially, Eric Meyers' books on the subject!
Mark Brittingham
A: 

Look into ASP.Net web parts. That will manage the whole thing for you in a dynamic and responsive way.

Joel Coehoorn