0

我试图让我的网络表单检查空文本框,然后验证它,提交到数据库然后打印它..但我似乎无法弄清楚如何连接这些方法以使其全部工作一个接一个一旦我点击提交按钮。我有检查它的代码,然后报告/验证它,但我需要在打印功能之前首先连接另一个方法(button1_click(),它是 default.aspx.cs 页面上的一个受保护的 void)。我将如何将它们连接在一起,一个接一个地工作?

function submitForm() {

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

    //report   

    report = ""
    report += "ID: " + document.getElementById("generateidtxt").value + "<br>" //generated id
    report += "HAWB (B/L): " + document.getElementById("hawbtxt").value + "<br>"
    report += "INV/REF/PO: " + document.getElementById("invrefpotxt").value + "<br>"
    report += "HP PART NUM: " + document.getElementById("hppartnumtxt").value + "<br>"
    report += "IEC PART NUM: " + document.getElementById("iecpartnumtxt").value + "<br>"
    report += "QTY: " + document.getElementById("qtytxt").value + "<br>"
    report += "BULKS: " + document.getElementById("bulkstxt").value + "<br>"
    report += "BOX/PLT: " + document.getElementById("boxplttxt").value + "<br>"
    report += "RCVD DATE: " + document.getElementById("rcvddatetxt").value + "<br>"
    report += "STATUS: " + document.getElementById("statustxt").value + "<br>"
    report += "CARRIER: " + document.getElementById("carriertxt").value + "<br>"
    report += "SHIPPER: " + document.getElementById("shippertxt").value + "<p>"

    document.open();
    document.clear();
    document.write(report);
    document.close();


    //print

    if (confirm('Print Label?')) {
     //code for button1_click?  
        window.print();
    } else {
        form1.reset();
    }

}

以下来自 default.aspx.cs 页面

public partial class _Default : System.Web.UI.Page 
{

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);




    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
    }

    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();
       // Label1.Visible = true;
       // Label1.Text = "Your DATA stored Successfully!";
        generateidtxt.Text = "";
        hawbtxt.Text = "";
        invrefpotxt.Text = "";
        hppartnumtxt.Text = "";
        iecpartnumtxt.Text = "";
        qtytxt.Text = "";
        bulkstxt.Text = "";
        boxplttxt.Text = "";
        rcvddatetxt.Text = "";
        statustxt.Text = "";
        carriertxt.Text = "";
        shippertxt.Text = "";
    }

}
4

1 回答 1

0

在当前设置中执行所需操作的唯一方法是使用 ajax 调用,该调用将模拟由于单击按钮而回发到页面。为什么需要在数据库表中插入记录之前而不是之后window.print()

作为旁注:
- 您应该在其中添加Page_Unload方法到您的网络表单con.Dispose();
- 插入查询应该被参数化。

更新:

如果Button1Button服务器控件,它应该将页面提交给服务器,如果Button1_Click连接为按钮的 OnClick 事件,它应该触发。submitForm要在表单提交时调用 javascript 函数,请将属性添加onsubmit="submitForm()"到页面标记中的表单标记。此外,在验证失败的情况下,使用return false;代替return;insidesubmitForm来防止表单提交。

于 2013-06-07T19:00:21.770 回答