get select html value on vb.net

2

I have problems to recover a selected value of a <select> or combo of html from VB.NET, I have 3 <div> each with a certain function and so that only the <div> that the user selects hidden appears two.

The problem is that I can not get the value that has been selected. When I try to retrieve the server variable "cd", I get the last value of the JavaScript function switch, that is, cd="ciudad3" and not the one that I select.

<script>
    function myFunction() {
    @code dim var cd As String end code
        var x = document.getElementById("sucursal").value;

        switch (x) {
            case "":
                alert("seleccion incorrecta")
                break;
            case "ciudad1":
                document.getElementById("ciudad2").style.display = "none";
                document.getElementById("ciudad3").style.display = "none";
                document.getElementById("ciudad1").style.display = "block";
                @code cd="ciudad1" end code
                break;
            case "ciudad2":
                document.getElementById("ciudad1").style.display = "none";
                document.getElementById("ciudad3").style.display = "none";
                document.getElementById("ciudad2").style.display = "block";
                @code cd="ciudad1" end code
                break;
            case "ciudad3":
                document.getElementById("ciudad2").style.display = "none";
                document.getElementById("ciudad1").style.display = "none";
                document.getElementById("ciudad3").style.display = "block";
                @code cd="ciudad1" end code
                break;
        }
    }
</script>
<div class="panel panel-primary">
    <div class="panel-heading">
        <h4>
            <span class="glyphicon glyphicon-th"></span>
            Directorio
        </h4>
    </div>

    <div class="panel-body">
        <form method="post" role="form">
            <div class="form-group">
                <label for="sel1">Sucursal</label>
                <select class="form-control" id="sucursal" name="sucursal" onchange="myFunction()">
                    <option value="">-- Seleccione una opción --</option>
                    <option value="ciudad1">Ciudad uno</option>
                    <option value="ciudad2">Ciudad dos</option>
                    <option value="ciudad3">Ciudad tres</option>
                </select>
            </div>
        </form>
   </div
</div>
@code
      'Dim x As String = Request.Form("sucursal")

    Try

        Dim cn = ConfigurationManager.ConnectionStrings("directorio").ConnectionString

        Dim conexion As New SqlConnection(cn)
        Dim cmd As New SqlCommand()
        cmd.Connection = conexion
        cmd.CommandText = "SELECT * FROM directorio WHERE idcIUDAD = '"& cd &"' ORDER BY puesto DESC"
        conexion.Open()

        Dim dr As SqlDataReader
          dr = cmd.ExecuteReader()
          If dr.Read() Then End code
              <div class="alert alert-success" role="alert" >@dr.Item("Puesto")</div>
               <strong>Nombre.:</strong><div>@dr.Item("nombre")</div>
                <strong>Telefono.:</strong><div>@dr.Item("telefono")</div>
                <strong>Extensión.:</strong><div>@dr.Item("ext")</div> 
                <strong>E-mail.:</strong><div>@dr.Item("correo_electronico")</div>                                  
          @code    
                  End If
          end code


        @Code Catch ex As Exception
                MsgBox("Error" + ex.ToString(), MsgBoxStyle.Critical, "Fónix")
    End Try
End Code

I've tried it with request.Form("sucursal") , but you do not get me anything.

    
asked by Drago25 15.02.2016 в 20:19
source

1 answer

1

What you work from javascript you can not get them in .net code unless you send it in a post. A simple way is to assign the value to a hidden in asp.net, or if in the page you define

<asp:Hidden id="sucursal" runat="server" />

you could from javascript assign the value using

document.getElementById("<%=sucursal.ClientID%>").value = valor;

This way, when the event is given to the server, you could take the value directly from the hidden one, since it intervenes in the post by being an asp.net control

Another way you could be using ajax, for this you should evaluate using jquery, by means of $ .ajax, you could invoke WebMethod in the aspx page, in this case the data would be taken with client code and sent to the server, there is no postback or events.

Using jQuery to directly call ASP.NET AJAX page methods

Calling ASP.Net WebMethod using jQuery AJAX

The only issue here is that you need the page to have codebehind , so there is a aspx.vb associated with aspx since that's where you define the [WebMethod] that you would invoke with ajax

    
answered by 15.02.2016 в 20:45