Ver Mensaje Individual
  #3 (permalink)  
Antiguo 26/09/2010, 09:44
Avatar de SDEK
SDEK
 
Fecha de Ingreso: diciembre-2009
Ubicación: MX
Mensajes: 156
Antigüedad: 14 años, 4 meses
Puntos: 8
De acuerdo Respuesta: Acceder a elementos de un Array dentro de un Hashtable

Aquí te dejo un código sencillo para acceder a uno de los elementos del Hashtable. Espero te sea de utilidad. Cualquier duda, con gusto te respondo.

Código C#:
Ver original
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Collections.Generic;
  8.  
  9. namespace webappHashtables
  10. {
  11.     public partial class _Default : System.Web.UI.Page
  12.     {
  13.         string name = "";
  14.         string str = "";
  15.         Hashtable hshTable = new Hashtable();
  16.         string[] datos = new string[3];
  17.  
  18.         protected void Page_Load(object sender, EventArgs e)
  19.         {
  20.             //hshTable.Add("1", "Usuario1");
  21.             //hshTable.Add("2", "Usuario2");
  22.             //hshTable.Add("3", "Usuario3");
  23.  
  24.             datos[0] = "Telefono";
  25.             datos[1] = "direccion";
  26.             datos[2] = "oficio";
  27.  
  28.             hshTable.Add("0", datos[0]);
  29.             hshTable.Add("1", datos[1]);
  30.             hshTable.Add("2", datos[2]);
  31.  
  32.             DataList1.DataSource = hshTable;
  33.             DataList1.DataBind();
  34.  
  35.         }
  36.  
  37.         protected void Button2_Click(object sender, EventArgs e)
  38.         {
  39.             if (hshTable.ContainsKey(TextBox1.Text))
  40.             {
  41.                 Label2.Text = "El No. de Llave: " + TextBox1.Text + " ha sido encontrado";
  42.                 Label3.Text = "Tiene el dato: " + datos[2];              
  43.  
  44.             }
  45.         }
  46.  
  47.     }
  48.  
  49. }


Código ASPX:
Ver original
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="webappHashtables._Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title></title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.    
  13.         <asp:Label ID="Label1" runat="server" Text="No. de Llave: "></asp:Label>
  14.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  15.         <asp:Button ID="Button2" runat="server" Text="Buscar" onclick="Button2_Click" />
  16.         <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
  17.         <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
  18.     </div>
  19.  
  20.     <div>
  21.             <asp:DataList ID="DataList1" runat="server">
  22.              <itemtemplate>
  23.             <%# DataBinder.Eval(Container.DataItem, "Key", "<font size='1'>{0}</font>") %>
  24.             <%# DataBinder.Eval(Container.DataItem, "Value", "<font size='1'>${0:f2}</font>") %>
  25.             </itemtemplate>
  26.             </asp:DataList>
  27.     </div>
  28.  
  29.     </form>
  30. </body>
  31. </html>

Saludos,
__________________
Tecnologías de la Información
Karel Priego
| @karelpriego

Última edición por SDEK; 26/09/2010 a las 10:06