miércoles, octubre 25, 2006

Menu de árbol - TreeView en ASPX y C#



--- ASPX ---

<%@ Page language="c#" Codebehind="Menu.aspx.cs" AutoEventWireup="false" Inherits="Sorteo.Menu" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<TITLE>Menu</TITLE>
<META content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<META content="C#" name="CODE_LANGUAGE">
<META content="JavaScript" name="vs_defaultClientScript">
<META content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="default.css" type="text/css" rel="stylesheet">
</HEAD>
<BODY ms_positioning="GridLayout">
<!--<FORM runat="server">
<TABLE width="100%">
<TR>
<TD colspan="3">
<DIV style="BACKGROUND-IMAGE: url(http://localhost/Sorteo/images/logo sorteo jaguar ur - watermark 1.jpg);WIDTH: 783px;POSITION: relative;HEIGHT: 144px"
ms_positioning="GridLayout">
<ASP:IMAGE id="Image1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
height="128px" width="128px" imageurl="images/logo sorteo jaguar ur.jpg"></ASP:IMAGE>
<ASP:LABEL id="Label1" style="Z-INDEX: 102; LEFT: 152px; POSITION: absolute; TOP: 24px" runat="server"
width="352px" font-size="XX-Large" font-names="Times New Roman">Sorteo Jaguar UR</ASP:LABEL>
<ASP:LABEL id="lblSorteo" style="Z-INDEX: 103; LEFT: 512px; POSITION: absolute; TOP: 24px"
runat="server" font-size="XX-Large" font-names="Times New Roman">2006</ASP:LABEL></DIV>
</TD>
</TR>
<TR>
<TD width="209" style="WIDTH: 209px">
<DIV style="MARGIN: 0px; OVERFLOW: scroll; WIDTH: 208px; CLIP: rect(0px auto auto 0px); POSITION: static; HEIGHT: 500px">-->
<DIV>
<SCRIPT languaje="javascript">
classPath = "class.tree";
ftv2blank = "ftv2/ftv2blank.gif";
ftv2doc = "ftv2/ftv2doc.gif";
ftv2folderclosed = "ftv2/ftv2folderclosed.gif";
ftv2folderopen = "ftv2/ftv2folderopen.gif";
ftv2lastnode = "ftv2/ftv2lastnode.gif";
ftv2link = "ftv2/ftv2link.gif";
ftv2mlastnode = "ftv2/ftv2mlastnode.gif";
ftv2mnode = "ftv2/ftv2mnode.gif";
ftv2node = "ftv2/ftv2node.gif";
ftv2plastnode = "ftv2/ftv2plastnode.gif";
ftv2pnode = "ftv2/ftv2pnode.gif";
ftv2vertline = "ftv2/ftv2vertline.gif";
basefrm = "main";
</SCRIPT>
<SCRIPT src="class.tree/ftiens4.js" type="text/javascript">
</SCRIPT>
<SPAN id="spnJavaScript" runat="server"></SPAN>
</DIV>
<!--</TD>
<TD width="60%" align="center" valign="middle"><IMG height="400" alt="" src="images/logo sorteo jaguar ur - fondo.jpg" width="425">
</TD>
<TD width="25%"></TD>
</TR>
</TABLE>
</FORM>-->
</BODY>
</HTML>


--- C# ---

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Sorteo;

