【问题标题】:Asp.net wizard control's Next button behavior seems to be getting altered by another control?Asp.net 向导控件的 Next 按钮行为似乎被另一个控件改变了?
【发布时间】:2016-07-17 18:02:52
【问题描述】:

我的应用程序中有一个 asp.net 向导控件。我无法解释我所看到的行为,但这就是它正在做的事情。

向导从用户那里收集数据,在向导第一步的底部,我有一个免责声明,说明用户同意条款和条件,并带有一个表示他们同意的复选框。我没有将所有术语都放在向导中,而是在另一个 aspx 页面中详细说明了所有这些。在向导中,我有一个声明,上面写着“我-您的名字-同意条款和条件。”条款和条件是一个 asp 链接按钮,它执行跨页回发到我的其他页面,并在新窗口/标签中打开。

当您单击“继续”按钮前进到向导的第二步时,事情变得疯狂。如果用户没有单击 LinkBut​​ton 来查看条款,那么一切正常 - 数据表单得到验证,向导继续下一步。但是,如果他们单击“条款和条件”链接按钮,它将返回到我的条款页面,并在该页面上显示带有输入名称的条款 - 应该如此。这是我无法解释的部分......当他们返回向导并单击“继续”按钮时,它会打开另一个条款和条件选项卡,并且不会进入下一个向导步骤。

这就像单击 LinkBut​​ton 控件以某种方式重新连接向导的下一步按钮应该执行的操作。但只有在单击 LinkBut​​ton 时才会发生这种情况。

这是我的向导声明...

<asp:Wizard ID="Wizard1" runat="server" CellSpacing="5" Font-Names="Verdana" DisplaySideBar="False"
            ActiveStepIndex="0" FinishCompleteButtonText="Submit" StartNextButtonText="Continue" Width="100%" OnNextButtonClick="Wizard1_NextButtonClick" OnFinishButtonClick="Wizard1_FinishButtonClick" ValidateRequestMode="Enabled">
            <StartNavigationTemplate>
                <asp:Button ID="StartNextButton" CausesValidation="true" runat="server" BackColor="White" BorderColor="#091C49" BorderStyle="Solid" BorderWidth="1px" CommandName="MoveNext" Font-Names="Verdana" Text="Continue" UseSubmitBehavior="False" />
            </StartNavigationTemplate>
            <StepNavigationTemplate>
                <asp:Button ID="StepPreviousButton" runat="server" BackColor="White" BorderColor="#091C49" BorderStyle="Solid" BorderWidth="1px" CausesValidation="False" CommandName="MovePrevious" Font-Names="Verdana" Text="Previous" />
                <asp:Button ID="StepNextButton" runat="server" BackColor="White" BorderColor="#091C49" BorderStyle="Solid" BorderWidth="1px" CommandName="MoveNext" Font-Names="Verdana" Text="Next" />
            </StepNavigationTemplate>
            <StepStyle VerticalAlign="Top" />
            <SideBarStyle BackColor="#507CD1" Height="100%" HorizontalAlign="Left"
                VerticalAlign="Top" Width="140px" />
            <NavigationButtonStyle BackColor="White" BorderColor="#091c49" BorderStyle="Solid"
                BorderWidth="1px" Font-Names="Verdana" />

这是第一个向导步骤底部的复选框和链接按钮。

<asp:CheckBox ID="cbSignature" runat="server" Checked="false" OnClick="ShowHideCVText()" />
                                <asp:CustomValidator runat="server" ID="cvCheckBoxRequired" EnableClientScript="true" Font-Bold="true" OnServerValidate="CheckBoxRequired_ServerValidate" ClientValidationFunction="ValidateCheckBox" ErrorMessage="You must indicate that you have read and agree to the terms and conditions of this lease by checking the chekcbox below to continue." ForeColor="Red">*</asp:CustomValidator> 
                                I <asp:Label ID="lblSignatureName" runat="server" /> have read and fully understand the <asp:LinkButton ID="lbLeaseTerms2" runat="server" OnClientClick="window.document.forms[0].target='_blank';" CausesValidation="false" PostBackUrl="~/LeaseAgreement" Text="Terms &amp; Conditions" />  of the lease agreement between blah blah blah...

这可能与我用来使 LinkBut​​ton 在新窗口中打开的技巧有关吗?似乎不太可能,因为它在单独的控件上,但我不知道。

OnClientClick="window.document.forms[0].target='_blank';"

这是从第 1 步到第 2 步时代码后面发生的唯一事情...只是用输入的文本值填充一些标签控件。

