asp. net data binding DataBind usage method

  • 2020-10-23 20:04:53
  • OfStack

A brief introduction to DataBind
DataBind includes three large methods, Repeater, DataList, and DataGrid, all of which are located in the System.Web.UI.WebControls namespace and derived directly or indirectly from the WebControl base class. These methods display the content of the data through HTML.

Establish DataBind
All DataBind should use DataBind () function to build (note that if you are using C #, please note that case) data binding, is the entire page PAGE and all controls a method, that is, he can be used by all of the controls, you build the data binding, DataBind can be used as the control of 1 item, such as DataList1. DataBind (), such as Page again. DataBind (), which can bind the entire page. DataBind is often bound as soon as the page loads. This is the case in the following example.


Protected Sub Page_Load(Src As Object, E As EventArgs) 
 DataBind()
End Sub

Start with simple data binding
Look at this example:


<script language="VB" runat="server">
 Sub SubmitBtn_Click(sender As Object, e As EventArgs) 
Page.DataBind
 End Sub
</script>
<B> Help Trinket choose 1 A wife </B>
<form runat=server>
 <asp:DropDownList id="StateList" runat="server">
<asp:ListItem> AKe </asp:ListItem>
<asp:ListItem> Once soft </asp:ListItem>
<asp:ListItem> Set up rather </asp:ListItem>
<asp:ListItem> Soak up the princess </asp:ListItem>
<asp:ListItem> sonya </asp:ListItem>
<asp:ListItem> Leader madam </asp:ListItem>
 </asp:DropDownList>
 <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat=server/>
 <p>
 The wife you have chosen for Trinket   : 
<asp:label text='<%# StateList.SelectedItem.Text %>' runat=server/>
</form>

We can see that there is no control used in the selection of the wife, but it can correctly display the result of our selection. This is the result of the binding, notice < %# StateList.SelectedItem.Text % > And that's where we get the bundled data. More often than not, we'll see examples where there's nothing in the program, but the data is already bound to it.

Bind 1 array and arrange them as a list.


<%@ Import namespace="System.Data" %>
<script language="VB" runat="server">
 Sub Page_Load(sender As Object, e As EventArgs) 
If Not IsPostBack Then 
 Dim values as ArrayList= new ArrayList()
 values.Add (" AKe ")
 values.Add (" Once soft ")
 values.Add (" Set up rather ")
 values.Add (" Soak up the princess ")
 values.Add (" sonya ") 
 values.Add (" Leader madam ")
 Dim dt As DataTable
 Dim dr As DataRow
 Dim i As Integer
 'create a DataTable
 dt = New DataTable
 dt.Columns.Add(New DataColumn(" Serial number ", GetType(Integer)))
 dt.Columns.Add(New DataColumn(" type ", GetType(String)))
 dt.Columns.Add(New DataColumn(" Whether or not ", GetType(String)))
 'Make some rows and put some sample data in
 For i = 1 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = values(i-1).ToString()
If (i > 3) Then
 dr(2) = " is "
Else
 dr(2) = " no "
End If
'add the row to the datatable
dt.Rows.Add(dr)
 Next
 DataGrid1.DataSource = new DataView(dt)
 DataGrid1.DataBind
End If
End Sub
</script>
<form runat=server>
<B> Is this Trinket's wife? </B>
<asp:DataGrid id="dataGrid1" runat="server"
 BorderColor="black"
 BorderWidth="1"
 GridLines="Both"
 CellPadding="3"
 CellSpacing="0"
 HeaderStyle-BackColor="#aaaadd"
/>
</form>

So in this example, we first set up table Dim dt As DataTable and then we set up the row concept Dim dr As DataRow, and then we add the row to the row, and finally we add the row to the table, bind DataView DataGrid1.DataSource = new DataView(dt) DataGrid1.DataBind and that's what we do, and then we use the DataGrid method, We generated the table.
Note: receiving DataBind controls, 1 have DropDownList, DataList, DataGrid, ListBox nature of these sets of controls, and bound mainly ArrayList (array), Hashtable (rare table), DataView (data view), DataReader these four.
Note 2: When we take data out of a data binding, program 1 usually converts it to String, so that when you write a program like a message book or chat room, you don't have to do the data processing, but sometimes you still have to do the data conversion. For example, If I want Boolean, what do I do? There are two methods: One is a function provided by the system:
< % # String. Format (" {0: c} ", (Container. DataItem. (" conversion type ") % > It can be converted
The binding also comes with a method < %# DataBinder. Eval(Container. DataItem, "conversion type ", "{0:c}") % >

Advanced Application Definitions section of DataBind
DataBind provides the following columns that you can define yourself
Bound is used to control data commands and readouts
HyperLink lets the data be displayed in hyperlinked form
Button creates dynamic data buttons
Template sample template mode outputs data
Here are some examples:
Bound controls display table header (base command) commands are all added in < ASP:DataGrid > < /ASP:DataGrid > Again using trinket's example above


<property name="Columns">
 <asp:BoundColumn HeaderText=" Wife number " DataField=" Serial number " />
 <asp:BoundColumn HeaderText=" Wife's name " DataField=" The name "/>
 <asp:BoundColumn HeaderText=" Whether or not " DataField=" Whether or not "/>
</property>


Do you see your form repeating twice? It's because you're here < ASP:DataGrid > The AutoGenerateColumns="false" command is not included in the tag. The default AutoGenerateColumns is for True, which means it generates its own header, which is sometimes not needed.
HyperLink When we output the data, we want to add hyperlinks under each woman's name and link to the page that describes the woman. We can use HyperLink to do this.


<property name="Columns">
 <asp:BoundColumn HeaderText=" Serial number " DataField=" Serial number " />
 <asp:HyperLinkColumn
HeaderText=" The name "
DataNavigateUrlField=" The name "
DataNavigateUrlFormatString="detailspage.aspx?id={0}"
DataTextField=" The name "
Target="_new"
/>
 <asp:BoundColumn HeaderText=" Whether or not " DataField=" Whether or not "/>
</property>


Button this is a very interesting example
with < asp:ButtonColumn HeaderText=" Add to list of women I like "Text="Add" CommandName="AddToCart" / >
Instead of < asp:BoundColumn HeaderText=" Wife number "DataField=" wife number" / >
We can trigger the AddToCart event to control the other events.
Template builds 1 template

<property name="Columns">
 <asp:TemplateColumn HeaderText=" Detailed information on ">
 <template name="ItemTemplate">
<asp:hyperlink id=HyperLink1 
  NavigateUrl = "detailspage.aspx?id={0}" runat="server">
 Click here for details 
</asp:hyperlink>
 </template>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText=" Wife's name " DataField=" The name "/>
<asp:BoundColumn HeaderText=" Whether or not " DataField=" Whether or not "/>
</property>


How do I sort in a table generated by DataBind
Add AllowSorting="true" to the ASP:DataGrid tag
Then insert the following subroutine

Sub MyDataGrid_Sort(sender As Object, e As 
DataGridSortCommandEventArgs)
SortField = e.SortField
DataGrid1.DataBind
End Sub

OK < asp:DataGrid > I don't want to do that < property > The sort option appears.


Related articles: