Asp.Net将SQL数据绑定到转发器?

时间:2009-04-07 22:01:03

标签: c# asp.net sql 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();

2 个答案:

答案 0 :(得分: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]"> 

         

答案 1 :(得分:0)

使用模板,在转发器上使用html(在John Nolans的答案中)这样做更容易。

如果你想在后面的代码中执行它,创建一个实现ITemplate的类,然后将其放置如下:

myRepeater.ItemTemplate = new MyTemplateClass();