Combo dinamico

13/07/2004 - 21:53 por Franco Figún | Informe spam
Hola grupo
Por casualidad alguien tendrá un ejemplo de un combo dinamico en asp.net con
c#?
Lo que intento hacer, es un combo de 3 categorias (Pais, provincia, ciudad),
y que al seleccionar pais en el combo1, en el combo 2 aparezca la provincia,
y asi sucesivamente...tambien busco hacer el abm de las tablas de forma
dinamica, pero eso mas adelante.
Gracias

FF
www.francofigun.com.ar
MSN: francofigun@hotmail.com
Yahoo MSN: frankofm@yahoo.com.ar
 

Leer las respuestas

#1 Alejandro Perez
13/07/2004 - 23:10 | Informe spam
Hola Franco, como estas? Mira, aqui te hice un ejemplo de
lo que necesitas. Puedes mejorarlo mucho mas porque
sinceramente lo hice un poco apurado (voy saliendo a
clases :p), pero es una referencia a lo que tienes que
hacer. Solo implemente region y territorio, pero seria lo
mismo para ciudad.

Cuidate y me avisas si te ayudó en algo!
Bye

Alejandro Perez
MCSD/MCDBA/MCT
Caracas - Venezuela

CODIGO:
.asp.cs

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 System.Data.SqlClient;

namespace PaisRegion
{
/// <summary>
/// Descripción breve de WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.DropDownList ddlPais;
protected
System.Web.UI.WebControls.DropDownList ddlCiudad;
protected System.Web.UI.WebControls.Label
lblPais;
protected System.Web.UI.WebControls.Label
lblCiudad;
private SqlConnection cnConexion;
private SqlDataAdapter daNorthWind;
private SqlDataReader drPais;
private SqlDataReader drCiudad;

private void Page_Load(object sender,
System.EventArgs e)
{
if(!Page.IsPostBack)
{
cnConexion.Open();

drPais=daNorthWind.SelectCommand.ExecuteReader();
ddlPais.DataSource=drPais;

ddlPais.DataTextField="RegionDescription";

ddlPais.DataValueField="RegionID";
ddlPais.DataBind();
cnConexion.Close();
}
}

#region Código generado por el Diseñador
de Web Forms
override protected void OnInit(EventArgs
e)
{
//
// CODEGEN: llamada requerida por
el Diseñador de Web Forms ASP.NET.
//
InitializeComponent();
base.OnInit(e);
cnConexion = new SqlConnection
("Initial Catalog=Northwind;Data
Source=localhost;Integrated Security=SSPI;");
daNorthWind = new SqlDataAdapter
("SELECT * FROM Region",cnConexion);
}

/// <summary>
/// Método necesario para admitir el
Diseñador. No se puede modificar
/// el contenido del método con el editor
de código.
/// </summary>
private void InitializeComponent()
{
this.ddlPais.SelectedIndexChanged
+= new System.EventHandler
(this.ddlPais_SelectedIndexChanged);
this.Load += new
System.EventHandler(this.Page_Load);
}
#endregion

private void ddlPais_SelectedIndexChanged
(object sender, System.EventArgs e)
{
ddlCiudad.Enabled=true;
cnConexion.Open();

daNorthWind.SelectCommand.CommandText="SELECT *
FROM Territories WHERE RegionID="+ddlPais.SelectedValue;

drCiudad=daNorthWind.SelectCommand.ExecuteReader
();
ddlCiudad.DataSource=drCiudad;

ddlCiudad.DataTextField="TerritoryDescription";
ddlCiudad.DataBind();
cnConexion.Close();
}
}
}



-
-
-
-
CODIGO .aspx:


<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="PaisRegion.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</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/intell.../ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post"
runat="server">
<asp:DropDownList id="ddlPais"
style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP:
40px" runat="server"
Width="144px"
Height="24px" AutoPostBack="True"></asp:DropDownList>
<asp:Label id="lblPais" style="Z-
INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 16px"
runat="server">Pais</asp:Label>
<asp:DropDownList id="ddlCiudad"
style="Z-INDEX: 102; LEFT: 200px; POSITION: absolute;
TOP: 40px"
runat="server"
Width="136px" Height="24px"
Enabled="False"></asp:DropDownList>
<asp:Label id="lblCiudad"
style="Z-INDEX: 104; LEFT: 208px; POSITION: absolute;
TOP: 16px"
runat="server"
Width="72px" Height="24px">Ciudad</asp:Label></form>
</body>
</HTML>






Hola grupo
Por casualidad alguien tendrá un ejemplo de un combo


dinamico en asp.net con
c#?
Lo que intento hacer, es un combo de 3 categorias (Pais,


provincia, ciudad),
y que al seleccionar pais en el combo1, en el combo 2


aparezca la provincia,
y asi sucesivamente...tambien busco hacer el abm de las


tablas de forma
dinamica, pero eso mas adelante.
Gracias

FF
www.francofigun.com.ar
MSN:
Yahoo MSN:


.

Preguntas similares