使用CustomValidator自定义验证控件检查是否有对L
网络编程 2021-07-04 22:40www.168986.cn编程入门
在网页前端处,我们放置ListBox控件,在数据提交前,检查用户是否有对此控件进行选择?长沙网络推广将介绍使用Javascript与CustomValidator自定义验证控件来检查感兴趣的朋友可以了解下
在前网页前端处,我们放置ListBox控件,在数据提交前,检查用户是否有对此控件进行选择? Insus.NET的方法是使用Javascript与CustomValidator自定义验证控件来检查。
可以看到最终结果
你也想参此例子,可以参考狼蚁网站SEO优化数据与准备方法,写一个对象,它将用来产生十个天干。
HeavenlyStem.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for HeavenlyStem
/// </summary>
public class HeavenlyStem
{
private int _ID;
private string _Name;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public HeavenlyStem()
{
//
// TODO: Add constructor logic here
//
}
public HeavenlyStem(int id, string name)
{
this.ID = id;
this._Name = name;
}
}
产生与用泛型来存储它们
private List<HeavenlyStem> GetHeavenStem()
{
List<HeavenlyStem> ListHS = new List<HeavenlyStem>();
HeavenlyStem hs = new HeavenlyStem();
hs.ID = 1;
hs.Name = "甲";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 2;
hs.Name = "乙";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 3;
hs.Name = "丙";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 4;
hs.Name = "丁";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 5;
hs.Name = "戊";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 6;
hs.Name = "己";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 7;
hs.Name = "庚";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 8;
hs.Name = "辛";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 9;
hs.Name = "壬";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 10;
hs.Name = "癸";
ListHS.Add(hs);
return ListHS;
}
Html标记
<asp:ListBox ID="ListBoxHeavenlyStem" runat="server" Width="60" Height="160" SelectionMode="Multiple"></asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" Display="None" ErrorMessage="必须选择选项" ClientValidationFunction="ValidateListBox"></asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true"
ShowMessageBox="true" ShowSummary="false" />
<asp:Button ID="Button1" runat="server" Text="Submit" />
接下来,你需要在.aspx.cs为ListBox控件绑定数据,数据源即是刚才产生的List<HeavenlyStem>
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Data_Binding();
}
private void Data_Binding()
{
this.ListBoxHeavenlyStem.DataSource = GetHeavenStem();
this.ListBoxHeavenlyStem.DataTextField = "Name";
this.ListBoxHeavenlyStem.DataValueField = "ID";
this.ListBoxHeavenlyStem.DataBind();
}
}
是写Javascript脚本
function ValidateListBox(sender, args) {
var lb = document.getElementById("<%=ListBoxHeavenlyStem.ClientID%>")
var options = lb.options;
args.IsValid = false;
for (var i = 0; i < options.length; i++) {
if (options[i].selected == true) {
args.IsValid = true;
return;
}
}
}
可以看到最终结果
你也想参此例子,可以参考狼蚁网站SEO优化数据与准备方法,写一个对象,它将用来产生十个天干。
HeavenlyStem.cs
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for HeavenlyStem
/// </summary>
public class HeavenlyStem
{
private int _ID;
private string _Name;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public HeavenlyStem()
{
//
// TODO: Add constructor logic here
//
}
public HeavenlyStem(int id, string name)
{
this.ID = id;
this._Name = name;
}
}
产生与用泛型来存储它们
代码如下:
private List<HeavenlyStem> GetHeavenStem()
{
List<HeavenlyStem> ListHS = new List<HeavenlyStem>();
HeavenlyStem hs = new HeavenlyStem();
hs.ID = 1;
hs.Name = "甲";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 2;
hs.Name = "乙";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 3;
hs.Name = "丙";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 4;
hs.Name = "丁";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 5;
hs.Name = "戊";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 6;
hs.Name = "己";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 7;
hs.Name = "庚";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 8;
hs.Name = "辛";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 9;
hs.Name = "壬";
ListHS.Add(hs);
hs = new HeavenlyStem();
hs.ID = 10;
hs.Name = "癸";
ListHS.Add(hs);
return ListHS;
}
Html标记
代码如下:
<asp:ListBox ID="ListBoxHeavenlyStem" runat="server" Width="60" Height="160" SelectionMode="Multiple"></asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" Display="None" ErrorMessage="必须选择选项" ClientValidationFunction="ValidateListBox"></asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true"
ShowMessageBox="true" ShowSummary="false" />
<asp:Button ID="Button1" runat="server" Text="Submit" />
接下来,你需要在.aspx.cs为ListBox控件绑定数据,数据源即是刚才产生的List<HeavenlyStem>
代码如下:
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Data_Binding();
}
private void Data_Binding()
{
this.ListBoxHeavenlyStem.DataSource = GetHeavenStem();
this.ListBoxHeavenlyStem.DataTextField = "Name";
this.ListBoxHeavenlyStem.DataValueField = "ID";
this.ListBoxHeavenlyStem.DataBind();
}
}
是写Javascript脚本
代码如下:
function ValidateListBox(sender, args) {
var lb = document.getElementById("<%=ListBoxHeavenlyStem.ClientID%>")
var options = lb.options;
args.IsValid = false;
for (var i = 0; i < options.length; i++) {
if (options[i].selected == true) {
args.IsValid = true;
return;
}
}
}
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程