【问题标题】:Asp.Net binding specific fields of sql query to ListviewAsp.Net 将 sql 查询的特定字段绑定到 Listview
【发布时间】:2009-04-14 20:17:50
【问题描述】:

我有一个包含几个不同控件的列表视图。我需要将查询的特定字段绑定到控件的某些部分。该表包含一个friendid 和一个名字。我想把friendid放在URL的末尾。我想把名字放在标签的文本中。查询将返回多个朋友。列表视图应在单独的行中显示所有内容。这是我所拥有的,这显然是不对的。

<asp:ListView ID="lvFriends" runat="server">
    <LayoutTemplate>
    <ul ID="itemPlaceholderContainer" runat="server" style="">
        <li ID="itemPlaceholder" runat="server" />
    </ul>
</LayoutTemplate>
    <ItemTemplate>
        <li>
            <div style="float:left;">
                <a id="A1" href='<%# ResolveUrl(String.Format("~/UserPages/FriendsPages/FriendsProfile.aspx?friend={0}", Container.DataItem)) %>' runat="server"><asp:Image ID="ImageButton1" ImageUrl='<%# ResolveUrl(String.Format("~/UserPages/FriendsPages/FriendsThumbImage.aspx?friend={0}", Container.DataItem)) %>' runat="server" /></a>
        </div>

            <div style="margin-left:300px;">
        <ul>
            <li><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FriendFN") %>' /></li>
        </ul>
    </div>
        </li>
    <div style="clear:both;"></div>
    </ItemTemplate>
<ItemSeparatorTemplate><br /></ItemSeparatorTemplate>
</asp:ListView>

代码隐藏:

string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strCon))
{
    using (SqlCommand cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT * from [Friends] WHERE [UserId] = @UserId";
        cmd.Parameters.AddWithValue("@UserId", User.Identity.Name);

        DataTable dtFriends = new DataTable();
        dtFriends.Columns.Add("FriendId");
        dtFriends.Columns.Add("FriendFN");

        SqlDataReader nwReader = cmd.ExecuteReader();
        while (nwReader.Read())
        {
            string RdFriendId = nwReader["FriendId"].ToString().ToLower();
            string RdFriendFN = nwReader["FriendFirstName"].ToString().ToLower();

            dtFriends.Rows.Add(new object[] {RdFriendId, RdFriendFN});
        }
        nwReader.Close();
        Session.Add("FriendsTable", dtFriends);

        conn.Close();
        lvFriends.DataSource = dtFriends;
        lvFriends.DataBind();
    }
}

【问题讨论】:

    标签: asp.net sql listview bind


    【解决方案1】:

    我会考虑使用中继器控件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多