2 Replies - 1284 Views - Last Post: 21 June 2013 - 07:25 PM Rate Topic: -----

#1 Supraman9291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-June 13

Server and client click not working together

Posted 12 June 2013 - 11:17 AM

My Button1_click is server sided code that when the button is pushed, data is submitted into a database. My submitForm() is clientside code. How can I get both to work when I push the button called button1? (my submit button). The code below only fires the onclick when the button is pushed and the OnClientClick doesn't.

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


This is my submit form code

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


and this is my button1_click code

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

    }


Is This A Good Question/Topic? 0
  • +

Replies To: Server and client click not working together

#2 jace75  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 11-June 13

Re: Server and client click not working together

Posted 21 June 2013 - 03:38 PM

try removing the onclick properties from your aspx page, and add this to your page load..

 button1.Attributes.Add("onclick", "javascript:SubmitForm()");
 button1.Attributes.Add("onclick", "button1_Click");



If nothing else prevents your script from firing, this will ensure that it will fire before your standard click event.

Happy coding B)
Was This Post Helpful? 0
  • +
  • -

#3 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Server and client click not working together

Posted 21 June 2013 - 07:25 PM

You can also create your own function, and use the jqueryplugin. Very easy to use.

First establish an id for the control:
button1.id = "test1"



Code using jquery would be as follows:

<script type='text/javascript'>
$('#test1').click(function () {
form1.submit(); //there are a number of ways to access the form but typically it's named form1 unless you changed it
return false;
});
</script>



You would load this script on page load, or you can go into the html editor of visual studio for your asp.net application and write it into the <script></script> tags.

This post has been edited by trevster344: 21 June 2013 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1