我试图在表单提交期间禁用该按钮,但无法这样做。这是javascript代码
<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" />
该按钮被禁用,但该值未提交到数据库中。
数据库更新代码是
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");
}