如果文本框为空但数据仍要记录在数据库中,则进行自定义验证

拉马特

当我插入数据时,我想要的这个我的 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>

当我点击成功插入消息但数据仍然记录在数据库中时?当我插入数据时,我想要的后面的代码不是要记录在数据库中...

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();
汉南阿万 |

在您的 OnSubmit 函数或 Button_Click 事件(表单提交的地方)中添加

Page.Validate();
 if (Page.IsValid == true){
//Add to Database
}else {
//Dont Add to Database
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android记录在数据库中存在()?

如何使存储在数据库中的自定义字段文本成为Wordpress前端中的链接

如何在数据库实体的子模型上使用自定义验证属性?

如何在VB.NET的文本框中显示数据库中的空值

在数据库上的Picketlink自定义关系重复记录

导入前如何验证记录在数据库中不存在?

如果文本框为空,则数据库中的自动空值

如何在数据库中包含引号的文本框中设置值?

从文本框中更新数据库记录时出错

将具有可自定义字体的文本存储在数据库中

如果绑定数据为空,则想在md-option中显示自定义文本

用户可自定义的验证,其中元数据保存在数据库中

根据文本框和字符串在数据库中查询搜索

在数据库中记录数据之前等待验证

如何使用AJAX或PHP在数据库中存储添加更多文本框的数据

无法将空的文本框值保存到数据库。(格式为int)

从数据库中获取单个记录,并使用Codeigniter在文本框中显示

如何验证是否在文本框中输入相同的数据?(无需数据库)

如何使用自定义键将数据保存在数据库中?

如何使用 Opencart 在数据库中插入自定义数据

使用数据库中的记录在数据表中设置单选按钮

自定义列不在数据库中

如果选择 Combobox vb.net mysql,则文本框中的数据库值

从动态文本框输入数据时,为什么在数据库中输入空值

更新在 Laravel 中不起作用,如果我在数据库中使用与列名不同的文本框名称

一旦文本框的值存储在数据库中,禁用它 - PHP

1的值存储在数据库中而不是文本框值/ PHP-MySql

在数据库 VB.NET 中插入文本框的问题

如何仅在数据库中添加具有价值的文本框