在没有ajax的情况下使用javascript调用c#代码

Calling C# code behind method using javascript without ajax

本文关键字:javascript 调用 代码 情况下 ajax      更新时间:2023-09-26

我有两个按钮在我的asp.net文件

<asp:Button ID="BTN_Send_LA" runat="server" Text="Save" OnClientClick="ConfirmSendData()"></asp:Button>
//The button the client will click
<asp:Button ID="UploadButton" runat="server" Text="" OnClick="BTN_Send_LA_Click"/>
//Dummy Button for the JS .click()

这是我的Js部分:

function ConfirmSendData() {
    var r = confirm("Êtes vous bien: " + document.getElementById("<%=DDL_LaveurLA.ClientID%>").options[document.getElementById("<%=DDL_LaveurLA.ClientID%>").selectedIndex].text + " sinon veuillez changer dans le champ spécifié 'Laveur'");
    if (r == true) {
        var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
        clickButton.click();
        //$('UploadButton').trigger('click'); TEST 1
        //__doPostBack not working aswell
    }
}

所以这是我希望做的:

  1. 客户端点击第一个按钮(触发JS) => Works
  2. R是true => Works
  3. JS部分触发Onclick的UploadButton =>不工作

我不明白为什么这个方法不起作用,因为它似乎是大多数其他答案采取StackOverflow的一般方法?

更新:

好的,我已经尝试了下面提出的所有解决方案,现在我有奇怪的问题:

当我点击客户端按钮时,以下3件事中的1随机发生(路由跟随调试器)

1:按钮点击做一个空白回发(IsPostBack == true)event OnClick="BTN_Send_LA_Click"未触发

2:按钮点击做一个空白回发(IsPostBack == false)event OnClick="BTN_Send_LA_Click"未触发

3:按钮正确触发虚拟按钮的OnClick="BTN_Send_LA_Click"事件。

我不明白为什么。当我直接点击虚拟按钮,一切工作正常

每次我按CTRL+F5,我第一次点击客户端按钮将100%工作(事件触发)

其他:在我的事件BTN_Send_LA_Click()中,我改变了多个控件的背景颜色(淡绿色)

1:如果我点击虚拟按钮=>控件的背景颜色被改变

2:如果我点击客户端按钮,即使触发了BTN_Send_LA_Click(),背景颜色也不会改变。

为什么?我完全迷失在这个

更新代码:

        function ConfirmSendData()
     {
            /*
            var dd = document.getElementById("<%=DDL_LaveurLA.ClientID%>");
            var txt = dd.options[dd.selectedIndex].text;
            var r = confirm("Êtes vous bien: " + txt + " sinon veuillez changer dans le champ spécifié 'Laveur'"); */
            var r = confirm("Êtes vous bien: " + document.getElementById("<%=DDL_LaveurLA.ClientID%>").options[document.getElementById("<%=DDL_LaveurLA.ClientID%>").selectedIndex].text + " sinon veuillez changer dans le champ spécifié 'Laveur'");
            if (r == true) {
                //$("#<%=UploadButton.ClientID%>").click();
                var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
                clickButton.click();
            }
            return false;  
    }

除了:

  1. 您的if语句需要一个关闭的}
  2. ConfirmSendData()需要return false来阻止第一个按钮提交。

function ConfirmSendData() {
    var r = confirm("Êtes vous bien...");
    if (r == true) {
        var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
        clickButton.click();
    }
    return false;
}

您已经注释掉了以下内容,我认为这意味着您已经尝试过了,但是它不起作用…

//$('UploadButton').trigger('click'); TEST 1

jQuery需要元素ID前的#字符来查找它,所以试试这个…

$("#<%=UploadButton.ClientID%>").click();

我也会稍微更新你的函数的第一部分,以一个更可读(和高效)的版本,只找到下拉元素一次…

var dd = document.getElementById("<%=DDL_LaveurLA.ClientID%>");
var txt = dd.options[dd.selectedIndex].text;
var r = confirm("Êtes vous bien: " + txt + " sinon veuillez changer dans le champ spécifié 'Laveur'");

您应该使用$("#<%=UploadButton.ClientID%>")而不是$('UploadButton'),因为asp:Button元素生成的不是仅id为UploadButton的元素

function ConfirmSendData() {
    var r = confirm("Êtes vous bien: " + document.getElementById("<%=DDL_LaveurLA.ClientID%>").options[document.getElementById("<%=DDL_LaveurLA.ClientID%>").selectedIndex].text + " sinon veuillez changer dans le champ spécifié 'Laveur'");
    if (r == true) {
        var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
        clickButton.click();
        // alternative variant for jquery
        // $("#<%=UploadButton.ClientID%>").click();
    }
}

功能ConfirmSendData需要return false阻止提交数据的第一个按钮

试试这个:

__doPostBack('<%= UploadButton.UniqueID %>', '');