簡體   English   中英

使用dataList循環鏈接按鈕和標簽

[英]Looping Linkbutton and Label using dataList

我正在嘗試使用DataListItemTemplate搜索數據庫。

我只想在linkbutton和一行label循環來自數據庫的數據。

我是新來的,在此先感謝

DataSet ds = new DataSet();
DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("Description", typeof(string)));
dt.Columns.Add(new DataColumn("Auctionno", typeof(string)));
dt.Columns.Add(new DataColumn("Location", typeof(string)));
SqlCommand cmd = new SqlCommand("select * from Auction_Upload where Keyword = '" + TextBox1.Text + "'", con);

con.Open();
SqlDataReader dr = cmd.ExecuteReader();

if(dr.Read())
{
    DataRow dc = dt.NewRow();
    dc["Description"] = dr["Description"].ToString();
    dc["Auctionno"] = dr["Auctionno"].ToString();
    dc["Location"] = dr["Location"].ToString();
    dt.Rows.Add(dc);
}

DataList1.DataSource = dt;
DataList1.DataBind();

aspx代碼:

<asp:DataList ID="DataList1" runat="server" Width="600">
    <ItemTemplate> 
        <br /> 
        <asp:LinkButton ID="LinkButton1" Font-Names="Raleway,sans-serif" Font-Size="15" runat="server" Text='<%#Eval("Auctionno") %>' /> 
        <br /> 
        <asp:Label ID="Label1" Font-Size="12" Font-Names="Raleway,sans-serif" runat="server" Text='<%#Eval("Description") %>' />
    </ItemTemplate> 
</asp:DataList> 

正如您在評論中所說,現有查詢僅返回一行,因此這就是您的DataList僅顯示一行的原因。

如果要從數據庫獲取所有行,請更改查詢並全部獲取

select * from Auction_Upload /*just remove the WHERE clause*/

其他一切都很好。

將if更改為While,您將獲得行,因為使用datareader.Read()一次只能讀取一行。

while(dr.Read())
{
    DataRow dc = dt.NewRow();
    dc["Description"] = dr["Description"].ToString();
    dc["Auctionno"] = dr["Auctionno"].ToString();
    dc["Location"] = dr["Location"].ToString();
    dt.Rows.Add(dc);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM