Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.Net将sql查询的特定字段绑定到Listview_Asp.net_Sql_Listview_Bind - Fatal编程技术网

Asp.Net将sql查询的特定字段绑定到Listview

Asp.Net将sql查询的特定字段绑定到Listview,asp.net,sql,listview,bind,Asp.net,Sql,Listview,Bind,我有一个listview,它有几个不同的控件。我需要将查询的特定字段绑定到控件的某些部分。该表包含一个friendid和一个firstname。我想把friendid放在URL的末尾。我想把名字放在标签的文本中。查询中将返回多个好友。listview应在单独的一行中显示所有内容。这就是我所拥有的,这显然是不对的 <asp:ListView ID="lvFriends" runat="server"> <LayoutTemplate> <ul ID="

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

<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();
    }
}