Hello.
I have a nested GridView inside of a DataList. I need to enable the paging control of the GridView, but I have no idea where to put the code for it to work. Also I would like to create a function which will do the following: when a user will click on a specific row on the GridView, he will be redirected for the appropriate page. I tried to find answers using google, but I didn't success to understand the samples that I found.
Here is my code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class SearchCDs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
localhost.Service my = new localhost.Service();
DataTable dt = my.BringTable("CDs", "*");
DataTable dtgv1 = my.BringTable("Songs", "*");
DataTable dtgv2 = my.BringTable("Songs", "*");
DataList1.DataSource = dt;
DataList1.DataBind();
string[] arrplaylist = null;
for (int i = 0; i < dt.Rows.Count; i++)
{
arrplaylist = dt.Rows[i][2].ToString().Split(',');
for (int j = 0; j < arrplaylist.Length; j++)
{
for (int z = 0; z < dtgv1.Rows.Count; z++)
{
if (dtgv1.Rows[z][0].GetHashCode() == System.Convert.ToInt32(arrplaylist[j], 10))
dtgv1.Rows.RemoveAt(z);
}
}
for (int j = 0; j < dtgv1.Rows.Count; j++)
{
for (int z = 0; z < dtgv2.Rows.Count; z++)
{
if (dtgv1.Rows[j][0].GetHashCode() == dtgv2.Rows[z][0].GetHashCode())
dtgv2.Rows.RemoveAt(z);
}
}
((GridView)DataList1.Items[i].FindControl("gr")).DataSource = dtgv2;
((GridView)DataList1.Items[i].FindControl("gr")).DataBind();
dtgv1 = my.BringTable("Songs", "*");
dtgv2 = my.BringTable("Songs", "*");
}
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "shownhide")
{
if (((Button)(DataList1.Items[e.Item.ItemIndex].FindControl("inf"))).Text.Equals("More Info"))
{
((Button)(DataList1.Items[e.Item.ItemIndex].FindControl("inf"))).Text = "Less Info";
((GridView)(DataList1.Items[e.Item.ItemIndex].FindControl("gr"))).Visible = true;
for (int i = 0; i < DataList1.Items.Count; i++)
{
if (i != e.Item.ItemIndex)
{
((Button)(DataList1.Items[i].FindControl("inf"))).Text = "More Info";
((GridView)(DataList1.Items[i].FindControl("gr"))).Visible = false;
}
}
}
else
{
((Button)(DataList1.Items[e.Item.ItemIndex].FindControl("inf"))).Text = "More Info";
((GridView)(DataList1.Items[e.Item.ItemIndex].FindControl("gr"))).Visible = false;
}
}
}
}
Here is the source:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="SearchCDs.aspx.cs" Inherits="SearchCDs" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table>
<tr>
<td style="width: 100px">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px">
<asp:DataList ID="DataList1" runat="server" BackColor="White" BorderColor="#E7E7FF" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" RepeatColumns="3" RepeatDirection="Horizontal" BorderStyle="None" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<table border="1">
<tr>
<td rowspan="3" style="width: 100px">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "pic", "albums/{0}") %>' Height="100px" Width="100px" /></td>
<td style="width: 100px">
Name:</td>
<td style="width: 100px">
<%# DataBinder.Eval(Container.DataItem, "NameCD")%>
</td>
<td rowspan="3" style="width: 100px">
<asp:Button ID="Button1" runat="server" Text="Buy" /></td>
</tr>
<tr>
<td style="width: 100px">
Price:</td>
<td style="width: 100px">
<%# DataBinder.Eval(Container.DataItem, "price")%>$</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="inf" runat="server" CommandName="shownhide" Text="More Info" /></td>
</tr>
<tr>
<td colspan="4" rowspan="1">
<asp:GridView ID="gr" runat="server" AutoGenerateColumns="False" Height="150px" PageSize="2" Visible="False" Width="150px" OnPageIndexChanging="gr_PageIndexChanging" >
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="NameSong" HeaderText="Name" />
<asp:BoundField DataField="AlbumSong" HeaderText="Album" />
<asp:BoundField DataField="BandSong" HeaderText="Performer" />
</Columns>
</asp:GridView>
<asp:Button ID="Button2" runat="server" Text="Edit Album" />
</td>
</tr>
</table>
</ItemTemplate>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<AlternatingItemStyle BackColor="#F7F7F7" />
<SelectedItemStyle BackColor="#738A9C" ForeColor="#F7F7F7" Font-Bold="True" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
</asp:DataList></td>
</tr>
<tr>
<td style="width: 100px">
</td>
</tr>
</table>
</asp:Content>
Please guide me where and what exactly to add to this code in order that the paging function and the select function will work properly. Please write in C#.
Thanks.