0

I've a Validate function and it returns true and false depending upon the inserted values. I want to validate as well as disable the submit button. for ex OnClientClick = "return Validate(this);" if it returns true, submit into database and disable submit for that period of time and if it is false disable submit and alert with the validate message.

<script type="text/javascript">
    function Validate(b) {
        function stuff() {
            var temp = document.getElementById("<%=txt_stuff.ClientID %>").value;
            var val = /^[a-zA-Z0-9 ]+$/
            if (temp == "") {
                alert("Please Enter Stuff");
                return false;
            } else if (val.test(temp)) {
                return true;
            } else {
                alert("Name accepts only spaces and charcters");
                return false;
            }
        }

        function price() {
            var temp2 = document.getElementById("<%=txt_price.ClientID %>").value;
            var val2 = /^[0-9 ]+$/
            if (temp2 == "") {
                alert("Please Enter Price");
                return false;
            } else if (val2.test(temp2)) {
                return true;
            } else {
                alert("Price accepts only Number");
                return false;
            }
        }
        if (stuff() && price()) {
            b.disabled = true;
            b.value = 'Submitting...';
            return true;
        } else {
            return false;
        }
    }
</script>

Here is the button code

 <asp:Button ID="Button2" runat="server" Text="Add Record" OnClientClick = "return Validate(this)" 
         onclick="Button2_Click" />

The button gets disabled but the value isn't submitted into the database and the button also doesn't get enabled again. What could be the reason?

Database code is

protected void Button2_Click(object sender, EventArgs e)
        {

            System.Threading.Thread.Sleep(500);
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=DTPXP-77A;Initial Catalog=practice;Integrated Security=true";
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into expense values(@person,@item,@expdate,@price)", con);
            cmd.Parameters.Add("@person", SqlDbType.VarChar);
            cmd.Parameters.Add("@item", SqlDbType.VarChar);
            cmd.Parameters.Add("@expdate", SqlDbType.DateTime);
            cmd.Parameters.Add("@price", SqlDbType.Int);
            cmd.Parameters["@person"].Value = droplist_person.SelectedItem.ToString();
            cmd.Parameters["@item"].Value = txt_stuff.Text;
            cmd.Parameters["@expdate"].Value = DateTime.Now.ToString();
            cmd.Parameters["@price"].Value = txt_price.Text;
            cmd.ExecuteNonQuery();
            InsertHistory();
            Response.Redirect("Add.aspx");

        }
Shad Khan
  • 119
  • 4
  • 12
  • Without actual code part nobody can tell you what is going on. Post the part where you make database query, for example. – Rolice Oct 08 '13 at 14:10
  • Reasons may be many. First of all are you sure the connection is made with the SQL Server (is it online)? Second, check if parameters have valid values for the Database. I would suggest to turn on the debugger and start it. You will face an exception if error had occurred. F10 is step over F11 is step-into. However, **I suspect `SqlDbType.DateTime` may differ from `Datetime.ToString`** - will expect different input. – Rolice Oct 08 '13 at 14:22
  • No. Everything is fine. Actually when I write OnClientClick= "return Validate();this.disabled=true" then values are submitted but button is not disabled but if I just reverse it OnClientClick= "this.disabled=true;return Validate()" then button is disabled but value isn't submitted. – Shad Khan Oct 08 '13 at 14:25
  • This is strange, as far as I remember disabled controls are not posted which may conflict. Check this: http://stackoverflow.com/questions/106509/disable-button-on-form-submission – Rolice Oct 08 '13 at 14:37

0 Answers0