I know this question has been asked several times, but I spent hours sifting through answers that do not correspond or do not work, and I am at the end.
Reference Information. I have a situation where I want to evaluate a record to make sure that it meets a specific set of criteria. If it meets the criteria, raise a warning message to confirm from the user. I do not want to raise a popup if the criteria do not match.
The pseudocode of what I want to execute is:
- The user enters information in several fields
- User clicks "Save" (cmdUpdate)
- in the Save function, which he checks to check if the same record exists in the database (for example, this is a duplicate).
- If this is not a duplicate, continue with the save function.
- If this is a duplicate, ask the user to confirm that the duplicate is saved.
I cannot get a popup to display before / after postback. I tried a workaround to set the session value to maintain state. The value checks for a positive value in the prerender and calls modalpopupextender.show, but it never fires successfully on the screen. I am not against switching to a javascript solution if someone has a better method, but I have to check for duplicates in asp.net code.
Markup:
<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;"> 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> <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>
Code behind:
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
source share