[英]Asp.Net binding SQL data to a repeater?
我試圖將數據從SQL綁定到轉發器控件。 我已經嘗試過我通常為Gridview做的事情而且它不起作用。 我想看一個例子,它是使用SQLAdapter還是使用命令中的ExecuteReader。 謝謝!
string sql = "SELECT [WallPost], [DatePosted] FROM [WallTable] WHERE [UserId] = '"
+ Request["friend"] + "'";
string strCon =
System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(strCon);
SqlDataAdapter daUserProfile = new SqlDataAdapter(sql, conn);
dsSocialSite.UserProfileDataTable tbUserProfile =
new dsSocialSite.UserProfileDataTable();
daUserProfile.Fill(tbUserProfile);
rpWall2.DataSource = tbUserProfile; //rpWall2 = repeater control
rpWall2.DataBind();
用一個例子我前幾天敲了一下
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true"CodeFile="Default.aspx.cs" Inherits="Default2"
Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<div id="Questions" align="center" style="background-color: #C0C0C0">
<asp:Repeater ID="QuestionsRepeater" runat="server"
DataSourceID="SqlDataSourceQuestions">
<ItemTemplate>
<div align="left" style="text-indent: 15px">
<asp:Label ID="Label1"
runat="server" Text= '<%# Eval("QCategory") %>'
Font-Bold="True" Font-Size="Medium"></asp:Label>
</div>
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
DataSourceID="SqlDataSourceRatings" DataTextField="RatingsCategory"
DataValueField="RatingsCategory" RepeatDirection="Horizontal" >
</asp:RadioButtonList>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSourceQuestions" runat="server"
ConnectionString="<%$ ConnectionStrings:sandboxConnectionString %>"
SelectCommand="SELECT [QCategory] FROM [QuestionsCategory]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceRatings" runat="server"
ConnectionString="<%$ ConnectionStrings:sandboxConnectionString %>"
SelectCommand="SELECT [RatingsCategory], [RatingsId] FROM [Ratings]">
因為它使用模板,所以在轉發器上使用html(如在John Nolans的答案中)這樣做更容易。
如果你想在后面的代碼中執行它,創建一個實現ITemplate的類,然后將其放置如下:
myRepeater.ItemTemplate = new MyTemplateClass();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.