asp.net implementation and instance code for binding a database to an DataList control
- 2020-05-12 02:27:33
- OfStack
Solution 1:
datalist databind()
Solution 2:
See the specifications on MSDN
Solution 3:
Use the table form in the DataList template, such as:
Solution 4:
Use text for your control < %# Eval(" IT practitioner's home (www.3ppt.com") % >
For example: txext = ' < %# Eval("title")% > '
Binding paging implementation
The dlBind method is a custom method with no return value type. This method is mainly used to query the records that meet the specified conditions from the database and bind them to the DataList control. Then, the paging function of the DataList control is realized by setting the AllowPaging property of the PagedDataSource class object to True. The implementation code of dlBind method is as follows:
datalist databind()
Solution 2:
See the specifications on MSDN
Solution 3:
Use the table form in the DataList template, such as:
<asp:DataList ID="dlDetailedInfo" runat="server" OnItemDataBound="dlDetailedInfo_ItemDataBound" Width="100%">
<ItemTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="TablebTitle">
<tr>
<th colspan="2" scope="col">
Database bound to DataList controls </th>
</tr>
<tr>
<td width="25%" class="TableGrayRight">
Database bound to DataList controls </td>
<td class="TableNoneLeft">
<asp:Label ID="lblTypeName" runat="server" Text='<%# Eval("TypeName") %>'></asp:Label>
</td><!-- Database bound to DataList controls </tr>
</table>
</ItemTemplate>
</asp:DataList>
Solution 4:
Use text for your control < %# Eval(" IT practitioner's home (www.3ppt.com") % >
For example: txext = ' < %# Eval("title")% > '
Binding paging implementation
The dlBind method is a custom method with no return value type. This method is mainly used to query the records that meet the specified conditions from the database and bind them to the DataList control. Then, the paging function of the DataList control is realized by setting the AllowPaging property of the PagedDataSource class object to True. The implementation code of dlBind method is as follows:
public void dlBind()
{
int curpage = Convert.ToInt32(this.labPage.Text);
PagedDataSource ps The tutorial = new PagedDataSource();
sqlcon = new SqlConnection(strCon);
sqlcon.Open();
string sqlstr = "select a.*,b.* from tb_Card as a join tb_Module as b on a.ModuleID=b.ModuleID";
SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlstr, sqlcon);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, "tb_Card");
ps.DataSource = ds.Tables["tb_Card"].DefaultView;
ps.AllowPaging = true; // Can I page through it
ps.PageSize = 2; // Quantity displayed
ps.CurrentPageIndex = curpage - 1; // Gets the page number of the current page
this.lnkbtnUp.Enabled = true;
this.lnkbtnNext.Enabled = true;
this.lnkbtnBack.Enabled = true;
this.lnkbtnOne.Enabled = true;
if (curpage == 1)
{
this.lnkbtnOne.Enabled = false;// Don't show the first 1 Page button
this.lnkbtnUp.Enabled = false;// Don't show up 1 Page button
}
if (curpage == ps.PageCount)
{
this.lnkbtnNext.Enabled = false;// Don't show the 1 page
this.lnkbtnBack.Enabled = false;// Don't show last 1 page
}
this.labBackPage.Text = Convert.ToString(ps.PageCount);
this.dlContent.DataSource = ps;
this.dlContent.DataKeyField = "CardID";
this.dlContent.DataBind();
sqlcon.Close();
}