0

I've found one way to skin this cat from this which led to this, but I'm wondering if it might be better to put all my "slide-uppable" elements on a div, and then slide up that div?

To be more specific, I'm creating controls in C# dynamically for a Sharepoint Web Page / Web Part. I conditionally need to hide or slide up "sections" of elements.

Theoretically (I think), I can create a DIV (Panel) like so (in C#, in my *.ascx.cs file):

Panel panelSec2 = new Panel(); // DIV
panelSec2.ID = "panelSection2";

... and then create the controls immediately thereafter like so:

boxRequesterName = new TextBox
{
    CssClass = "dplatypus-webform-field-input"
};

...then add these controls to the DIV/Panel:

panelSec2.Controls.Add(boxRequesterName);
. . .

In this way (presumably/theoretically), I can hide/slide up the panel like so in jQuery in the corresponding *.ascx file:

$(document).on("change", '[id$=ckbxPaymentForSelf]', function () {
    if (this.checked) {
        var $panelSection2 = $('[id$=panelSection2]');
        $(panelSection2).slideUp();
    }
});

...and not have to worry about designating the individual controls for hidation/slidation.

Am I right? Or is there something wrong in my theory?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

1

There's nothing wrong with your logic that I can think of.

One thing that might make things a little simpler on the JS side though would be to set the ClientIDMode to Static. Then you can use the more straight-forward '#ControlID' syntax instead of the attribute ends with syntax you've got now. You could also combine the two lines in the if statement to $('[id$=panelSection2]').slideUp()

  • I have to use the "ends with" because Sharepoint (or some slack-jawed miscreant) prepends a bunch of "gobbledygook" to the ID I supply, so that the name and ID turn out being something like: fajkd;;jlafdjl;dffdjl;panelSection2 – B. Clay Shannon-B. Crow Raven Jun 17 '15 at 21:19
  • Yes, it works just fine (with "$('[id$=panelSection2]').slideUp();") – B. Clay Shannon-B. Crow Raven Jun 17 '15 at 21:35
  • 1
    I'm a bit surprised to see semicolons in the supplied ID, and I'm not familiar enough with Sharepoint to say whether that is something specific to Sharepoint, but usually the IDs are autogenterated to include `$` or `_` separating a nested tree of parent IDs and the control number. [Here's an article](http://weblog.west-wind.com/posts/2009/Nov/07/ClientIDMode-in-ASPNET-40) with more detail on what I was talking about. Maybe it isn't applicable to Sharepoint, but it's worth looking into. – Christopher Jennings Jun 17 '15 at 21:36
  • No, that was just me bashing on the keyboard like a monkey or maniac to simulate the "greek" that is prepended to the ID. Nothing that could be confused with Shakespeare resulted, as you can attest. – B. Clay Shannon-B. Crow Raven Jun 17 '15 at 21:55
  • In actuality, the geekier-than-all-get-out string generated by SharePoint is: ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_panelSection2 – B. Clay Shannon-B. Crow Raven Jun 17 '15 at 22:03
  • 1
    In that case, you may actually be able to use the client id mode attribute/property and set it to static to achieve what I was describing. There's more detail in the article I posted in my first comment. – Christopher Jennings Jun 17 '15 at 22:05