lunes, 15 de mayo de 2017

ASP.NET: Demostración de DropDownList

Demostración de un DropDownList, en Web Form de ASP.NET, que permite seleccionar un cliente y muestra el Código del Cliente seleccionado.

https://drive.google.com/open?id=0B7Ct7K_LTJvSeV9Ja3NKLXBuc1U


DemoCombo.aspx


<%@ Page Title="" Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="true" CodeBehind="DemoCombo.aspx.cs" Inherits="CrediSeguro.DemoCombo" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <title>Demo Combo</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphPrincipal" runat="server">
    <!-- Primera fila Grid -->
    <div class="row">
        <div class="col-sm-12 col-md-12 col-lg-12">
            <div class="jumbotron">
                <h1>Demo Combo</h1>
            </div>
        </div>
    </div>
    <!-- Primera fila Grid -->

    <div class="row">
        <!-- Primera Columna -->
        <div class="col-xs-1 col-md-2 col-lg-4">
        </div>
        <!-- Segunda Columna -->
        <div class="col-xs-11 col-md-10 col-lg-8">
            <form action="DemoCombo.aspx" method="post" runat="server">
                <fieldset>
                    <legend>Datos:</legend>
                    <div class="form-group">
                        <label class="control-label">Código</label>
                        <asp:Label ID="lblCodigo" runat="server"
                             Text="Label"></asp:Label>
                        <asp:DropDownList ID="ddClientes" runat="server"
                             AutoPostBack="true"
                             OnSelectedIndexChanged=
                             "ddClientes_SelectedIndexChanged" >
                             </asp:DropDownList>
                        <asp:SqlDataSource ID="sdsClientes" runat="server">
    </asp:SqlDataSource>
                    </div>
                </fieldset>
            </form>
           
        </div>

    </div>

</asp:Content>


DemoCombo.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CrediSeguro
{
    public partial class DemoCombo : System.Web.UI.Page
    {
        private void LlenarCombo()
        {
            StringBuilder sbCond;
            string sApellido = "";
            string sCondicion;
            sbCond = new StringBuilder();

            sApellido = "Da";
            //Construye el SELECT
            sbCond.Append(" SELECT CodCliente, Apellido+', '+Nombre AS Cliente ");
            sbCond.Append(" FROM dbo.Cliente ");
            sbCond.Append(" WHERE ");
            sbCond.AppendFormat(" Apellido LIKE '{0}%' ", sApellido);
            sbCond.Append(" ORDER BY Apellido, Nombre ");
            //SELECT FROM WHERE
            sCondicion = sbCond.ToString();

            //Configura la conexión del DataSource
            sdsClientes.ConnectionString = Properties.Settings.Default.ConexionSQL;
            //Configura el Select del DataSource
            sdsClientes.SelectCommand = sCondicion;

            //Asocia el Combo con los datos
            //Campo que se va a mostrar en el Combo
            ddClientes.DataTextField = "Cliente";
            //Valor que devuelve al seleccionar el elemento del Combo
            ddClientes.DataValueField = "CodCliente";
            ddClientes.DataSource = sdsClientes;
            ddClientes.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Solo llena el contenido del combo
            if (!IsPostBack)
            {
                //Llenar el Combo
                LlenarCombo();
            }
        }

        protected void ddClientes_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Cuando selecciona un elemento del Combo
            //Cambia el valor del componente Label lblCodigo
            lblCodigo.Text = ddClientes.SelectedValue;
        }
    }
}







No hay comentarios.:

Publicar un comentario