Intimate contact asp.net (5) DataBind data core

Source: Internet
Author: User
Tags bind eval final tostring

This section is mainly to talk about DataBind, which is very important in the ASP.net, almost all the controls need it to control the operation of the data. It can also be said to be the ASP.net data core. Let's take a look at a simple example:

<% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<script language= "C #" runat= "Server" >
public void Page_Load (Object Src,eventargs E)
{
file:// First set up an array
ArrayList arr=new ArrayList ();
Arr. ADD ("flying Knife");
Arr. ADD ("Zsir");
Arr. ADD ("Gale");
Arr. ADD ("pudding");
Arr. Add ("Ya Hao");

file:// bind the array to the DropDownList control
Ddl. DataSource = arr;
Ddl. DataBind ();
}
</script>
<title></title>
<body>
<asp:dropdownlist id= "DDL" runat= "Server"/>
</body>

The final display is:

Flying Knife Zsir Gale Buddingahau

We can see in the code that we've built a DropDownList, but he doesn't have <asp:ListItem> attributes, and we can still see the options listed in the final display.

Here's what we've done with the DataBind, in the Page_Load method we built an array (ArrayList) and bundled the array into the DropDownList control through the DataBind method, Make DropDownList finally have data show:, How to bind have a certain perceptual knowledge of it. Let's start with a formal explanation.

In fact, DataBind (), not only can bind the control, but also to the page properties, methods to bundle, or even the entire page can be bundled. For example, if you call the Page.DataBind () method or use DataBind () directly, the entire page is bundled and all the data is under surveillance. For the following example, we'll use the DataBind method to bundle the DropDownList and get the data

<% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<script language= "C #" runat= "Server" >
public void Sub_click (Object Sender,eventargs e)
{
Page.DataBind ();
}
</script>
<title></title>
<body>
<form runat=server>
<asp:dropdownlist id= "DDL" runat= "Server" >
<asp:listitem>asp Technology </asp:ListItem>
<asp:listitem selected>asp. NET Technology </asp:ListItem>
<ASP:LISTITEM>JSP Technology </asp:ListItem>
<asp:listitem>php Technology </asp:ListItem>
<asp:ListItem> Component Technology </asp:ListItem>
</asp:DropDownList>
<br>
Your choice now is: <font color=red><%# DDL. Selecteditem.text%></font> District
<br>
<asp:button id= "Sub" text= "submitted" type= "Submit" Runat=server/>
</form>
</body>

After implementation, we select the JSP technology we click the "Submit" button, see the situation is:


ASP TechnologyAsp. NET TechnologySELECTED>JSP TechnologyPHP TechnologyComponent Technology


Your choice now is:JSP technology Area

We saw that the red [JSP technology], we didn't use any controls, but he was able to correctly display our selection results, this is the result of bundling, note <%# DDL. Selecteditem.text%> This sentence, it is it let us get the bundle of data. It looks like we are familiar with the <%=...%> this statement, their use of the method is not much different, just <%=...%> is in the execution of the program, <%# ...%> is called after the DataBind () method. Later we can often see his figure, hehe. The control that receives bind, generally has dropdownlist,datalist,datagrid,listbox controls of these set properties, while the main bundle is ArrayList (array), Hashtable (HA dilute table), DataView ( Data View), DataReader These four, we will be able to take a seat, there will be no DataTable bundled error:

When it comes to bind, you can't say the DataBinder.Eval () method.

We're using databind to get the data, and the system will think of it as string, which is very handy for our normal output, but we don't need string types every time, and sometimes we need to boolean,int32 these types. At this point we need to transform the type. Perhaps the first thing people think of is the String.Format method, this is the best, but the usage is too annoying. So it's better not to. We can use the DataBinder.Eval () method, his format is:

DataBinder.Eval (Container.DataItem, "type of conversion", "format")

The last "format" is optional, generally do not have to care about him, Container.DataItem is a bundle of data items, "conversion type" refers to the Integer,string,boolean this kind of thing.

With it, we'll be more comfortable with the data.

Finally we look at an example, about DataView bundle, hehe, always with DropDownList annoying, this time with the DataGrid, in this example we can see "How the Table is tempered"

<% @ Page language= "C #"%>
<% @ Import namespace= "System.Data"%>
<script language= "C #" runat= "Server" >
public void Page_Load (Object Src,eventargs E)
{
int i;
file:// to establish the data of the table
DataTable dt=new DataTable ();
DataRow Dr;
file://to build The column example, you can specify the type of the example, where you use the default string
Dt. Columns.Add (New DataColumn ("number"));
Dt. Columns.Add (New DataColumn ("username"));
for (i=1;i<8;i++)
{
Dr=dt. NewRow ();
Dr[0]=int32.tostring (i);
dr[1]= "ASPCN" +int32.tostring (i);
Dt. Rows.Add (DR);
}
file:// Tied
DG1. DataSource = new DataView (DT);
DG1. DataBind ();
}
</script>
<title></title>
<body>
<asp:datagrid id= "DG1" Runat=server align=center headerstyle-backcolor= "#aaaadd"/>

</body>

The situation we are seeing is:

Numbered User name
1 aspcn1
2 ASPCN2
3 Aspcn3
4 ASPCN4
5 Aspcn5
6 Aspcn6
7 aspcn7

I will not say more about this program, the program has instructions, just to pay attention to our bind here is DataView, not DataTable, this is a common mistake:)

Here our foundation is finished, we should be more familiar with the Web control, or later unintelligible:

Later, we'll take a few verses to talk about the database call.



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.