按钮单击方法

时间:2013-06-07 18:39:05

标签: javascript asp.net

我正在尝试让我的webform检查空文本框,然后验证它,提交到数据库然后打印它..但我似乎无法弄清楚如何连接方法使它全部工作一旦我点击提交按钮,一个接一个。我有代码检查它,然后报告/验证它,但我需要在打印功能之前连接另一个方法(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 = "";
    }

}

1 个答案:

答案 0 :(得分:0)

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

作为附注:
- 您应该将con.Dispose();方法添加到您的网络表单中,其中包含Button1; - 插入查询应该参数化。

<强>更新

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

相关问题