从确认框jQuery向behid code.vb发送值

时间:2018-03-07 07:21:12

标签: javascript jquery asp.net .net vb.net

我想用jquery在我的网站上制作确认框。当我选择确定/取消时,它会向我的vb发送值。但我总是对结果感到麻烦。无论我选择什么(确定/取消)它都会给我结果'你点击了NO!'。 当我点击按钮时,我的第二个麻烦就会发生。我希望它在onClick之前触发onClientClick。 请帮忙解决这两个问题。

先谢谢

Jquery的

<script type="text/javascript">
    jQuery.noConflict();
    function cekData() {
        var periode;
        periode = document.getElementById('ContentPlaceHolder1_txtTglAwal').value;

        jQuery.ajax({
            type: "post",
            url: "AjaxToolkit.asmx/CekPeriodeData",
            data: '{ tgl: "' + periode + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                onGenerateCA(r.d,periode);
            },
            error: function (xhr, status, error) {
                alert('Error xhr:' + xhr + ' status:' + status + ' error:' + error);
            }
        });
    }



    // Code that uses other library's $ can follow here.
</script>
<script type="text/javascript">
    function onGenerateCA(cek, periode) {
        if (cek == true) { ConfirmKetemuData(periode); }
        else { Confirm(); }
    }

    function Confirm() {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Apakah anda yakin untuk generate data ke CA?")) {
            confirm_value.value = "Ok";
        } else {
            confirm_value.value = "Cancel";
        }
        document.forms[0].appendChild(confirm_value);
    }

    function ConfirmKetemuData(periode) {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";

        if (confirm("Data periode " + periode + " sudah ada. Apakah anda yakin untuk generate data ke CA?")) {
            confirm_value.value = "Ok";
        } else {
            confirm_value.value = "Cancel";
        }
        document.forms[0].appendChild(confirm_value);
    }
</script>

按钮

<td class="btn" width="1100">
      <asp:Button ID="btnGenerateCA" runat="server" OnClick="onConfirm" Text="Generate CA"  OnClientClick = "cekData()"/>
</td>

.VB

Protected Sub OnConfirm(ByVal sender As Object, ByVal e As EventArgs)
    Dim confirmValue As String = Request.Form("confirm_value")
    If confirmValue = "ok" Then
        ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('You clicked YES!')", True)
    Else
        ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('You clicked NO!')", True)
    End If

End Sub

修改

我将按钮更改为

 <asp:Button ID="btnGenerateCA" runat="server"  OnClientClick = "if (!cekData()) { return false;};" OnClick="OnConfirm" Text="Generate CA" />

此代码首先使我的程序点击了onclientclick,但是在出现确认框后我点击了ok / cancel,它没有带我到.vb,所以我无法访问.vb。请帮我解决这个问题。感谢

0 个答案:

没有答案