<%@ language="C#" %>
<%@ Import namespace="System.Data"%>
<%@ Import namespace="ByteFX.Data"%>
<%@ Import namespace="ByteFX.Data.MySqlClient"%>
<script language="C#" runat="server">

	public	String CorBorda;

	void Page_Load(Object src, EventArgs e)
	{
		string connectionString = "Server=localhost;Database=devgoiania;User ID=root;Password=1978rpa;";
		string sql, categoria;
		MySqlConnection 	dbcon;
		MySqlCommand 			dbcmd;
		MySqlDataAdapter 	adapter;
		MySqlDataReader 	reader;
		DataSet          	dtsArquivos;

		categoria = "1";
		
		dbcon = new MySqlConnection(connectionString);
		dbcon.Open();
		dbcmd = dbcon.CreateCommand();
		
		if(ddlCategorias.SelectedItem == null)
		{
			sql = "SELECT 'Nome' As Nome, 1 As CategoriaId";
			dbcmd.CommandText = sql;
			reader = dbcmd.ExecuteReader(CommandBehavior.CloseConnection);
			ddlCategorias.DataSource 			= reader;
			ddlCategorias.DataTextField 	= "Nome";
			ddlCategorias.DataValueField 	= "CategoriaId";
			ddlCategorias.DataBind();
			reader.Close();
		}
		
		if(Request.QueryString["Categoria"] != null)
			categoria = Request.QueryString["Categoria"];		
		
		sql = "SELECT 'Nome' As Nome, '1MB' As Tamanho, 'Free' as Licenca, 'Win2K' As Requisitos, 1 As ArquivoId, 'Algo' as Descricao, 'teste.zip' as Arquivo";
		dbcmd.CommandText = sql;
		adapter = new MySqlDataAdapter(dbcmd);
		dtsArquivos = new DataSet ();
		adapter.Fill(dtsArquivos, "dtsArquivos");		
		ViewState["Arquivos"] = dtsArquivos;
		dbcon.Close();			
		
		PagedDataSource pdsDownloads = new PagedDataSource();
		pdsDownloads.DataSource 	= ((DataSet)ViewState["Arquivos"]).Tables[0].DefaultView;
		//pdsDownloads.DataSource 	= dtsArquivos.Tables[0].DefaultView;
		pdsDownloads.AllowPaging	= true;
		pdsDownloads.PageSize 		= 20;
		int CurPage;
		if (Request.QueryString["Pagina"] != null)
			CurPage = Convert.ToInt32(Request.QueryString["Pagina"]);
		else
			CurPage = 1;
			
		pdsDownloads.CurrentPageIndex = CurPage - 1; 	

		int indicePagina = 0;
		int index = 1;
		indicePagina = CurPage - 5;
		while(indicePagina < 1)
			indicePagina++;		 
		if(pdsDownloads.PageCount > 0)
		{
		 	foreach(Control controle in Controls)
			{
				if(controle is HyperLink)
				{
					((HyperLink) controle).Visible = false;	
					if(indicePagina <= pdsDownloads.PageCount && index <= 10)
					{
						((HyperLink) controle).Visible = true;	
						((HyperLink) controle).Text    = indicePagina.ToString();
						((HyperLink) controle).NavigateUrl = Request.CurrentExecutionFilePath + "?Pagina=" + indicePagina.ToString() + "&Categoria=" + categoria;
					}
					indicePagina ++;
					index++	;			
				}
			}	
		}			
				
		rptDownloads.DataSource = pdsDownloads;
		rptDownloads.DataBind();		
	}
	
	void Command(Object src, CommandEventArgs e)
	{ 
		Response.Redirect("http://www.mono-project.com/" + e.CommandArgument.ToString());
	}
	
	void ItemSelecionado(Object src, EventArgs e)
	{
		string connectionString = "Server=localhost;Database=devgoiania;User ID=root;Password=1978rpa;";
		MySqlConnection dbcon;
		dbcon = new MySqlConnection(connectionString);
		dbcon.Close();
		dbcon.Open();
		MySqlCommand dbcmd = dbcon.CreateCommand();
		string sql = "SELECT 'Nome' As Nome, '1MB' As Tamanho, 'Free' as Licenca, 'Win2K' As Requisitos, 1 As ArquivoId, 'Algo' as Descricao, 'teste.zip' as Arquivo";
		dbcmd.CommandText = sql;
		
		MySqlDataAdapter adapter = new MySqlDataAdapter(dbcmd);
		DataSet          dtsArquivos = new DataSet ();
		adapter.Fill(dtsArquivos, "dtsArquivos");		
		ViewState["Arquivos"] = dtsArquivos;
		
		PagedDataSource pdsDownloads = new PagedDataSource();
		pdsDownloads.AllowPaging	= true;
		pdsDownloads.PageSize 		= 20;
		pdsDownloads.DataSource 	= dtsArquivos.Tables[0].DefaultView;
		rptDownloads.DataSource 	= pdsDownloads;
		rptDownloads.DataBind();	
		dbcon.Close();

		int index = 1;
		if(pdsDownloads.PageCount > 0)
		{
		 	foreach(Control controle in Controls)
			{
				if(controle is HyperLink)
				{
					((HyperLink) controle).Visible = false;	
					if(index <= pdsDownloads.PageCount && index <= 10)
					{
						((HyperLink) controle).Visible = true;	
						((HyperLink) controle).Text    = index.ToString();
						((HyperLink) controle).NavigateUrl = Request.CurrentExecutionFilePath + "?Pagina=" + index.ToString() + "&Categoria=" + ddlCategorias.SelectedItem.Value;
					}
					index++	;			
				}
			}	
		}			
	}
