简体   繁体   中英

Retrieve value from window.confirm, in code-behind. [VB.net]

I am doing a project where I have made a button that adds a product.

When the user clicks this it calls a function that adds the product, and then looks through my DataBase(CheckProdRef function), to see if this product has some "followup" products.

If this is the case, I want my sub to make a pop-up box, "This product has a follow-up product, do you wish to add?".

Then based on the answer from the user, I should add the product, or not.

However I have the dear problem that I cannot really seem to retrieve any meaningful value from the window.confirm .

My code is this:

Protected Sub BTN_EM_TILFØJ_Click(sender As Object, e As EventArgs) Handles BTN_EM_TILFØJ.Click
    Dim Lbl As New System.Web.UI.WebControls.Label

    Call AddRowTbl(GV_EM, "MAT")

    If CheckProdRef(TB_PROD_NR.Text) <> vbNullString Then

        Lbl.Text = "<script language='javascript'>" & Environment.NewLine _
& "window.confirm(" & "'" & "There is a followup product, do you wish to add it?" & "'" & ")</script>"
        Page.Controls.Add(Lbl)

The following is my desperate check to see if true/false was the value, which it is not

If Lbl.Text = "True" Then`  
    Labtest.Text = "Du trykkede ok"
Else
    Labtest.Text = "du trykkede cancel"
End If

and then it goes onxxxxxxxxxxxxx

I need a way to register if the user clicks yes or no. And I dont see how I can add the functionality to the button, as this pop-up only is shown IF there is a follow-up product(obv).

Assuming you are using ajaxcontroltoolkit, you can do this:

On your aspx add a block of code refering to the "popup" which will be showing the message to the user

<AjaxToolKit:ModalPopupExtender ID="mpeDiscrepancias" <-- ID to be used on code behind runat="server" TargetControlID="NuevaDiscrepanciaBtn" <-- button that will be popping out the message
            PopupControlID="DiscrepanciaPanel" <-- control that will be displayed BackgroundCssClass="modalBackground" <-- css class for the modal CancelControlID="CerrarDiscrepanciaBtn"<-- button that will be closing the modal
            Enabled="True">
        </AjaxToolKit:ModalPopupExtender>

        <div id="DiscrepanciaPanel" style="background-color: gray; display: none; max-height: 600px; overflow: auto;max-width:95%">

            <table style="text-align:left">
                <tr>
                    <td colspan="2" class="MainTitle">
                        YOUR MESSAGE
                    </td>
                </tr>
                <tr>
                    <td>

                    </td>
                    <td>

                    </td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                        <br />
                        <asp:Button ID="GuardarDiscrepanciaBtn" <-- save button runat="server" Text="Guardar" CausesValidation="true" ValidationGroup="Discrepancias" OnClick="GuardarDiscrepanciaBtn_Click" <--on click action />
                        <asp:Button ID="CerrarDiscrepanciaBtn"<-- close modal button runat="server" Text="Cerrar" CausesValidation="False" />
                    </td>
                </tr>
            </table>
        </div>

As you can see is pretty easy, just remember that TargetControlID, popupControlID and CancelControlID they need to exist, in order to work, if you are showing the modal by an action, just add a dummy to be the targetControlId, i use a hiddenfield

<asp:HiddenField ID="NuevaDiscrepanciaBtn" runat="server" />

then on your code behind is pretty simple as this.

if(conditions that makes your item has more items)
 YourPopUpExtenderId.show()

then you can handle the save button as a regular button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM