<asp:TextBox ID="txtAtlasName" runat="server" width="400px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtAtlasName" ErrorMessage="不能为空">不能为空</asp:RequiredFieldValidator>
<asp:Button ID="btn_createAtlas" runat="server" Text="创建图集"
οnclick="btn_createAtlas_Click" CausesValidation="True" />
如上所示,RequiredFieldValidator用来验证textbox是否为空
protected void Page_Load(object sender, EventArgs e)
{
this.btn_createAtlas.Attributes.Add("onclick", "javascript:return window.confirm('确定创建吗?');");
}
page_load里添加了button事件,但是在点击按钮后,并不会触发页面的RequiredFieldValidator的验证。那么修改如下:
1 将button的CausesValidation属性设置为false
<asp:Button ID="btn_createAtlas" runat="server" Text="创建图集"
οnclick="btn_createAtlas_Click" CausesValidation="false" />
2 button确认属性中加入Page_ClientValidate()判断
protected void Page_Load(object sender, EventArgs e)
{
this.btn_createAtlas.Attributes.Add("onclick", "javascript:if(Page_ClientValidate()) return window.confirm('确定创建吗?');");
}