namespace Sorteo
{
/// <summary>
/// Summary description for Menu.
/// </summary>
public class Menu : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlGenericControl spnJavaScript;

void Page_Load(Object sender, EventArgs e)
{

buildMenu();


}

protected void buildMenu()
{
MenuWS ws = new MenuWS();

DataSet menuDS = ws.getMenu();
DataTable menuDT = menuDS.Tables["Menu"];
string varName="";
string papaVarName="";
string treeCommand = "";
string hayHijos;
string caption;
string url;

spnJavaScript.Controls.Add(new LiteralControl("<scr" + "ipt languaje=\"javascript\">\n"));

foreach(DataRow opcion in menuDT.Rows)
{
varName = "aux" + opcion["Nivel"];
papaVarName = "aux" + Convert.ToString(Convert.ToInt32(opcion["Nivel"])-1);
caption = Convert.ToString(opcion["Opcion"]).Trim();
url = Convert.ToString(opcion["URL"]).Trim();

if (Convert.ToInt32(opcion["Nivel"]) == 0)
{
treeCommand = varName + "= gFld(\"" + caption + "\", \"" + url + "\")\n";
}
else
{
hayHijos = Convert.ToString(opcion["hayHijos"]);
if (hayHijos.Equals("Y"))
{
treeCommand = varName + "= insFld(" + papaVarName + ", gFld(\"" + caption + "\", \"" + url + "\"))\n";
}
else
{
treeCommand = "insDoc(" + papaVarName + ", gLnk(" + papaVarName + ", \"" + caption + "\", \"" + url + "\"))\n";
}
}

treeCommand = treeCommand.Replace("aux0","foldersTree");
spnJavaScript.Controls.Add(new LiteralControl(treeCommand));
}
spnJavaScript.Controls.Add(new LiteralControl("initializeDocument()\n"));
spnJavaScript.Controls.Add(new LiteralControl("</scr" + "ipt>\n"));
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

--- WebService ---


<%@ WebService Language="c#" Codebehind="MenuWS.asmx.cs" Class="Sorteo.MenuWS" %>


--- WebService C# ---


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace Sorteo
{
/// <summary>
/// Summary description for CartasWS.
/// </summary>
public class MenuWS : System.Web.Services.WebService
{
public MenuWS()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}

private System.Data.SqlClient.SqlConnection con;
private System.Data.SqlClient.SqlDataAdapter spGetMenuDA;
private System.Data.SqlClient.SqlCommand spGetMenu;

#region Component Designer generated code

//Required by the Web Services Designer
private IContainer components = null;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
this.con = new System.Data.SqlClient.SqlConnection();
this.spGetMenuDA = new System.Data.SqlClient.SqlDataAdapter();
this.spGetMenu = new System.Data.SqlClient.SqlCommand();
//
// con
//
this.con.ConnectionString = ((string)(configurationAppSettings.GetValue("con.ConnectionString", typeof(string))));
this.con.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.con_InfoMessage);
//
// spGetMenuDA
//
this.spGetMenuDA.SelectCommand = this.spGetMenu;
this.spGetMenuDA.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Menu", new System.Data.Common.DataColumnMapping[0])});
//
// spGetMenu
//
this.spGetMenu.CommandText = "dbo.[spGetMenu]";
this.spGetMenu.CommandType = System.Data.CommandType.StoredProcedure;
this.spGetMenu.Connection = this.con;
this.spGetMenu.Parameters.Add(new System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));
this.spGetMenu.Parameters.Add(new System.Data.SqlClient.SqlParameter("@iIDMenu", System.Data.SqlDbType.Int, 4));

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

/// <summary>
/// Regresa el arbol del menu ordenado ... por default el menu 1
/// </summary>
/// <returns>Regresa un DataSet con los datos del menu, ya ordenado, en forma de arbol.</returns>
[WebMethod]
public DataSet getMenu()
{
DataSet ds = new DataSet();

con.Open();
spGetMenu.Parameters["@iIDMenu"].Value = 1; // Cargar el menu por default
spGetMenuDA.Fill(ds,"Menu");
con.Close();

return ds;
}

private void con_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
{

}
}
}

2 comentarios:

  1. veo que te gusta el .net

    personalmente ami me gusta mas pa programacion wey el php y mysql o jsp XD no entiendo mucho de .net jejejej

    ResponderBorrar
  2. La verdad no me gusta tanto, he publicado algunas cosas de dotNet, pero por falta de tiempo no he subido algo de Flex, que es lo que mas me gusta hoy en dia.

    Donde trabajo usamos dotNet, donde trabaje usabamos Delphi, Java/JSP, y C#, el año pasado estuve en un proyecto con Java,Interbase y Flex. He hecho algunas aplicaciones con PHP. No me gusta casarme con ninguna, ya lo he hecho con Delphi por ejemplo y dime ¿donde está Delphi? en el olvido.

    Hay que ser pragmático: Usar lo que sirva y te de para la papa.

    ResponderBorrar