Used to bind to WPF, ASP. NET binding does not seem to be very easy to use. Here's a look at the use of ASP.net bindings. In general, you can bind a DataTable directly, but I think the binding list is more consistent with object oriented programming.
The binding method is a two-sentence code:
Copy Code code as follows:
GridView name. DataSource = list< Custom Class >;
GridView name. DataBind ();
Just look at the example, here is an example of binding a Personmodel class. The use of the page return parameters, using JS delivery, JS can be written in the front-end can also be directly written in the background code.
Project structure:
Effect:
Entity classes
Copy Code code as follows:
public class Personmodel
{
private int personindex;
public int Personindex
{
get {return personindex;}
set {Personindex = value;}
}
private string PersonID;
public string PersonID
{
get {return PersonID;}
set {PersonID = value;}
}
private string PersonName;
public string PersonName
{
get {return personname;}
set {personname = value;}
}
private string Personsex;
public string Personsex
{
get {return personsex;}
set {personsex = value;}
}
private int personage;
public int personage
{
get {return personage;}
set {personage = value;}
}
private bool personselected = false;
public bool Personselected
{
get {return personselected;}
set {personselected = value;}
}
}
Write a management class for the bound ASPX page to manipulate the data
Copy Code code as follows:
public class Childfrmmanager
{
Private list<personmodel> Personcollect = new list<personmodel> ();
private static Childfrmmanager instance = NULL;
Public list<personmodel> Personcollect
{
get {return personcollect;}
set {Personcollect = value;}
}
public static Childfrmmanager Dogetinstance ()
{
if (instance = null)
{
Instance = new Childfrmmanager ();
}
return instance;
}
public void Doaddpersons ()
{
for (int i = 0; i < i++)
{
Personmodel model = new Personmodel ();
Model. Personindex = i + 1;
Model. PersonID = System.Guid.NewGuid (). ToString ();
Model. PersonName = "Test" + i;
Model. Personage = + i;
Model. Personsex = i% 2 = 0? "Male": "Female";
Model. personselected = false;
This. Personcollect.add (model);
}
}
}
Bound page Front End
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= true "codebehind=" ChildFrm.aspx.cs "inherits=" ASPNetGridView.Pages.ChildFrm "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<<body id= "Mybody" runat= "Server" ms_positioning= "GridLayout" >
<form id= "Form1" runat= "Server" method= "POST" >
<div>
<asp:gridview id= "dgpersons" runat= "Server" autogeneratecolumns= "False"
Enableviewstate= "false"
Cellpadding= "4" forecolor= "#333333" datakeynames= "PersonID"
onselectedindexchanged= "Selcted_click" >
<Columns>
<asp:commandfield showselectbutton= "True"/>
<asp:boundfield datafield= "Personindex" headertext= "serial number"/>
<asp:TemplateField>
<ItemTemplate>
<input id= "RadioButton1" name= "Pselect" type= "Radio"/>
</ItemTemplate>
</asp:TemplateField>
<asp:boundfield datafield= "PersonName" headertext= "name"/>
<asp:boundfield datafield= "personage" headertext= "Age"/>
<asp:boundfield datafield= "Personsex" headertext= "gender"/>
</Columns>
<footerstyle backcolor= "#5D7B9D" font-bold= "True" forecolor= "white"/>
<pagerstyle backcolor= "#284775" forecolor= "white" horizontalalign= "Center"/>
<selectedrowstyle backcolor= "#E2DED6" font-bold= "True" forecolor= "#333333"/>
<editrowstyle backcolor= "#999999"/>
<alternatingrowstyle backcolor= "White" forecolor= "#284775"/>
</asp:GridView>
</div>
</form>
</body>
Binding page Background
Copy Code code as follows:
public partial class ChildFrm:System.Web.UI.Page
{
Private Childfrmmanager Dmanager = null;
protected Personmodel SelectItem = null;
protected void Page_Load (object sender, EventArgs e)
{
Dmanager = Childfrmmanager.dogetinstance ();
if (! IsPostBack)
{
Dmanager.doaddpersons ();
This.dgPersons.DataSource = Dmanager.personcollect;
This.dgPersons.DataBind ();
}
}
protected void Selcted_click (object sender, EventArgs e)
{
int selectindex = This.dgPersons.SelectedIndex;
foreach (Personmodel mitem in Dmanager.personcollect)
{
if (Mitem. PersonIndex-1 = = Selectindex)
{
Mitem. Personselected = true;
}
Else
{
Mitem. personselected = false;
}
}
SelectItem = Dmanager.personcollect[selectindex];
String vbCrLf = "";
String strscript = "<script>" + vbCrLf;
Strscript + + "window.parent.returnvalue= '" + selectitem.personname + "';" + vbCrLf;
Strscript + = "window.parent.close ();" + vbCrLf;
Strscript + + "</script>" + vbCrLf;
if (! IsClientScriptBlockRegistered ("ClientScript"))
{
RegisterClientScriptBlock ("ClientScript", strscript);
}
}
}
Pages that host bound pages
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= true "codebehind=" FrameFrm.aspx.cs "inherits=" ASPNetGridView.Pages.FrameFrm "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<frameset rows= "0,*" >
<frame src= "About:blank" >
<frame src= "Childfrm.aspx" >
</frameset>
Main Page, get the return value of JS in front
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Aspnetgridview._default"% >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "
<title></title>
<body>
<div align= "center" >
<form id= "Form1" runat= "Server" method= "POST" >
<table runat= "Server" >
<tr>
<td>
<asp:label id= "Label1" runat= "Server" font-bold= "true" > select results </asp:Label>
</td>
<td>
<asp:textbox id= "Txtshowreturnvalue" runat= "Server" width= "100px"/>
</td>
<td>
<asp:button id= "btnopennewfrm" runat= "Server" text= "select" Width= "60px" onclientclick= "Opennewwindow ()"/>
</td>
</tr>
</table>
</form>
</div>
</body>
<script type = ' Text/javascript ' >
function Opennewwindow () {
var str = window.showmodaldialog (' pages/framefrm.aspx ', document. Form1.txtShowReturnValue.value, ' dialogwidth=1000px;dialogheight=900px ', ' scroll:yes ');
if (str!= null)
{document. Form1.txtShowReturnValue.value = str; }
}
</script>
Code download