Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After a day or two of pulling my hair out I managed to crack this... hope this helps others as frustrated as I was:</p> <p>Anyone that has found this post knows that the intuitive answer here, setting the default button of the form to the Next button of your Wizard, does nothing. So, the next logical step is to try and design around the problem by clicking the button on behalf of the user. Assume I've attached the below handler to my textbox from within the codebehind and that myButtonId is the id of the button I need as my default:</p> <pre><code> function textKeyDown(e) { if (e.keyCode == 13) { var myBtn = $get(myButtonId); myBtn.click(); } } </code></pre> <p>You would think the above would work just fine, but as it turns out, forcing the Wizard button to click was creating an extremely sneaky conflict - the first button on the page would click as well, overriding the initial button click I forced via script and creating very strange results. In my case, the first button in my master page is a button that says Home, and when the user clicks it, they go back to their home page. So in my attempts to force the Wizard's Next button to click I would end up right back at my homepage for what looked like no reason at all. After attaching to PageRequestManager.initializeRequest I was able to see I had two seperate postbacks initiating: 1 for my forced wizard button click, and 1 for a button that wasn't even on my radar at the time - the home page button. </p> <p>So, long story short, if I update the form's Default Button (Page.Form.DefaultButton) from within the code behind, then emit this script from within the code behind and reference it from my textbox as well, all works just fine:</p> <pre><code> public static void ExampleAssignDefaultButton(WizardStepBase activeWizardStep, TextBox myTextBox, IButtonControl myButton) { // create the format string for the script string javaScript = @"function defaultButton(src, evt){{ if(evt.keyCode == 13){{ var btn = $get('{0}'); setTimeout(new function() {{ btn.click(); }}, 5); }} }}"; // format it ... javaScript = String.Format(javaScript, ((WebControl)myButton).ClientID); // register it... ScriptManager.RegisterClientScriptBlock(activeWizardStep, activeWizardStep.GetType(), "defaultButton", javaScript, true); // assign it to my textbox myTextBox.Attributes.Add("onkeydown", "defaultButton(this, event);"); // also update the form's default button: myTextBox.Page.Form.DefaultButton = ((WebControl)myButton).UniqueID; } </code></pre> <p>It is worth noting that I always call the above method from the active wizard step's OnPreRender event. It's also worth noting that, because I only have one textbox per step in my wizard, I only had to assign this behavior to a single control. I have not attempted this fix scoped to catch the Enter key press across the whole page, though I can't imagine it'd behave too differently in that context. You probably don't need to catch the Enter key on each control within your wizard if you have lots of inputs per step.</p> <p>So, there you have it - only 12 hours of troubleshooting to do something that should have taken about 10 minutes. </p> <p>Happy coding.</p> <p>B</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload