At the front end of the previous page, we place the ListBox control. Before submitting data, check whether the user has selected this control? The Insus. NET method is to use Javascript and CustomValidator custom verification controls to check.
You can see the final result:
You can refer to the following data and preparation methods to write an object that will be used to generate ten days of work.
HeavenlyStem. cs
Copy codeThe Code is as follows: 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;
}
}
Generate and use generics to store them:Copy codeThe Code is as follows: 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 = "B ";
ListHS. Add (hs );
Hs = new HeavenlyStem ();
Hs. ID = 3;
Hs. Name = "C ";
ListHS. Add (hs );
Hs = new HeavenlyStem ();
Hs. ID = 4;
Hs. Name = "ding ";
ListHS. Add (hs );
Hs = new HeavenlyStem ();
Hs. ID = 5;
Hs. Name = "E ";
ListHS. Add (hs );
Hs = new HeavenlyStem ();
Hs. ID = 6;
Hs. Name = "Ji ";
ListHS. Add (hs );
Hs = new HeavenlyStem ();
Hs. ID = 7;
Hs. Name = "Geng ";
ListHS. Add (hs );
Hs = new HeavenlyStem ();
Hs. ID = 8;
Hs. Name = "Xin ";
ListHS. Add (hs );
Hs = new HeavenlyStem ();
Hs. ID = 9;
Hs. Name = "success ";
ListHS. Add (hs );
Hs = new HeavenlyStem ();
Hs. ID = 10;
Hs. Name = "";
ListHS. Add (hs );
Return ListHS;
}
Html Tag:Copy codeThe Code is as follows: <asp: ListBox ID = "ListBoxHeavenlyStem" runat = "server" Width = "60" Height = "160" SelectionMode = "Multiple"> </asp: ListBox>
<Asp: CustomValidator ID = "CustomValidator1" runat = "server" Display = "None" ErrorMessage = "required" ClientValidationFunction = "ValidateListBox"> </asp: CustomValidator>
<Asp: ValidationSummary ID = "ValidationSummary1" runat = "server" EnableClientScript = "true"
ShowMessageBox = "true" ShowSummary = "false"/>
<Asp: Button ID = "Button1" runat = "server" Text = "Submit"/>
Next, you need to bind data to The ListBox control in. aspx. cs. The data source is the generated List <HeavenlyStem>:Copy codeThe Code is as follows: 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 ();
}
}
Finally, write Javascript scripts.:Copy codeThe Code is as follows: 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;
}
}
}