asp.net中将数据库绑定到DataList控件的实现方法与实例代码
生活随笔
收集整理的這篇文章主要介紹了
asp.net中将数据库绑定到DataList控件的实现方法与实例代码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
解決方法1:
datalist databind()
解決方法2:
查看MSDN上的詳細(xì)說明資料
解決方法3:
在DataList的模板中用table表格,如:
復(fù)制代碼 代碼如下:
<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">
數(shù)據(jù)庫(kù)綁定到DataList控件 </th>
</tr>
<tr>
<td width="25%" class="TableGrayRight">
數(shù)據(jù)庫(kù)綁定到DataList控件 </td>
<td class="TableNoneLeft">
<asp:Label ID="lblTypeName" runat="server" Text='<%# Eval("TypeName") %>'></asp:Label>
</td><!--數(shù)據(jù)庫(kù)綁定到DataList控件 </tr>
</table>
</ItemTemplate>
</asp:DataList>
解決方法4:
你的控件的text使用<%# Eval(" IT從業(yè)者之家(www.3ppt.com") %>
比如:txext='<%# Eval("title")%>'
綁定分頁(yè)實(shí)現(xiàn)
dlBind方法為自定義的無(wú)返回值類型方法,該方法主要用來(lái)從數(shù)據(jù)庫(kù)中查詢出符合指定條件的記錄,并綁定到DataList控件中,然后通過設(shè)置 PagedDataSource類對(duì)象的AllowPaging屬性為True,來(lái)實(shí)現(xiàn)DataList控件的分頁(yè)功能。dlBind方法實(shí)現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:
public void dlBind()
{
int curpage = Convert.ToInt32(this.labPage.Text);
PagedDataSource ps教程 = 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; //是否可以分頁(yè)
ps.PageSize = 2; //顯示的數(shù)量
ps.CurrentPageIndex = curpage - 1; //取得當(dāng)前頁(yè)的頁(yè)碼
this.lnkbtnUp.Enabled = true;
this.lnkbtnNext.Enabled = true;
this.lnkbtnBack.Enabled = true;
this.lnkbtnOne.Enabled = true;
if (curpage == 1)
{
this.lnkbtnOne.Enabled = false;//不顯示第一頁(yè)按鈕
this.lnkbtnUp.Enabled = false;//不顯示上一頁(yè)按鈕
}
if (curpage == ps.PageCount)
{
this.lnkbtnNext.Enabled = false;//不顯示下一頁(yè)
this.lnkbtnBack.Enabled = false;//不顯示最后一頁(yè)
}
this.labBackPage.Text = Convert.ToString(ps.PageCount);
this.dlContent.DataSource = ps;
this.dlContent.DataKeyField = "CardID";
this.dlContent.DataBind();
sqlcon.Close();
}
datalist databind()
解決方法2:
查看MSDN上的詳細(xì)說明資料
解決方法3:
在DataList的模板中用table表格,如:
復(fù)制代碼 代碼如下:
<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">
數(shù)據(jù)庫(kù)綁定到DataList控件 </th>
</tr>
<tr>
<td width="25%" class="TableGrayRight">
數(shù)據(jù)庫(kù)綁定到DataList控件 </td>
<td class="TableNoneLeft">
<asp:Label ID="lblTypeName" runat="server" Text='<%# Eval("TypeName") %>'></asp:Label>
</td><!--數(shù)據(jù)庫(kù)綁定到DataList控件 </tr>
</table>
</ItemTemplate>
</asp:DataList>
解決方法4:
你的控件的text使用<%# Eval(" IT從業(yè)者之家(www.3ppt.com") %>
比如:txext='<%# Eval("title")%>'
綁定分頁(yè)實(shí)現(xiàn)
dlBind方法為自定義的無(wú)返回值類型方法,該方法主要用來(lái)從數(shù)據(jù)庫(kù)中查詢出符合指定條件的記錄,并綁定到DataList控件中,然后通過設(shè)置 PagedDataSource類對(duì)象的AllowPaging屬性為True,來(lái)實(shí)現(xiàn)DataList控件的分頁(yè)功能。dlBind方法實(shí)現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:
public void dlBind()
{
int curpage = Convert.ToInt32(this.labPage.Text);
PagedDataSource ps教程 = 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; //是否可以分頁(yè)
ps.PageSize = 2; //顯示的數(shù)量
ps.CurrentPageIndex = curpage - 1; //取得當(dāng)前頁(yè)的頁(yè)碼
this.lnkbtnUp.Enabled = true;
this.lnkbtnNext.Enabled = true;
this.lnkbtnBack.Enabled = true;
this.lnkbtnOne.Enabled = true;
if (curpage == 1)
{
this.lnkbtnOne.Enabled = false;//不顯示第一頁(yè)按鈕
this.lnkbtnUp.Enabled = false;//不顯示上一頁(yè)按鈕
}
if (curpage == ps.PageCount)
{
this.lnkbtnNext.Enabled = false;//不顯示下一頁(yè)
this.lnkbtnBack.Enabled = false;//不顯示最后一頁(yè)
}
this.labBackPage.Text = Convert.ToString(ps.PageCount);
this.dlContent.DataSource = ps;
this.dlContent.DataKeyField = "CardID";
this.dlContent.DataBind();
sqlcon.Close();
}
總結(jié)
以上是生活随笔為你收集整理的asp.net中将数据库绑定到DataList控件的实现方法与实例代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET 的数据绑定,DataLi
- 下一篇: Gridview数据控件的七种字段类型