繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM