ASP. NET-001: GridView binding List

Source: Internet
Author: User

This article mainly introduces the implementation of ASP. NET-GridView binding List and page return values. For more information, see

I used to bind WPF. ASP. NET does not seem to be very useful. Next let's take a look at ASP. NET binding usage. In general, DataTable can be bound directly, but I think the bound List is more in line with object-oriented programming.

The binding method is the following code:

 

The Code is as follows:

GridView name. DataSource = List <custom class>;

 

GridView name. DataBind ();

 

 

Let's look at the example. Here is an example of binding a PersonModel class. The page response parameters are used and transmitted using js. js can be written at the front end or directly in the background code.

Project Structure:

 

Effect:

 

Entity class

The Code is 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 for Data Operations

 

The Code is 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 <20; I ++)

{

PersonModel model = new PersonModel ();

 

 

Model. PersonIndex = I + 1;

 

 

Model. PersonID = System. Guid. NewGuid (). ToString ();

 

 

Model. PersonName = "test" + I;

 

 

Model. PersonAge = 27 + I;

 

 

Model. PersonSex = I % 2 = 0? "Male": "female ";

 

 

Model. PersonSelected = false;

 

 

This. PersonCollect. Add (model );

}

}

}

 

 

 

Bound page front-end

The Code is 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">

 

 

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head runat = "server">

<Title> </title>

</Head>

<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 = "no."/>

<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"/>

<HeaderStyle BackColor = "# 5D7B9D" Font-Bold = "True" ForeColor = "White"/>

<EditRowStyle BackColor = "#999999" type = "regxph" text = "yourobjectname"/>

<AlternatingRowStyle BackColor = "White" ForeColor = "#284775"/>

 

</Asp: GridView>

</Div>

</Form>

</Body>

</Html>

 

 

 

Bind page background

The Code is 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 );

}

}

}

 

 

 

Page hosting the binding page

 

 

Copy the 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">

 

 

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head runat = "server">

<Title> </title>

</Head>

<Frameset rows = "0, *">

<Frame src = "about: blank">

<Frame src = "ChildFrm. aspx">

</Frameset>

</Html>

 

 

 

On the homepage, obtain the returned js values on the front-end

 

 

Copy the 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">

 

 

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head runat = "server">

<Title> </title>

</Head>

<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 result </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.txt ShowReturnValue. value, 'dialogwidth = 1000px; dialogHeight = 900px', 'scroll: Yes ');

 

If (str! = Null)

{Document.Form1.txt ShowReturnValue. value = str ;}

}

</Script>

</Html>

Code download

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.