Hi merve,
I have created sample code which fullfill your requirement.
HTML
<div>
<asp:DataList ID="dlresimYoluMulti" runat="server" OnItemDataBound="dlresimYoluMulti_ItemDataBound">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblCateGoryId" Text='<%#Eval("CateGoryId")%>' runat="server" />
<%--Put the categoryId instead user userId --%>
<asp:DataList ID="dlImage" RepeatColumns="2" runat="server">
<ItemTemplate>
<a href="#">
<img id="imgResimm1" src='<%#Eval("Url")%>' width="50px" height="50px" class="w3-margin-bottom" />
</a>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:DataList>
</div>
C#
private string config = ConfigurationManager.ConnectionStrings["Constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetData();
}
}
private void GetData()
{
using (SqlConnection con = new SqlConnection(config))
{
using (SqlCommand cmd = new SqlCommand("SELECT CateGoryId,CategoryName FROM Images GROUP BY CategoryName,CateGoryId", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
dlresimYoluMulti.DataSource = dt;
dlresimYoluMulti.DataBind();
}
}
}
}
protected void dlresimYoluMulti_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int categoryId = Convert.ToInt32((e.Item.FindControl("lblCateGoryId") as Label).Text); // you are get UserId here pass the sql query like categoryId.
DataList dlImage = e.Item.FindControl("dlImage") as DataList;
using (SqlConnection con = new SqlConnection(config))
{
using (SqlCommand cmd = new SqlCommand("SELECT [Url] FROM [Images] WHERE CategoryId=@CategoryId ", con))
{
cmd.Parameters.AddWithValue("@CategoryId", categoryId);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
dlImage.DataSource = dt;
dlImage.DataBind();
}
}
}
}
}
Vb.net
Private config As String = ConfigurationManager.ConnectionStrings("Constring").ToString()
Protected Sub Page_Load(sender As Object, e As EventArgs)Handles Me.Load
If Not Me.IsPostBack Then
Me.GetData()
End If
End Sub
Private Sub GetData()
Using con As New SqlConnection(config)
Using cmd As New SqlCommand("SELECT CateGoryId,CategoryName FROM Images GROUP BY CategoryName,CateGoryId", con)
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
dlresimYoluMulti.DataSource = dt
dlresimYoluMulti.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub dlresimYoluMulti_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim categoryId As Integer = Convert.ToInt32(TryCast(e.Item.FindControl("lblCateGoryId"), Label).Text)
Dim dlImage As DataList = TryCast(e.Item.FindControl("dlImage"), DataList)
Using con As New SqlConnection(config)
Using cmd As New SqlCommand("SELECT [Url] FROM [Images] WHERE CategoryId=@CategoryId ", con)
cmd.Parameters.AddWithValue("@CategoryId", categoryId)
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
dlImage.DataSource = dt
dlImage.DataBind()
End Using
End Using
End Using
End If
End Sub
Screenshot