I have a listview that has a few different controls. I need to bind specific fields of a query to certain parts of a control. The table contains a friendid and a firstname. I want to put the friendid at the end of a URL. I want to put the firstname in the text of a label. There will be multiple friends returned on the query. The listview should show all in a separate row. Here is what I have, which is obviously not right.
<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>
CODE BEHIND:
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();
}
}