ModalPopupExtender不在updatepanel中的按钮回发上显示

时间:2011-04-07 14:09:54

标签: asp.net ajax vb.net .net-3.5 modalpopupextender

我知道这个问题有多次被问过,但是我花了几个小时来筛选那些不匹配或不起作用的答案,而且我的智慧结束了。

背景:我有一种情况,我想评估一条记录,以确保它符合一组特定的标准。如果符合条件,请发出警报消息以供用户确认。除非标准匹配,否则我不想提升弹出窗口。

我想要完成的伪代码:

  • 用户在多个字段中输入信息
  • 用户点击“保存”(cmdUpdate)
  • 在“保存”点击功能中,它会检查是否已存在相同的记录 在数据库中(例如,这是重复的)。
  • 如果不是重复,请继续使用保存功能
  • 如果是重复提示,则用户确认保存dup。

我无法在回发之前/之后显示弹出窗口。我尝试过设置会话值以维持状态的黑客解决方法。该值在预渲染中测试为正,并且调用modalpopupextender.show但它从未成功触发到屏幕。如果有人有更好的方法,我不反对切换到JavaScript解决方案,但我必须检查后面的asp.net代码中的重复项。

标记:

<asp:UpdatePanel ID="upMainContent" runat="server" UpdateMode="Conditional"  ChildrenAsTriggers="False" >
    <ContentTemplate>
    <asp:Label ID="lblDummy" runat="server" EnableViewState="false" Style="display: none;" />
    <asp:Panel ID="pnlConfirmation" runat="server" Width="400px" Style="display: none;" CssClass="ModalPopupFront">
        <div ID="Div1" runat="server" class="PopupHeader" style="width:400px;">&nbsp;&nbsp;Duplicate Record</div>
        <br />
        <asp:Label ID="lblConfirmationMessage" runat="server" Text="This record has already exists.<br/> Are you sure you want to save a duplicate entry?"></asp:Label><br />
        <br />
        <div style="text-align:right;">
        <asp:Button ID="btnSaveAnyway" runat="server" Text="Save" OnClick="btnSaveAnyway_Click" />
        <asp:Button ID="btnCancelSave" runat="server" Text="Cancel" OnClick="btnCancelSave_Click"  />
        </div>
    </asp:Panel>
    <ajax:ModalPopupExtender ID="mpeSaveConfirmation" runat="server" Enabled="False" 
        TargetControlID="lblDummy" PopupControlID="pnlConfirmation" 
        BackgroundCssClass="modalBackground" DropShadow="true" 
        CancelControlID="btnCancelSave"
        RepositionMode="RepositionOnWindowResizeAndScroll" PopupDragHandleControlID="pnlConfirmation" Drag="true">
    </ajax:ModalPopupExtender>


    <!-- all the input fields/misc content -->

     <asp:Button id="cmdUpdate" runat="server" CausesValidation="true" OnClick="cmdUpdate_Click" Text="Save" ValidationGroup="vg1"  ToolTip="Save the current record" TabIndex="102" />

    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlStateId" EventName="SelectedIndexChanged" />
        <asp:AsyncPostBackTrigger ControlID="ddlCountyId" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel> 

代码背后:

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        '...

        If GetSessionValue("HackWorkaround") Then
            mpeSaveConfirmation.Enabled = True
            mpeSaveConfirmation.Show()         
        End If        
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '...
        If not Page.IsPostBack Then
            SetSessionValue("HackWorkaround", False)
        End if
        '...
    End Sub

 Protected Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)


        If tbOpTill.NewRecordIdenticalToLast() And tbOpRecord.NewRecordIdenticalToLast() Then
            SetSessionValue("HackWorkaround", True)
        Else
            SetSessionValue("HackWorkaround", False)
            SetSessionValue("LastOpRecordIDSaved", tbOpRecord.OpRecordId)

            Dim isEdit As Boolean = ResetOpRecord("Till", tbOpTill)
            SmartRedirect("Optill/oprecord_edit.aspx")
        End If

    End Sub


Protected Sub btnSaveAnyway_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    SetSessionValue("HackWorkaround", False)
    mpeSaveConfirmation.Enabled = False
    mpeSaveConfirmation.Hide()
    'Duplicate record exists, but the customer wants to save anyway.
    DoSave()

    Dim isEdit As Boolean = ResetOpRecord("Till", tbOpTill)
    SmartRedirect("Optill/oprecord_edit.aspx")
End Sub

Protected Sub btnCancelSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    SetSessionValue("HackWorkaround", False)
    mpeSaveConfirmation.Enabled = False
    mpeSaveConfirmation.Hide()
    'do nothing and return to the screen.
End Sub

1 个答案:

答案 0 :(得分:2)

您的问题在这里:

<ajax:ModalPopupExtender ID="mpeSaveConfirmation" runat="server" Enabled="False"

Protected Sub btnSaveAnyway_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    SetSessionValue("HackWorkaround", False)
    **mpeSaveConfirmation.Enabled = False**
    mpeSaveConfirmation.Hide()

在后面的代码中使它成为现实。和sync同步隐藏显示。此外,我可以在您的代码中看到您正在使用Style =“display:none”的某些地方。所以如果你想显示你需要使用HtmlStyleWriter.Display,“阻止”。如果在这种情况下使用Visible true false,它将无效。我的意思是说,无论你在哪里使用可见的真假,在代码隐藏中你必须使用类似的。如果你正在使用样式,那么在代码隐藏中你必须使用相同的。