【问题标题】:Server and client click not working together服务器和客户端点击不一起工作
【发布时间】:2013-06-12 17:16:20
【问题描述】:

我的 Button1_click 是服务器端代码,当按下按钮时,数据会提交到数据库中。我的 SubmitForm() 是客户端代码。当我按下名为 button1 的按钮时,如何让两者都工作? (我的提交按钮)。下面的代码仅在按下按钮时触发 Onclick 而 OnClientClick 不触发。

<asp:Button ID="Button1" runat="server"  Onclick = "Button1_Click" 
            OnClientClick = "javascript:SubmitForm();return false" 
            Text="Submit" Width="98px"
              />

这是我的提交表单代码

function SubmitForm() {


    if (document.getElementById("hawbtxt").value == "") {
        alert("Please enter the HAWB (B/L)!");
        return false;
    }
    if (document.getElementById("invrefpotxt").value == "") {
        alert("Please enter the INV/REF/PO!");
        return false;
    }
    if (document.getElementById("hppartnumtxt").value == "") {
        alert("Please enter the HP PART NUM!");
        return false;
    }
    if (document.getElementById("iecpartnumtxt").value == "") {
        alert("Please enter the IEC PART NUM!");
        return false;
    }
    if (document.getElementById("qtytxt").value == "") {
        alert("Please enter the QUANTITY!");
        return false;
    }
    if (document.getElementById("bulkstxt").value == "") {
        alert("Please enter the BULKS!");
        return false;
    }
    if (document.getElementById("boxplttxt").value == "") {
        alert("Please enter the BOX/PLT!");
        return false;
    }
    if (document.getElementById("rcvddatetxt").value == "") {
        alert("Please enter the DATE!");
        return false;
    }
    if (document.getElementById("statustxt").value == "") {
        alert("Please enter the STATUS!");
        return false;
    }
    if (document.getElementById("carriertxt").value == "") {
        alert("Please enter the CARRIER!");
        return false;
    }
    if (document.getElementById("shippertxt").value == "") {
        alert("Please enter the SHIPPER!");
        return false;

    }

//create coo_bto_test.bat
    var sText, s;
    var fso = new ActiveXObject("Scripting.FileSystemObject");

    alert("called");
    if (!fso.FileExists("C:\\COO_BTO_Test.bat")) {
        s = fso.CreateTextFile("C:\\COO_BTO_Test.bat", true);
        sText = "@echo off";
        s.WriteLine(sText);

        sText = ":Lbl";
        s.WriteLine(sText);

        sText = "ECHO \"^XA^MD0^PRB^JVY^LL1760^LH%XL%,%YL%^FS   \">> COO.TXT";
        s.WriteLine(sText);

        sText = "ECHO \"^FO66,08^BY2,2.0,32^BCN,N,N,N^SN%Sno0%,1,Y^FS   \">> COO.TXT";
        s.WriteLine(sText);

        sText = "ECHO \"^FO66,48^AF,8,8^SN%Sno0%,1,Y^FS     \">> COO.TXT";
        s.WriteLine(sText);

        sText = ":END";
        s.WriteLine(sText);

        sText = "ECHO \"^PQ1     \">> COO.TXT";
        s.WriteLine(sText);

        sText = "ECHO \"^XZ     \">> COO.TXT";
        s.WriteLine(sText);

        sText = "REM MODE COM1 9600"
        s.WriteLine(sText);

        sText = "REM TYPE COO.TXT > COM1"
        s.WriteLine(sText);

        sText = "TYPE COO.TXT > LPT1"
        s.WriteLine(sText);
    }
      }

     function WriteToFile(sText) {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var s = fso.CreateTextFile("C:\\PRT_Test.bat", true);
        s.WriteLine(sText);
        s.Close();
    }

