使用没有ajax的javascript调用C#代码隐藏方法 [英] Calling C# code behind method using javascript without ajax

查看:58
本文介绍了使用没有ajax的javascript调用C#代码隐藏方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 2 buttons in my asp.net File

<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为真=>工作

  3. JS部分触发了UploadButton的Onclick =>不工作

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

I don't understand why this method doesn't work as it seems to be the general approach most other answers take on StackOverflow?

更新:

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

Ok, I've tried every solutions proposed below and now i have weird problems:

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

When I click on the client button, 1 of the 3 following things happens randomly (route followed with the debugger)

1:按钮单击执行空白回发(IsPostBack == true)
事件OnClick =BTN_Send_LA_Click未激活

1: The button click do a blank postback (IsPostBack == true) event OnClick="BTN_Send_LA_Click" not fired

2:按钮单击执行空白回发(IsPostBack == false)
事件OnClick =BTN_Send_LA_Click未被解雇

2: The button click do a blank postback (IsPostBack == false) event OnClick="BTN_Send_LA_Click" not fired

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

3: The button fire the event OnClick="BTN_Send_LA_Click" of the dummy button properly.

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

I don't understand why. When i click directly on the dummy button, everything works fine

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

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

something else: in my event BTN_Send_LA_Click(), I change the background color of multiple controls (lightgreen)

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

1: If I click on the dummy button => the background color of the controls are changed

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

2: If I click on the client button and even if the BTN_Send_LA_Click() is fired, the background color doesn't change.

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

Why ? I'm totally lost on this one

更新代码:

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


推荐答案

你已经拥有了一切正确的除外:

You've got it all right except:


  1. 您需要在} > if statement。

  2. ConfirmSendData()需要返回false 以阻止第一个按钮提交。

  1. You need a closing } on your if statement.
  2. ConfirmSendData() needs to return false to prevent the first button from submitting.

ie

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

这篇关于使用没有ajax的javascript调用C#代码隐藏方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