How to save the value of a dropdownlist web application in a variable?

1

I have a Web application with C #, which generates a page with several dropdownlist .

The dropdownlist are filled from a stored procedure (SP) , what I require is that when I select a value of the dropdownlist (any) I store it in a variable to be able to use it as a filter of a new SP that I am building.

I enclose the code with which I am trying to store the value of this dropdownlist ; I'm using JavaScript to save it, but it does not show me anything.

This is the code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Home.Master" AutoEventWireup="true" CodeBehind="PanelGeneral.aspx.cs" Inherits="SistemaDashBoardFEMSA.PanelGeneral" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        #formulario_grafica {
            width: 1068px;
            height: 105px;
        }
    </style>
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <section class="content-header">
        <h3 style="text-align:center">REPORTES MENSUALES FEMSA</h3>
    </section>

    <section class="container">
        <div class ="row">
            <div class="col-sm-4"> <!--DROPDOWNLIST-->
                <asp:DropDownList ID="dpl_semanas" runat="server" OnSelectedIndexChanged="dpl_fechas_SelectedIndexChanged"></asp:DropDownList>
            </div>
            <div class ="col-sm-4">
                <asp:DropDownList ID="dpl_meses" runat="server" style ="width: auto"></asp:DropDownList>
            </div>
            <div class ="col-sm-4">
                <asp:DropDownList ID="Zonas" runat="server"></asp:DropDownList>
            </div>            
        </div>

        <div class="row"> <!--ESTE ES EL BOTON QUE GENERA EL EVENTO-->
            <asp:Button ID="genera_reporte" runat="server" Text="Generar" OnClientClick="mostrar()"/>                


                <script type ="text/javascript">
                    function mostrar() {
                        variable = dpl_meses.items(dpl_meses.SelectedIndex).Text;
                        alert("Hola");
                    }

                </script>
        <asp:Label ID="lbl_prueba" runat="server" Text="Label"></asp:Label>


    &nbsp;</div>
    </section>

    <section class ="container">
        <iframe src="Graficas.aspx" style="width: 1019px; height: 480px;" :></iframe>

    </section>

</asp:Content>

SOLUTION:

This is the method that controls the filling of the dropdrownlist, the dropdrownlist that takes by name "lbl_test" is filled with dpl_meses.selectedItem.text. But if they do not add the instruction (! Page.IsPostback) at the beginning, they will not be filled. Due to being a WEB application, at each page load, the data of the dropdrownlist is lost, therefore if it is not (! Page.IsPostBack) do not reload the page and prevent it from being reseted.

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Configuration; using System.Data;

namespace SystemDashBoardFEMSA {     public partial class PanelGeneral: System.Web.UI.Page     {         protected void Page_Load (object sender, EventArgs e)         {

        if (!Page.IsPostBack)
        {


            clsConexioncs conexion_server = new clsConexioncs();

            conexion_server.Conexion = ConfigurationManager.ConnectionStrings["DefaultDW"].ToString();


            //-----------------combos
            conexion_server.PreparaComandoSP("prm_spFEMSA_SemanasIndicadoresTelemetria");

            conexion_server.LlenaComboConConsulta(dpl_semanas, "Semana", "RangoFechas");


            conexion_server.PreparaComandoSP("sp_meses_femsa");
            conexion_server.LlenaComboConConsulta(dpl_meses, "Mes", null);

            conexion_server.PreparaComandoSP("prm_spFEMSA_SemanasIndicadoresTelemetria");
            conexion_server.LlenaComboConConsulta(Zonas, "FechaFin", "RangoFechas");

            //---------------------------    

        }
    }

    protected void dpl_fechas_SelectedIndexChanged(object sender, EventArgs e)
    {
        //lbl_prueba.Text = dpl_fechas.SelectedValue;
    }




    protected void genera_reporte_Click1(object sender, EventArgs e)
    {

    }

    protected void dpl_meses_SelectedIndexChanged(object sender, EventArgs e)
    {

        lbl_prueba.Text = dpl_meses.SelectedItem.Text; //esta label, se llana a partir de la selección del item del drop

    }


}

}

    
asked by Ric_hc 01.03.2017 в 17:10
source

1 answer

1

This is the method that controls the filling of the dropdrownlist , the dropdrownlist that takes the name lbl_prueba is filled with dpl_meses.selectedItem.text . But if you do not add the (!Page.IsPostback) instruction to the start, do not fill them in. Due to being a WEB application, at each page load, the data of the dropdrownlist is lost, therefore if it is not (!Page.IsPostBack) do not reload the page and prevent it from being reseted.

if (!Page.IsPostBack)
    {


        clsConexioncs conexion_server = new clsConexioncs();

        conexion_server.Conexion = ConfigurationManager.ConnectionStrings["DefaultDW"].ToString();


        //-----------------combos
        conexion_server.PreparaComandoSP("prm_spFEMSA_SemanasIndicadoresTelemetria");

        conexion_server.LlenaComboConConsulta(dpl_semanas, "Semana", "RangoFechas");


        conexion_server.PreparaComandoSP("sp_meses_femsa");
        conexion_server.LlenaComboConConsulta(dpl_meses, "Mes", null);

        conexion_server.PreparaComandoSP("prm_spFEMSA_SemanasIndicadoresTelemetria");
        conexion_server.LlenaComboConConsulta(Zonas, "FechaFin", "RangoFechas");

        //---------------------------    

    }
}

protected void dpl_fechas_SelectedIndexChanged(object sender, EventArgs e)
{
    //lbl_prueba.Text = dpl_fechas.SelectedValue;
}




protected void genera_reporte_Click1(object sender, EventArgs e)
{

}

protected void dpl_meses_SelectedIndexChanged(object sender, EventArgs e)
{

    lbl_prueba.Text = dpl_meses.SelectedItem.Text; //esta label, se llana a partir de la selección del item del drop

}


}
    
answered by 02.03.2017 / 19:15
source