4. uses DataList component to show data set
Relevant introducing:
The Repeater that DataList is functional aggrandizement accuses, besides having the function with original Repeater, the pen that still can set order for goods to show data all right is counted (RepeatColumn) , by option example (SelectedItemTemple) , edit an example (EditTemplate) . But the data arrangement that DataList meeting general outputs is outputted in form, and Repeater criterion more devoted to the definition of example, won't add any HTML mark. (Attention: To can display code, all code are in under " < " later and " > " before added blank space, discomfort is excused more please! Discomfort is excused more please!!
STEP 1 builds a page
The page that we should build is shown with respect to following plan institute. Can play a detailed data when we click the super link of Detail, no less than the first in be shown. When clicking Close, detailed data can be shut, restore former model.
[Graph 1-1 page demonstrates]
Choose to want indication data first. The data to show Europe (namely Region_ID=3) , we can pick the data that chooses Region_ID=3 surely in Dataset set.
[Graph 1-2 data is chosen]
Accuse with Datalist do a data brief indication page. We build a page that does not have data first. Choose the Server Behavior in Application face plate next. Click " + " , choose DataList.
[Graph 1-3 DataList chooses a page]
In shooting the dialog box that come out, we can join page pattern plate according to needing corresponding place.
U Header: Express head pattern plate
U Item: Data pattern plate
U Alternating Item: Across shows pattern plate
U Edit Item: Modification pattern plate ((acquiesce is to was not shown those who come, must answer ability to be able to show through incident)
U Selected Item: The pattern plate of the indication after pitch on (same beard answers ability to be able to show through incident)
U Separator: Disjunctive pattern plate
U Footer: Express bottom pattern plate
[Graph 1-4 edits DataList dialog box]
We can connect the pattern plate that HTML code adds to make place need in associate with Contents, also can add data at the same time through clicking pushbutton. After clicking second pushbutton, can play a data the data that the dialog box lets you choose to need. Join in Contents input casing
<%# DataBinder.Eval(Container.DataItem, "Data ") % >
code is used at showing data.
[Graph 1-5 adds data]
To achieve previewed result, join code in Header above all: LOcation Name. In order to shows caption. Code joins in Item:
<%# DataSet1.FieldValue("LOCATION_NAME" , container) % > , will make each headline with Location_name.
Code is joined in Alternating Item
<Fontcolor="#0000F F" ><%#DataSet1.FieldValue("LOCATION_NAME" , container)% ></ Font >
Will show data with different font color.
Although Selected Item can not show immediately, but we still should write down code, call in order to have later. Following:
Address:
<%# DataSet1.FieldValue("ADDRESS" , container) % >
<BR >
City:
<%# DataSet1.FieldValue("CITY" , container) % >
<BR >
Telephone:
<%# DataSet1.FieldValue("TELEPHONE" , container) % >
<BR >
Click OK, preview a page then, the effect that pursue below even if piles up with the previous generation. Meeting such as our goes coming true show Selected Item of the effect.
[Graph 1-6 is previewed at first]
A differentia of DataList distinction Repeater can be installed namely one-way show much pen data, can undertake installing in DataList editor window.
[Graph 1-7 setting is one-way show much pen data]
Choice Use Line Breaks just have<BR > mark has separate data. Choosing Use A Table is the formal output with form, and the pen number that can decide one-way indication data through installing Table Columns.
STEP2 writes code
The need when Selected Item shows through incident, so we need to build pushbutton in order to start incident.
J adds LinkButton, in order to produces event. Move cursor of source code window<ItemTemplate > and</ in ItemTemplate > , click More Tags, asp:L chooses in shooting the dialog box that giveInkButton accuses.
[Dialog box of graph 2-1 Tag Chooser]
In editing LinkButton dialog box, install the attribute of LinkButton. The name is inputted in ID input casing: DEtail,
Input in Command Name " Detail " the order that is used at producing event, in Text dialog box input Detail(will be shown come) of of of of of of of of of of of of of of of of of of of of of of of of of of of
[Graph 2-2 LinkButton edits casing]
Needs color chooses in Layout again next, click OK, can generate code.
<Asp:LInkButton BackColor="#FFFFFF" CommandName="Detail" ForeColor="#000000" ID="Detail" Runat="server" Text="Detail" ></ Asp:LInkButton >
To be in across shows in also have such effect, we need to be in<AlternatingItemTemplate > and</ same code also is inserted in AlternatingItemTemplate > .
Pluck the sample plate after the choice that come out to shut at the same time, same need generates an order, want to add many Linkbutton so. Need to be in so<SelectedItemTemplate ></ code inserts in SelectedItemTemplate > :
<Asp:LInkButton BackColor="#FFFFFF" CommandName="Close" ForeColor="#000000" ID="Close" Runat="server" Text="Close" ></ Asp:LInkButton >
K had an order, we still need to use a program to explain this order. The process is not actually complex, need to join one fraction code only. In<Head ></ following code add in Head > :
<Script Language="VB" Runat="server" >
Sub DataList_ItemCommand(sender As Object, e As DataListCommandEventArgs)
If E.CommandSource.CommandName="Detail" Then
DataList1.SelectedIndex=e.Item.ItemIndex
ElseIf E.CommandSource.CommandName="Close" Then
DataList1.SelectedIndex=-1
End If
DataList1.DataBind()
End Sub
</ Script >
The order that the program can obtain you to click LinkButton (CommandName) will judge the order that should exe cute. It is E.Item.ItemIndex when the SelectedIndex attribute set of DataList, can open SelectedItemTemplate page. If the command is Close criterion the SelectedIndex attribute set of DataList is - 1(does not have data namely by pitch on) , criterion DataList can use ItemTemplate example shows this one data.
In<Asp:DCode is added even in AtaList > paragraph
OnItemCommand="DataList_ItemCommand "
With the relation of statement and block DataList_ItemCommand.
By " F12 " preview, the page can show the result of expect.
[Graph 2-3 is previewed finally edition]
Attach is main the source code of block:
Sub DataList_ItemCommand(sender As Object, e As DataListCommandEventArgs)
If E.CommandSource.CommandName="Detail" Then
DataList1.SelectedIndex=e.Item.ItemIndex
ElseIf E.CommandSource.CommandName="Close" Then
DataList1.SelectedIndex=-1
End If
DataList1.DataBind()
End Sub
<Form Runat="server" >
<Asp:DAtaList Id="DataList1 "
Runat="server "
RepeatColumns="1 "
RepeatDirection="Vertical "
RepeatLayout="Flow "
DataSource= "<%# DataSet1.DefaultView % > "
OnItemCommand="DataList_ItemCommand" >
<HeaderTemplate >
Location Name</ HeaderTemplate >
<ItemTemplate >
<%# DataSet1.FieldValue("LOCATION_NAME" , container) %>
<Asp:lInkbutton BackColor="#FFFFFF" CommandName="Detail" ForeColor="#000000" ID="Detail" Runat="server" Text="Detail" ></ Asp:lInkbutton >
</ ItemTemplate >
<AlternatingItemTemplate ><Font Color="#0000FF" >
<%# DataSet1.FieldValue("LOCATION_NAME" , container) % >
</ Font >
<Asp:lInkbutton BackColor="#FFFFFF" CommandNa me="Detail" ForeColor="#000000" ID="Detail" Runat="server" Text="Detail" ></ Asp:lInkbutton ></ AlternatingItemTemplate >
<SelectedItemTemplate >Address:
<%# DataSet1.FieldValue("ADDRESS" , container) % >
<BR >
City:
<%# DataSet1.FieldValue("CITY" , container) % >
<BR >
Telephone:
<%# DataSet1.FieldValue("TELEPHONE" , container) % >
<BR >
<Asp:LInkButton BackColor="#FFFFFF" CommandName="Close" ForeColor="#000000" ID="Close" Runat="server" Text="Close" ></ Asp:LInkButton >
</ SelectedItemTemplate >
</ Asp:DAtaList>
</ Form >
DataList still has the model of Edit Item, basically use at data to update. This book is met tail section introduces.