</script>

Categorias: <asp:DropDownList id="ddlCategorias" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ItemSelecionado"></asp:DropDownList>
<br>
<br>
<asp:repeater ID="rptDownloads" runat="server">
	<itemtemplate>
		<div width="500px" style="border-right:1px solid <%=CorBorda%>;border-left:1px solid <%=CorBorda%>;border-top:1px solid <%=CorBorda%>;border-bottom:1px solid <%=CorBorda%>;width:500px;">
			<table border="0" width="500px" cellspacing="0">
				<tr height="25px">
					<td width="60%" bgcolor="blue" style="border-right:1px solid <%=CorBorda%>;border-left:1px solid <%=CorBorda%>;border-top:1px solid <%=CorBorda%>;border-bottom:1px solid <%=CorBorda%>;"><b><asp:LinkButton style="text-decoration:none; color:white" id="lkbDownload" CommandName="CodigoArquivo" CommandArgument="<%# DataBinder.Eval(Container.DataItem, "ArquivoId")%>"   OnCommand="Command" runat="server"><%# DataBinder.Eval(Container.DataItem, "Nome")%> </asp:LinkButton></b></td>
					<td width="5%"></td>
					<td bgcolor="lightblue" width="25%" style="border-right:1px solid <%=CorBorda%>;border-left:1px solid <%=CorBorda%>;"><b style="color:blue"><%# DataBinder.Eval(Container.DataItem, "Arquivo")%></b></td>
					<td bgcolor="lightblue" width="10%" style="border-right:1px solid <%=CorBorda%>;"></td>
				</tr>
				<tr>
					<td colspan="4">Tamanho: <%# DataBinder.Eval(Container.DataItem, "Tamanho")%></td>
				</tr>	
				<tr>
					<td colspan="4">Requisitos Mínimos: <%# DataBinder.Eval(Container.DataItem, "Requisitos")%></td>
				</tr>	
				<tr>
					<td colspan="4">Licença: <%# DataBinder.Eval(Container.DataItem, "Licenca")%></td>
				</tr>
				<tr>
					<td colspan="4">Descrição: <%# DataBinder.Eval(Container.DataItem, "Descricao")%></td>
				</tr>
			</table>
		</div>
	</itemtemplate>
</asp:repeater>
<center>
<asp:HyperLink id="lnkPagina1" 	runat="server" visible="false"/>&nbsp;&nbsp
<asp:HyperLink id="lnkPagina2" 	runat="server" visible="false"/>&nbsp;&nbsp
<asp:HyperLink id="lnkPagina3" 	runat="server" visible="false"/>&nbsp;&nbsp
<asp:HyperLink id="lnkPagina4" 	runat="server" visible="false"/>&nbsp;&nbsp
<asp:HyperLink id="lnkPagina5" 	runat="server" visible="false"/>&nbsp;&nbsp
<asp:HyperLink id="lnkPagina6" 	runat="server" visible="false"/>&nbsp;&nbsp
<asp:HyperLink id="lnkPagina7" 	runat="server" visible="false"/>&nbsp;&nbsp
<asp:HyperLink id="lnkPagina8" 	runat="server" visible="false"/>&nbsp;&nbsp
<asp:HyperLink id="lnkPagina9" 	runat="server" visible="false"/>&nbsp;&nbsp
<asp:HyperLink id="lnkPagina10" runat="server" visible="false"/>&nbsp;&nbsp
</center>