how to get html label value from ASP.NET server side c #?

1

On my client side I have an html select where changing the index changes an html label with jquery

    $(document).ready(function () {

        $("select[name = sreport]").change(function () {
            var selected = $("option:selected", this).text();
            if (selected == "Seleccione el tipo de Reporte") {
                $('#divtxtbuscar').hide();
                $('#divbuscar').hide();
                $("label[for = sreport]").text("");
               
            }
            else if (selected == "Por Operación") {
                $('#divtxtbuscar').show();
                $('#divbuscar').show();
                $("label[for = sreport]").text("Folio de Operación:");
            }
            else if (selected == "Por No. de Parte") {
                $('#divtxtbuscar').show();
                $('#divbuscar').show();
                $("label[for = sreport]").text("No. de Parte:");
            }
            else if (selected == "Por Maquina") {
                $('#divtxtbuscar').show();
                $('#divbuscar').show();
                $("label[for = sreport]").text("No. de Maquina:");
            }
            else if (selected == "Por Empleado") {
                $('#divtxtbuscar').show();
                $('#divbuscar').show();
                $("label[for = sreport]").text("No. de Empleado:");
            }


        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Tipo de Reporte:</label>
<select id="cmb" name="sreport" class="form-control" >
  <option value="seleccionar">Seleccione el tipo de Reporte</option>
  <option value="operacion">Por Operación</option>
  <option value="parte">Por No. de Parte</option>
  <option value="maquina">Por Maquina</option>
  <option value="empleado">Por Empleado</option>
</select>
<div id="divtxtbuscar" class="form-group" style="display: none">
  <label id="lblsreport" for="sreport" runat="server"></label><br />
  <asp:TextBox ID="txtbuscar" runat="server" CssClass="form-control"></asp:TextBox>
</div>

Now what I want is to take the value of my label from the side of my server (C #) and save it in a variable but I can not use .value, I use innertext and it does not return anything. What other option do I have?

Or how can I change an asp label instead of an html?

    
asked by Danitza Ayala 03.10.2016 в 20:11
source

2 answers

0

To take it using the .value as mencioans you should define the runat="server" in <select> in this way if you define an id to the combo you could access it as an htmlcontrol

HtmlSelect Class

But the alternative is for you to forget the asp.net events and send using jquery using $ .ajax by invoking a webmethod on the web server But if you do this you will no longer use an event or need to access the controls on the server since you send the data from the client using json, as I explain here

[jQuery] DropDownList nested

    
answered by 03.10.2016 / 20:29
source
2

In Webforms the names change according to how they are structured, for example:

You give:

<asp:TextBox ID="txtEcho2" runat="server" ClientIDMode="Static" /> 

And render:

<input id="Text1" name="ctl00$MasterPageBody$ctl00$txtEcho2" />

What you should do in your JavaScript is something like the following:

<script type="text/javascript">
  var miVariableEnJavaScript = '<%= txtUsername.ClientID %>';
  //Con esto obtienes el valor
  $(miVariableEnJavaScript).value;
</script>

This JavaScript must be in a .aspx file to recognize the <%... %>

    
answered by 03.10.2016 в 20:27