当我插入数据时,我想要的这个我的aspx不是要记录在数据库中......
<div class="form-group">
<label for="description" class="control-label">Hazard Description : </label>
<div class="col-xs-8">
<asp:TextBox ID="TextBox3" runat="server" CssClass="form-control" TextMode="MultiLine" Columns="55" Rows="7"></asp:TextBox>
<asp:CustomValidator ID="CustomValidatorHazardDescription" ValidateEmptyText="true" runat="server" Display="Dynamic" ErrorMessage="Tidak Boleh Kosong" OnServerValidate="CustomValidatorHazardDescription_ServerValidate"></asp:CustomValidator>
</div>
</div>
当我Clik插入消息成功但数据仍然要记录在数据库中? 当我插入数据时,我想要的这个我的代码不是要记录在数据库中......
protected void CustomValidatorHazardDescription_ServerValidate(object source, ServerValidateEventArgs args)
{
TextBox txb = (TextBox)FormView1.FindControl("TextBox3");
if(string.IsNullOrEmpty(txb.Text))
{
args.IsValid = false;
}
else
{
args.IsValid = true;
return;
}
}
此流程插入
tbl_hzr_main newitem = new tbl_hzr_main();
newitem.code_company = int.Parse(((CustomControls_DdlCompany)_f.Controls[0].FindControl("ddl_company1")).SelectedValue);
newitem.code_empPIC = int.Parse(((HiddenField)_f.Controls[0].FindControl("hid_pic_id")).Value);
newitem.code_hzrUser = id_spv;
newitem.code_incdLocation = int.Parse(((CustomControls_DdlLocation)_f.Controls[0].FindControl("ddl_location1")).SelectedValue);
newitem.code_main_contractor = int.Parse(((CustomControls_DdlCompany)_f.Controls[0].FindControl("ddl_company1")).SelectedMainConValue);
newitem.code_section = code_section;
newitem.code_usrEntry = int.Parse(uc.usrID);
newitem.date_hzrMain = DateTime.Parse(((TextBox)_f.Controls[0].FindControl("TextBox1")).Text);
newitem.desc_hzrMain = ((TextBox)_f.Controls[0].FindControl("TextBox3")).Text; // this insert to database
newitem.dueDate_hzrMain = DateTime.Parse(((TextBox)_f.Controls[0].FindControl("TextBox2")).Text);
newitem.entryDate_hzrMain = DateTime.Now;
newitem.folup_hzrMain = ((TextBox)_f.Controls[0].FindControl("TextBox4")).Text;
newitem.locDetail_hzrMain = ((TextBox)_f.Controls[0].FindControl("txb_loc_detail")).Text;
newitem.PICsign_status = byte.Parse(((RadioButtonList)_f.Controls[0].FindControl("rbl_sign_pic")).SelectedValue);
newitem.stat_hzrMain = byte.Parse(((RadioButtonList)_f.Controls[0].FindControl("RadioButtonList1")).SelectedValue);
dbcontext.tbl_hzr_main.Add(newitem);
dbcontext.SaveChanges();
答案 0 :(得分:1)
在OnSubmit函数或Button_Click事件(其中表单提交)中添加
Page.Validate();
if (Page.IsValid == true){
//Add to Database
}else {
//Dont Add to Database
}
答案 1 :(得分:1)
来自你更新的问题,在这一行之下:
newitem.desc_hzrMain = ((TextBox)_f.Controls[0].FindControl("TextBox3")).Text; // this insert to database
尝试添加此代码:
if(string.isNullOrEmpty(newitem.desc_hzrMain)){
//show alert or something that data fail to insert
return;
}