Asp. Net:datalist control's selection input function

Source: Internet
Author: User
Tags eval response code
The asp.net| control uses the <SelectedItemTemplate> properties of the DataList control to lay out the data in the table as it pleases. According to the link, the user is friendly to display the data of interest, we look at the following example.

9.3.2 DataList control's selection input function

Add a Web Form to the Datacon Web project, named Datalist_sample2.aspx, add a DataList control, and the main HTML code for datalist_sample2.aspx is as follows:

<form id= "Form1" method= "POST" runat= "Server" >
<asp:datalist id= "DataList1"
runat= "Server" repeatcolumns= "1"
Bordercolor= "#000099" cellpadding= "0"
Borderwidth= "1px" gridlines= "Both" >
<selecteditemstyle font-size= "X-small" ></SelectedItemStyle>
<HeaderTemplate> Student Information </HeaderTemplate>
<SelectedItemTemplate>
Name: <%# DataBinder.Eval (Container.DataItem, "name")%>
(Item No: <%# DataBinder.Eval (Container.DataItem, "id")%>) <br>
Sex: <%# DataBinder.Eval (Container.DataItem, "sex")%><br>
Professional: <%# DataBinder.Eval (Container.DataItem, "major")%><br>
Class: <%# DataBinder.Eval (Container.DataItem, "class")%><br>
Address: <%# DataBinder.Eval (Container.DataItem, "adress")%><br>
Tel: <%# databinder.eval (Container.DataItem, "tel")%><br>
Email: <%# DataBinder.Eval (Container.DataItem, "email")%><br>
<asp:linkbutton runat= "Server" commandname= "close" > Shutdown </asp:LinkButton>
</SelectedItemTemplate>
<alternatingitemstyle backcolor= "Gainsboro" ></AlternatingItemStyle>
<separatorstyle backcolor= "#339966" ></SeparatorStyle>
<itemstyle font-size= "X-small" ></ItemStyle>
<ItemTemplate>
Item No: <%# DataBinder.Eval (Container.DataItem, "id")%>
Name: <%# DataBinder.Eval (Container.DataItem, "name")%>
<asp:linkbutton runat= "Server" commandname= "Moreinfor" >
Details </asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</form>

In the application of this example, we need to be aware of the <SelectedItemTemplate> layout format and the format used to add controls. When we click on the LinkButton control in the DataList control, the LinkButton control is the basis of which the LinkButton control's CommandName property is raised. The event that is raised by the button-type control deployed in the DataList control is a ItemCommand event procedure, and all we have to do is add the response code in the process.

Here's a look at the logical code in DataList_Sample2.aspx.vb:

'-----Code begin----
' Omit references to namespaces
Public Class Datalist_sample2
Inherits System.Web.UI.Page
#Region the code generated by the Web Forms Designer
' The code generated by the form designer is omitted here
#End Region
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
GetData ()
End Sub
Sub GetData ()
Dim Mycon as Oledb.oledbconnection
Try
Mycon = New oledb.oledbconnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath (".") + "\ Studentinfor.mdb ")
Dim mycmd as Oledb.oledbdataadapter = New oledb.oledbdataadapter ("SELECT * from Student", mycon)
Dim dt as Data.dataset = New Data.dataset
myCMD. Fill (DT)
Datalist1.datasource = dt. Tables (0). DefaultView
Datalist1.databind ()
Catch ex as Exception
Response.Write ("program error, information described below:<br>" & Ex.) message.tostring)
Finally
Mycon. Close ()
End Try
End Sub
Private Sub datalist1_selectedindexchanged (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles DataList1. SelectedIndexChanged
End Sub
Private Sub Datalist1_itemcommand (ByVal source as Object, ByVal e as System.Web.UI.WebControls.DataListCommandEventArgs ) Handles Datalist1.itemcommand
If e.commandname = "Moreinfor" Then
' When you click the [Details] link, show details
Datalist1.selectedindex = E.item.itemindex
ElseIf e.commandname = "Close" Then
' When the close link is clicked, close the details
Datalist1.selectedindex =-1
End If
GetData ()
End Sub
End Class
'-----Code end-------

After you save the compilation, the effect is shown in Figure 9.15.


Figure 15

Asp. NET does not have the built-in paging capability of the system DataList control, but this does not mean that the DataList control does not support paging, we can pass DataAdapter.Fill (datatable,intstartpage,intmaxpages, Strdatatablename) to implement DataList paging in a programmatic way. Readers can see the knowledge in the custom paging functionality of the DataGrid control.



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.