Data View Controls: Form Action Button and Dynamic Redirect

Andrew Sapp just saved me countless hours of frustration.
 
For the past few days I have been trying to convert a lengthy paper-based form into a SharePoint form.  I needed to be able to use workflows on list items (nothing special, just approval) and export the form data (several RTF fields).  Also, I knew I personally wouldn’t be happy until the form worked like a survey–answer a question, click ‘save-the-answer-and-continue’, answer another question, etc.  This would prevent user frustration with a long form making them scroll or a wayward click wiping out 20 minutes of data entry.
 
Well, the SharePoint survey let me down on a number of levels.  First, I know from past experience that you can’t workflow a survey.  Second, the data seems only able to export as Excel data (may not work with older versions of Excel and lengthy RTF data).  Third, it gave me no clues into how to replicate the "Next>" function in a Data View or Custom List From web part.
 
I tried the Data View web part, and I was really happy with my early goings since I could [apparently] change the behavior of the form buttons.  However, by using the UI to add a redirect action and clicking on Parameters, then selecting {@ID}, the best results yeilded "…aspx?ID={@ID}".  SharePoint Designer simply didn’t understand that my Parameter, already defined on the page, needed to actually be dynamic in the redirectURL.  So, into the code I treaded.
<input type="button" value="Form Action" name="btnFormAction" onclick="javascript: {ddwrt:GenFireServerEvent(‘__commit;__redirect={EditForm.aspx?@ID_x003D__x007B_@ID_x007D_}’)}" />
No matter where I googled, I couldn’t find the syntax for that javascript action.  Changing the URL-encoded brackets, adding quotes, changing backets, nothing worked.  Then I found Andrew’s post, replying to a blog about C# code-behind redirect (something over my head and not where I wanted to go–it should be easier than this!).
 
Since the ID parameter is already on my copied EditForm page, I followed Andrew’s code and did the following:
  1. Right-click on the web part and pull up web part properties, click "…" under Parameters Editor.
  2. Checked to make sure my dynamic element is there and is formatted like Andrew’s
    1. <ParameterBinding Name="ListItemId" Location="QueryString(ID)" DefaultValue="0"/>
  3. Closed that editor and opened the XSL Editor.
  4. Inserted the following between lines 5 and 6 of the stylesheet:
    1.  <xsl:param name="ListItemId" />
       <xsl:variable name="RedirectLoc">EditForm.aspx?ID=<xsl:value-of select="$ListItemId"/></xsl:variable>
  5. Saved my new properties.
  6. Replaced the standard EditForm web part with Custom List Form (see previous blogs about customizing forms).
  7. Added a Form Action Button to the web part (Insert > SharePoint Controls > More SharePoint Controls).
  8. Changed my form button code to the following:
    1. <input type="button" value="Save" name="btnTopSave" onclick="javascript: {ddwrt:GenFireServerEvent(concat(‘__commit;__redirect={’,$RedirectLoc,’}’))}"/>
  9. Saved my page.
  10. Tested the page (changed a value, hit the custom button.  The page updated and redirected from EditForm1.aspx to EditForm.aspx and pointed to the same record).
 

4 thoughts on “Data View Controls: Form Action Button and Dynamic Redirect

  1. Actually, it can be easier than this. In step 4, make sure your xsl stylesheet contains . Then skip to step 8, and change the form button code to:

Leave a comment