1

我正在尝试将数据从 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();
4

2 回答 2

4

举一个我前几天敲过的例子

<%@ 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]"> 

于 2009-04-07T22:07:10.803 回答
0

使用转发器在 html(如 John Nolans 的回答)方面执行此操作更容易,因为它使用模板。

如果您想在后面的代码中执行此操作,请创建一个实现 ITemplate 的类,然后像这样放置它:

myRepeater.ItemTemplate = new MyTemplateClass();
于 2009-04-07T22:21:12.830 回答