How do I store a value of a DropDownList in a variable?

1

Good morning,

With the following C # code, I generate a DropDownList.

 public void CargarViaSolicitud()
    {
        Class.EntidadConexion cnn = new Class.EntidadConexion();
        cnn.Conexion().Open();
        SqlCommand cmd = new SqlCommand("Stp_Drops", cnn.Conexion());
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Opcion", SqlDbType.Int).Value = 3;

        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        this.ddlViaUtilizada.DataSource = dt;
        this.ddlViaUtilizada.DataValueField = "ViaSolicitudId";
        this.ddlViaUtilizada.DataTextField = "ViaSolicitud";
        this.ddlViaUtilizada.DataBind();

        cnn.Conexion().Close();
        ddlViaUtilizada.Items.Insert(0, new ListItem("Seleccione Vía... "));
    }

What I need is to capture that DataValue and DataText that the user selects, to send it to a table in the database.

I would appreciate your help.

    
asked by Oscar Diaz 03.01.2017 в 14:12
source

2 answers

3

You can use this code.

private void DdlViaUtilizada_SelectedIndexChanged(object sender, EventArgs e)
{
    string valorSeleccionado = ddlViaUtilizada.SelectedItem.Value;
    string TextoSeleccionado = ddlViaUtilizada.SelectedItem.Text;
}
    
answered by 03.01.2017 в 20:44
1

You could add a handler to the SelectedIndexChanged event as follows:

protected void Page_Load(object sender, EventArgs e)
    {
        this.ddlViaUtilizada.SelectedIndexChanged += DdlViaUtilizada_SelectedIndexChanged;
    }

    private void DdlViaUtilizada_SelectedIndexChanged(object sender, EventArgs e)
    {
        //ddlViaUtilizada.SelectedItem.Value
        //ddlViaUtilizada.SelectedItem.Text
    }
    
answered by 03.01.2017 в 19:33