More in this section

Forums / Developing with Sitefinity / Forms - View, Manipulate data

Forms - View, Manipulate data

7 posts, 0 answered
  1. Matt
    Matt avatar
    4 posts
    Registered:
    10 Oct 2012
    31 Jan 2013
    Link to this post
    This has got to be elementary, but I cannot figure it out. I just need to have access to the data on a current form. All the examples I find only explain how to get data from submitted forms.

    One specific example: we re-use an application form for different physical site locations. Depending on how the user gets to the form, I need to change a default value in a drop-down field on the form. Pre-Sitefinity, this was just done in the code-behind: check a query string value, flip the value on the field. But in my class on the Sitefinity form, I can't seem to determine a way to "find" the control. The .Net id is buried under 12 layers of nested generated controls, and the "advanced name for developers" is buried in some JS.

    Please point me to the relevant documentation or explain how to do this. Thanks!
  2. Victor
    Victor avatar
    94 posts
    Registered:
    15 Jun 2011
    01 Feb 2013 in reply to Matt
    Link to this post
    You could approach this two ways.
    Server side.  Create a control overriding Telerik.Sitefinity.Modules.Forms.Web.UI.FormsControl
    In the override event for InitializeControls you can run the following code to get all the controls on the form:
    foreach (IFormFieldControl fc in this.FieldControls)
                   {
                   //run code here
                   }

    Or client side you can use jquery to select the combobox and set the value on page load.
  3. Matt
    Matt avatar
    4 posts
    Registered:
    10 Oct 2012
    01 Feb 2013 in reply to Victor
    Link to this post

    This is an existing page on a Sitefinity site, with an existing Sitefinity form. I'm adding the code-behind to the page as the documentation explains by referencing my custom assembly in the page properties: Edit Page > Title and Properties > Code behind type (for ASP.NET developers).

    If I change the class from extending System.Web.UI.Page to Telerik.Sitefinity.Modules.Forms.Web.UI.FormsControl, I get the runtime error 'mynamespace' is not allowed here because it does not extend class 'System.Web.UI.Page'

    FieldControls does not exist for System.Web.UI.Page. There is no Title and Properties option to add a code-behind file to the Sitefinity form.

    I've also tried using this.FindControl, but you have to know the control id. Looking at the source of the rendered page, the ID is "C001_ctl00_ctl00_C014_ctl00_ctl00_dropDown". How do I know this ID from code-behind? Same with jQuery - how do I determine this ID? 

    If I view source, in the Telerik js init method sets a "dataFieldName" for the control to "FormDropDownList_C014", which appears to be static. But I don't see how to select the control using this value.

    Thanks for any help, this is driving me crazy!

  4. Victor
    Victor avatar
    94 posts
    Registered:
    15 Jun 2011
    01 Feb 2013 in reply to Matt
    Link to this post
    Sorry if I wasn't very clear.

    What I meant was to create a custom "control" (widget) based on the formsControl and use that in place of the built in SF form control.

    As far as the jquery, when you setup the form you can pick the developer name for the field.  Say you pick "MyDropDown", then use jquery to select it like this:
    $('select[id*="MyDropDown"]')
  5. Matt
    Matt avatar
    4 posts
    Registered:
    10 Oct 2012
    01 Feb 2013 in reply to Victor
    Link to this post
    Ugh. So every control on every form on every page has to be custom in order to access its data?

    The id is not selectable like you mention: I created a new test form, with a new dropdown list and set the advanced developer name to "ddlTestSelection". Still, the page renders it as a generated id (e.g. "C002_ctl00_ctl00_C001_ctl00_ctl00_dropDown"). Using the console, $('select[id*="ddlTestSelection"]') returns [ ].
  6. Victor
    Victor avatar
    94 posts
    Registered:
    15 Jun 2011
    01 Feb 2013 in reply to Matt
    Link to this post
    You are indeed correct.  The developer name only works server-side.

    I would just cheat here.  
    How many dropdowns are on the page with ids that end in "_dropDown"?

    use $('select[id*="_dropDown"]')[x] (where x is the one you want)

  7. Matt
    Matt avatar
    4 posts
    Registered:
    10 Oct 2012
    01 Feb 2013 in reply to Victor
    Link to this post

    Thanks for the help Victor, I think I've finally got a solution. The "cheat" suggestion didn't really work for me as the page has many drop downs and various other controls - it returns every one (e.g. 3 dropdowns), with still no good way to determine which is which.

    However, thankfully I finally found the right advanced menu and made the right changes to get the IDs to come across:

    • edit the form (that's embedded in the page)
    • edit the control (e.g. the drop down)
    • go in to the advanced view
    • set ClientIDMode to Static
    • set ID to a custom value (e.g. 'myDropdown')

    Rinse and repeat for all controls on the form.

    This then enables me to do something like 

    $('#myDropdown #dropDown').val('myNewValue');

    to access and edit the control values. Note - the [second] 'dropDown' id is generated by Sitefinity once the ClientIdMode is set to Static.

    Unfortunately, the controls are STILL not visible from code-behind. It seems that they get generated by the Sitefinity client-side JS inits after Server Render completes. 

    I can live with moving all the logic to JS, though so I'm happy now. :-)


7 posts, 0 answered