这是我的 button1_click 代码

 protected void Button1_Click(object sender, EventArgs e)
    {

        SqlCommand cmd = new SqlCommand("insert into John_IEP_Crossing_Dock_Shipment values('" + generateidtxt.Text + "','" + hawbtxt.Text + "','" + invrefpotxt.Text + "','" + hppartnumtxt.Text + "','" + iecpartnumtxt.Text + "','" + qtytxt.Text + "','" + bulkstxt.Text + "','" + boxplttxt.Text + "','" + rcvddatetxt.Text + "','" + statustxt.Text + "','" + carriertxt.Text + "','" + shippertxt.Text + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
        generateidtxt.Text = "";
        hawbtxt.Text = "";
        invrefpotxt.Text = "";
        hppartnumtxt.Text = "";
        iecpartnumtxt.Text = "";
        qtytxt.Text = "";
        bulkstxt.Text = "";
        boxplttxt.Text = "";
        rcvddatetxt.Text = "";
        statustxt.Text = "";
        carriertxt.Text = "";
        shippertxt.Text = "";
        con.Dispose();

    }

【问题讨论】:

  • 页面渲染时你的按钮是否被禁用?如果是这样,OnClientClick 将不会被写入 HTML
  • 不,我相信在页面渲染时它没有被禁用。
  • @user2457203 你能用SubmitForm 代码更新问题吗?

标签: javascript asp.net


【解决方案1】:

请试试这个:

<asp:Button ID="Button1" runat="server"  Onclick = "Button1_Click" 
            OnClientClick = "javascript:SubmitForm();" 
            Text="Submit" Width="98px"
/>

编辑:

function SubmitForm() {


    if (document.getElementById("hawbtxt").value == "") {
        alert("Please enter the HAWB (B/L)!");
        return false;
    }
    if (document.getElementById("invrefpotxt").value == "") {
        alert("Please enter the INV/REF/PO!");
        return false;
    }
    if (document.getElementById("hppartnumtxt").value == "") {
        alert("Please enter the HP PART NUM!");
        return false;
    }
    if (document.getElementById("iecpartnumtxt").value == "") {
        alert("Please enter the IEC PART NUM!");
        return false;
    }
    if (document.getElementById("qtytxt").value == "") {
        alert("Please enter the QUANTITY!");
        return false;
    }
    if (document.getElementById("bulkstxt").value == "") {
        alert("Please enter the BULKS!");
        return false;
    }
    if (document.getElementById("boxplttxt").value == "") {
        alert("Please enter the BOX/PLT!");
        return false;
    }
    if (document.getElementById("rcvddatetxt").value == "") {
        alert("Please enter the DATE!");
        return false;
    }
    if (document.getElementById("statustxt").value == "") {
        alert("Please enter the STATUS!");
        return false;
    }
    if (document.getElementById("carriertxt").value == "") {
        alert("Please enter the CARRIER!");
        return false;
    }
    if (document.getElementById("shippertxt").value == "") {
        alert("Please enter the SHIPPER!");
        return false;

    }

//create coo_bto_test.bat
    var sText, s;
    var fso = new ActiveXObject("Scripting.FileSystemObject");

    alert("called");
    if (!fso.FileExists("C:\\COO_BTO_Test.bat")) {
        s = fso.CreateTextFile("C:\\COO_BTO_Test.bat", true);
        sText = "@echo off";
        s.WriteLine(sText);

        sText = ":Lbl";
        s.WriteLine(sText);

        sText = "ECHO \"^XA^MD0^PRB^JVY^LL1760^LH%XL%,%YL%^FS   \">> COO.TXT";
        s.WriteLine(sText);

        sText = "ECHO \"^FO66,08^BY2,2.0,32^BCN,N,N,N^SN%Sno0%,1,Y^FS   \">> COO.TXT";
        s.WriteLine(sText);

        sText = "ECHO \"^FO66,48^AF,8,8^SN%Sno0%,1,Y^FS     \">> COO.TXT";
        s.WriteLine(sText);

        sText = ":END";
        s.WriteLine(sText);

        sText = "ECHO \"^PQ1     \">> COO.TXT";
        s.WriteLine(sText);

        sText = "ECHO \"^XZ     \">> COO.TXT";
        s.WriteLine(sText);

        sText = "REM MODE COM1 9600"
        s.WriteLine(sText);

        sText = "REM TYPE COO.TXT > COM1"
        s.WriteLine(sText);

        sText = "TYPE COO.TXT > LPT1"
        s.WriteLine(sText);
    }
        return true;
      }

【讨论】:

  • 请分享SubmitForm函数..检查你是否在这个函数的任何地方返回false..
  • 好的,我发布了 SubmitForm 函数。我可以看到我在那里返回 false 。我尝试删除返回的 false 部分。但同样的事情也会发生。
  • 所以,我猜当你说这不起作用时..它实际上会进入这些return false; 请检查单元测试场景.. 或者请在最后使用return true; SubmitForm 函数。
  • 我改了全部返回false;返回;在所有警报中,我已经从 asp 按钮中删除了 return false。当我按下按钮时,我将第一个文本框留空,以便显示第一个警报。完成警报后,它仍然运行 onclick..
  • 应该是这样。不要到处改变它。要么使用这样的测试用例,使其不会进入 if 块,要么在函数末尾使用 return true;
【解决方案2】:

从您的 javascript 函数中删除 return false。你会得到它的工作。

仅供参考:

return false 的表单将不会被提交:

并且 从 JavaScript 事件返回 false 通常会取消“默认”行为 - 在链接的情况下,它会告诉浏览器不要跟随链接。

【讨论】:

    【解决方案3】:

    删除 返回false来自OnClientClick

    OnClientClick = "javascript:SubmitForm()" 
    

    由于您从客户端单击返回 false,因此服务器事件不会触发

    如果你从 sumitform 返回布尔值,你可以这样做

    OnClientClick="javascript: return SubmitForm();"
    

    如果您从SubmitForm 方法返回true,那么服务器事件也会触发。

    更新

    你只从你的SubmitForm方法返回false,最后如果所有需要的输入都在你可以返回true的表单中

    【讨论】:

    • 你能显示它应该是什么样子的正确代码,所以我可以复制和粘贴它。谢谢。
    猜你喜欢
    • 2016-08-06
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多