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


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") {
                $("label[for = sreport]").text("");
            else if (selected == "Por Operación") {
                $("label[for = sreport]").text("Folio de Operación:");
            else if (selected == "Por No. de Parte") {
                $("label[for = sreport]").text("No. de Parte:");
            else if (selected == "Por Maquina") {
                $("label[for = sreport]").text("No. de Maquina:");
            else if (selected == "Por Empleado") {
                $("label[for = sreport]").text("No. de Empleado:");

<script src=""></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>
<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>

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

2 answers


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 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

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

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

answered by 03.10.2016 в 20:27