使用没有ajax的javascript调用C#代码隐藏方法

时间:2015-05-19 13:54:17

标签: javascript c# jquery asp.net ajax

我的asp.net文件中有2个按钮

<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)=&gt;作品
  2. R为真=&gt;作品
  3. JS部分触发了UploadButton的Onclick =&gt;不要工作
  4. 我不明白为什么这种方法不起作用,因为它似乎是大多数其他答案在StackOverflow上采用的一般方法?

    更新

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

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

    1:按钮单击执行空白回发(IsPostBack == true)    事件OnClick =&#34; BTN_Send_LA_Click&#34;不解雇

    2:按钮单击执行空白回发(IsPostBack == false)    事件OnClick =&#34; BTN_Send_LA_Click&#34;不解雇

    3:按钮触发事件OnClick =&#34; BTN_Send_LA_Click&#34;正确的虚拟按钮。

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

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

    别的东西:在我的事件BTN_Send_LA_Click()中,我改变了多个控件的背景颜色(lightgreen)

    1:如果我点击虚拟按钮=&gt;控件的背景颜色已更改

    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;  
        }
    

4 个答案:

答案 0 :(得分:4)

你做得很好,除了:

  1. 您需要在}声明中结束if
  2. ConfirmSendData()需要return false才能阻止第一个按钮提交。
  3. function ConfirmSendData() {
        var r = confirm("Êtes vous bien...");
        if (r == true) {
            var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
            clickButton.click();
        }
        return false;
    }
    

答案 1 :(得分:2)

你有以下注释,我认为这意味着你已经尝试过它并没有用...

//$('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'");

答案 2 :(得分:1)

您应该使用$("#<%=UploadButton.ClientID%>")代替$('UploadButton'),因为asp:Button元素不会生成只有身份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才能阻止通过第一个按钮提交数据

答案 3 :(得分:1)

试试这个:

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