protected void Wizard1_NextButtonClick(object sender, System.Web.UI.WebControls.WizardNavigationEventArgs e)
{
    lblCustomersName.Text = txtFirstName.Text + " " + txtLastName.Text;
    lblCustomersPhone.Text = txtPhoneNumber.Text;
    lblPhoneType.Text = rblPhoneType.SelectedItem.Text;
    lblCustomerStreetAddress.Text = txtStreetAddress.Text + Environment.NewLine +
                                    txtCity.Text + ", " + ddlState.SelectedItem.Text + " " + txtZip.Text;
    lblCustomersEmail.Text = txtEmailAddress.Text;
    lblParkingLength.Text = ddlLengthNeeded.SelectedItem.Text;
    lblVehicleYear.Text = txtVehicleYear.Text;
    lblRegisteredOwner.Text = txtRegisteredOwner.Text;
    lblVehicleMake.Text = txtVehicleMake.Text;
    //lblVehicleLicensePlate.Text = txtLicensePlate.Text;
    lblLeaseDuration.Text = ddlLeaseDuration.SelectedItem.Text;
    lblSummarySignature.Text = txtFirstName.Text + " " + txtLastName.Text;
}

在这一点上完全混淆了为什么单击的链接按钮会改变向导的继续按钮的行为。

【问题讨论】:

    标签: asp.net wizard


    【解决方案1】:

    除非您使用 OnClientClick="return false;",否则表单中的任何 asp 按钮都会提交表单这在这种情况下是不可取的,因为您使用的是 onclick,这将禁用 onclick。

    自从按钮提交表单后,它就会提交到该表单的目标,而您已经更改了该目标(这几乎是您自己想出来的)。

    如果我是你,我会使用标准的 HTML 链接而不是链接按钮:

    <a href="~/LeaseAgreement" target="_blank">Terms &amp; Conditions</a>
    

    或者,如果您确实需要将其作为链接按钮,您可以使用 javascript setTimeout 函数将表单目标重置为 '',但我不确定 '' 是否可以在没有先测试的情况下工作。

    【讨论】:

    • 感谢您的回复。不幸的是,我确实需要使用链接按钮(或我想的按钮),因为指向条款页面的链接是帖子,而不是获取。我需要条款页面中显示的表单中的信息(用户输入的名称)。我也真的不想使用查询字符串。我以前没有使用过会话变量,但似乎这是唯一的其他选择,除非有人知道如何发布到不同的页面。根据我的理解,一个简单的 html 锚标记不会这样做。
    • 这样的话,看我最后一句话;保持 OnClientClick="window.document.forms[0].target='_blank';"但在分号后添加一个 settimeout 以将表单目标更改回 ''
    • 我现在明白你的意思了。我相信这将解决定位问题,但不会将页面设置为回发到源页面。它可能仍会回发到条款页面。只有在单击链接按钮时才需要这样做。如果单击向导按钮,它需要回发到自身。不过,我对正在发生的事情有了更好的了解。谢谢!
    【解决方案2】:

    所以在阅读了上面 Jack Arbiter 的 cmets 之后,这帮助我找出了目标问题。基本上我从链接按钮的 OnClientClick 事件中调用下面的函数,它将目标设置为一个新窗口,然后等待一秒钟并将其重置回自身。

    function handlePostBack() {
            window.document.forms[0].target = '_blank';
    
            setTimeout(function () {
                window.document.forms[0].target = '_self';
            }, 1000);
        }
    

    为了找出回发问题,我实际上是在查看向导的设计器视图时偶然发现的。如果您转到设计视图并使用“>”按钮展开向导控件,您会注意到那里有一个“编辑模板”选项。我选择了它,然后不得不再次使用“>”按钮扩展该 Designer 事物。一旦我这样做了,我就可以选择“开始导航模板”,因为那是我的“继续”按钮所在的位置。当您单击 Continue 按钮时,您将获得在属性窗口中显示的按钮的所有属性,包括 PostBackUrl。我注意到它没有设置,因为我相信它默认为 PostBack 本身。我明确将其设置为回发给自身并哇啦...问题解决了。

    这是它对标记的影响:

    <StartNavigationTemplate>
                    <asp:Button ID="StartNextButton" CausesValidation="true" runat="server" BackColor="White" BorderColor="#091C49" BorderStyle="Solid" BorderWidth="1px" CommandName="MoveNext" Font-Names="Verdana" Text="Continue" UseSubmitBehavior="False" PostBackUrl="~/LeaseOnline.aspx" />
                </StartNavigationTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 2013-07-03
      • 2018-10-31
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      相关资源
      最近更新 更多