asp.net中將數(shù)據(jù)庫(kù)綁定到DataList控件的實(shí)現(xiàn)方法與實(shí)例代碼
更新時(shí)間:2011年06月28日 21:36:50 作者:
我現(xiàn)在要做這個(gè)頁(yè)面,然后我用DataList控件顯示題目,我想問(wèn)如何把我數(shù)據(jù)庫(kù)的題目綁定到DataList控件里面,然后讓它顯示考試題目?
解決方法1:
datalist databind()
解決方法2:
查看MSDN上的詳細(xì)說(shuō)明資料
解決方法3:
在DataList的模板中用table表格,如:
<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控件中,然后通過(guò)設(shè)置 PagedDataSource類對(duì)象的AllowPaging屬性為T(mén)rue,來(lái)實(shí)現(xiàn)DataList控件的分頁(yè)功能。dlBind方法實(shí)現(xiàn)代碼如下:
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ì)說(shuō)明資料
解決方法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控件中,然后通過(guò)設(shè)置 PagedDataSource類對(duì)象的AllowPaging屬性為T(mén)rue,來(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();
}
您可能感興趣的文章:
- asp.net中Datalist使用數(shù)字分頁(yè)的實(shí)現(xiàn)方法
- ASP.NET中利用DataList實(shí)現(xiàn)圖片無(wú)縫滾動(dòng) 實(shí)例分享
- ASP.Net 之Datalist刪除功能詳解附代碼
- asp.net datalist綁定數(shù)據(jù)后可以上移下移實(shí)現(xiàn)示例
- 在ASP.NET 2.0中操作數(shù)據(jù)之三十五:使用Repeater和DataList單頁(yè)面實(shí)現(xiàn)主/從報(bào)表
- 在ASP.NET 2.0中操作數(shù)據(jù)之三十六:在DataList里編輯和刪除數(shù)據(jù)概述
- 在ASP.NET 2.0中操作數(shù)據(jù)之三十七:DataList批量更新
- asp.net控件DataList分頁(yè)用法
- 在ASP.NET 2.0中操作數(shù)據(jù)之三十九:在DataList的編輯界面里添加驗(yàn)證控件
- 在ASP.NET 2.0中操作數(shù)據(jù)之四十:自定義DataList編輯界面
相關(guān)文章
ASP.NET網(wǎng)站聊天室的設(shè)計(jì)與實(shí)現(xiàn)(第3節(jié))
這篇文章主要介紹了ASP.NET網(wǎng)站聊天室的設(shè)計(jì)與實(shí)現(xiàn),了解Session、Application對(duì)象的屬性和事件,并且掌握利用它們?cè)陧?yè)面間保存和傳遞數(shù)據(jù)的方法,需要的朋友可以參考下2015-08-08
使用本機(jī)IIS?Express開(kāi)發(fā)Asp.Net?Core應(yīng)用圖文教程
IIS Express是一個(gè)Mini版的IIS,能夠支持所有的Web開(kāi)發(fā)任務(wù),本篇經(jīng)驗(yàn)將和大家介紹使用自定義主機(jī)名來(lái)訪問(wèn)運(yùn)行在IIS?Express上的站點(diǎn)程序的方法,希望對(duì)大家的工作和學(xué)習(xí)有所幫助2023-06-06
ASP.NET Core在WebApi項(xiàng)目中使用MiniProfiler分析Entity Frame
這篇文章介紹了ASP.NET Core在WebApi項(xiàng)目中使用MiniProfiler分析Entity Framework Core的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
解決ASP.NET?Core中使用漏桶算法限流的問(wèn)題
漏桶算法是限流的四大主流算法之一,其應(yīng)用場(chǎng)景各種資料中介紹的不多,一般都是說(shuō)應(yīng)用在網(wǎng)絡(luò)流量控制中,今天通過(guò)本文給大家介紹ASP.NET?Core中使用漏桶算法限流的問(wèn)題,感興趣的朋友跟隨小編一起看看吧2021-12-12
解決uploadify使用時(shí)session發(fā)生丟失問(wèn)題的方法
這篇文章主要為大家詳細(xì)介紹了uploadify使用時(shí)發(fā)現(xiàn)session發(fā)生丟失問(wèn)題的解決方法,遇到過(guò)類似問(wèn)題的朋友可以參考本文進(jìn)行解決2016-05-05

