2

我的 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();

    }
4

3 回答 3

0

请试试这个:

<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;
      }
于 2013-06-12T17:22:17.600 回答
0

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

供参考:

with return falseform 将不会提交:

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

于 2013-06-12T17:22:58.360 回答
0

删除 return falseOnClientClick

OnClientClick = "javascript:SubmitForm()" 

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

如果您从 sumitform 返回布尔值,您可以执行以下操作

OnClientClick="javascript: return SubmitForm();"

如果您trueSubmitForm方法返回,则服务器事件也会触发。

更新

你只从你的SubmitForm方法中返回 false ,最后如果所有必需的输入都以你可以返回 true 的形式出现

于 2013-06-12T17:19:39.187 回答