c#
  • asp.net
  • sql
  • repeater
  • 2009-04-07 80 views 1 likes 
    1

    我想將數據從SQL綁定到中繼器控件。我已經嘗試了我通常爲Gridview所做的工作,但它不起作用。我希望看到一個示例,它是使用SQLAdapter還是使用命令中的ExecuteReader。謝謝!Asp.Net將SQL數據綁定到中繼器?

    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(); 
    
    +0

    請出示你嘗試過什麼,以及「不工作」。 – 2009-04-07 22:02:06

    +0

    太好了。但什麼沒有工作? – 2009-04-07 22:08:47

    回答

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

    0

    做它的HTML的例子(如約翰·諾蘭的答案)使用中繼器更容易使用模板。

    如果你想做到這一點在後面的代碼,創建一個實現然後了Itemplate把它像這樣一類:

    myRepeater.ItemTemplate = new MyTemplateClass(); 
    
    相關問題