Foros del Web » Programación para mayores de 30 ;) » .NET »

Pasar parámetro de boton del aspx a funcion javascript

Estas en el tema de Pasar parámetro de boton del aspx a funcion javascript en el foro de .NET en Foros del Web. Hola, No consigo recoger el valor del botón. Tengo lo siguiente en un .aspx: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código aspx: Ver original <%@ Page Title="Página principal" Language="vb" ...
  #1 (permalink)  
Antiguo 26/04/2013, 00:58
 
Fecha de Ingreso: abril-2012
Mensajes: 449
Antigüedad: 12 años, 1 mes
Puntos: 7
Pregunta Pasar parámetro de boton del aspx a funcion javascript

Hola,

No consigo recoger el valor del botón. Tengo lo siguiente en un .aspx:

Código aspx:
Ver original
  1. <%@ Page Title="Página principal" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
  2.     CodeBehind="Default.aspx.vb" Inherits="AJAXejemplo01._Default" %>
  3.  
  4. <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
  5. </asp:Content>
  6. <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">  
  7.  
  8.     <script>
  9.         var conexion1;
  10.         function obtener_operario(str)
  11.         {
  12.           if(str=='')
  13.           {
  14.             return;
  15.           }
  16.           conexion1=crearXMLHttpRequest();
  17.           conexion1.open("GET", "Default.aspx?nombre=" + str, true);
  18.           conexion1.send(null);
  19.       }
  20.  
  21.       //***************************************
  22.       //Funciones comunes a todos los problemas
  23.       //***************************************
  24.       function crearXMLHttpRequest() {
  25.           var xmlHttp = null;
  26.           if (window.ActiveXObject)
  27.               xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  28.           else
  29.               if (window.XMLHttpRequest)
  30.                   xmlHttp = new XMLHttpRequest();
  31.           return xmlHttp;
  32.       }
  33.     </script>
  34.  
  35.     <div id="busqueda">
  36.         <asp:Table ID="TableBuscar" runat="server" BorderColor="Gray" CellPadding="2" CellSpacing="5"  Width="300px" align="center">  
  37.             <asp:TableRow Font-Bold="True" Font-Overline="False">
  38.                 <asp:TableCell ID="TableCellBuscar1" HorizontalAlign="Left" Enabled="True" Width="300px">
  39.                     <asp:Label ID="LabelBuscar1" runat="server" Text="Introduzca el nombre del operario:" aling="center"></asp:Label><br />
  40.                 </asp:TableCell>
  41.             </asp:TableRow>
  42.             <asp:TableRow>
  43.                 <asp:TableCell ID="TableCellBuscar2" HorizontalAlign="Left" Enabled="True" Width="250px">
  44.                     <asp:TextBox ID="TextNombreBuscar" runat="server" aling="Left" Width="250px"></asp:TextBox>                    
  45.                     <asp:Label ID="LabelAsterisco1" runat="server" aling="center"></asp:Label><br />
  46.                 </asp:TableCell>                
  47.                 <asp:TableCell ID="TableCellBuscar3" HorizontalAlign="Left" Enabled="True" Width="50px">
  48.                     [B]<asp:Button ID="btnBuscar" runat="server" Text="BUSCAR" OnClientClick="obtener_operario(TextNombreBuscar.value);" />[/B]
  49.                 </asp:TableCell>
  50.             </asp:TableRow>
  51.         </asp:Table>      
  52.     </div>
  53.  
  54.     <div id="tabla2">
  55.         <asp:Table ID="Table2" runat="server" BorderColor="Black" CellPadding="2" CellSpacing="5"  Width="770px" align="center" HorizontalAlign="Center">  
  56.             <asp:TableRow Font-Bold="True" Font-Overline="False" BackColor="Silver">
  57.                 <asp:TableCell ID="Table2Cell1" HorizontalAlign="Center" Enabled="True" Width="150px" VerticalAlign="Middle">
  58.                     <asp:Label ID="LabelCell1" runat="server" Text="NºOPERARIO" Font-Bold="True" Font-Underline="True" ForeColor="Black" Font-Size="Medium"></asp:Label>
  59.                 </asp:TableCell>
  60.                 <asp:TableCell ID="Table2Cell2" HorizontalAlign="Center" Enabled="True" Width="300px" VerticalAlign="Middle">
  61.                     <asp:Label ID="LabelCell2" runat="server" Text="OPERARIO" Font-Bold="True" Font-Underline="True" ForeColor="Black" Font-Size="Medium"></asp:Label>
  62.                 </asp:TableCell>
  63.                 <asp:TableCell ID="Table2Cell3" HorizontalAlign="Center" Enabled="True" Width="170px" VerticalAlign="Middle">
  64.                     <asp:Label ID="LabelCell3" runat="server" Text="DETALLE MENSUAL" Font-Bold="True" Font-Underline="True" ForeColor="Black" Font-Size="Medium"></asp:Label>
  65.                 </asp:TableCell>
  66.                 <asp:TableCell ID="Table2Cell4" HorizontalAlign="Center" Enabled="True" Width="150px" VerticalAlign="Middle">
  67.                     <asp:Label ID="LabelCell4" runat="server" Text="MES" Font-Bold="True" Font-Underline="True" ForeColor="Black" Font-Size="Medium"></asp:Label>
  68.                 </asp:TableCell>
  69.             </asp:TableRow>
  70.         </asp:Table>  
  71.     </div>
  72. </asp:Content>

Lo que quiero es que cuando se clique en el Boton "btnBuscar", que lo he puesto en negrita, con la variable que se escribe en "TextNombreBuscar" vaya a la función "obtener_operario" y este, como indica la función, mande a "Default.aspx?nombre=" + str" la variable que se introduce y haga lo que se le indica en el page_load de la pagina Default.asp.

Este es el codigo que tengo en la pagina Default.aspx que esta programada en vb.net:

Código vb.net:
Ver original
  1. Imports MySql.Data.MySqlClient
  2. Imports System.Configuration
  3. Imports Oracle.ManagedDataAccess.Client
  4. Imports Oracle.ManagedDataAccess.Types
  5. Imports System.IO
  6.  
  7. Public Class _Default
  8.     Inherits System.Web.UI.Page
  9.  
  10.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  11.         'Register Ajax.NET methods from this class
  12.         'AjaxPro.Utility.RegisterTypeForAjax(GetType(_Default))
  13.         Dim nombreBuscar As String
  14.         nombreBuscar = UCase(Request.QueryString("nombre"))
  15.         Dim conn As New MySqlConnection
  16.         'se apunta a la cadena de conexion guardada en el archivo Web.config
  17.         conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("conexionMySQL").ConnectionString
  18.         Dim mysql_comando As New MySqlCommand("select distinct noperario, operario from cdp_operaciones where operario like '%" & nombreBuscar & "%'order by operario asc", conn)
  19.         Dim reader As MySqlDataReader
  20.         'Dim da As New MySqlDataAdapter(mysql_comando)
  21.         Dim objRow, objRow1 As New TableRow()
  22.         Dim numoperario, nombre As String
  23.  
  24.         'If IsPostBack = False Then
  25.  
  26.         Try
  27.  
  28.             Using conn
  29.                 'se abre la conexion
  30.                 conn.Open()
  31.  
  32.                 With mysql_comando
  33.                     reader = .ExecuteReader()
  34.                     Dim i As Integer = 1
  35.                     While reader.Read()
  36.  
  37.                         objRow = New TableRow()
  38.                         Dim objCell1, objCell2, objCell3, objCell4 As New TableCell()
  39.                         Dim linkDetalle, linkMes As String
  40.                         'crear los hyperlinks para tener acceso a los detalles mensuales
  41.                         Dim sw1 As StringWriter = New StringWriter
  42.                         Dim ht1 As HtmlTextWriter = New HtmlTextWriter(sw1)
  43.                         Dim sw2 As StringWriter = New StringWriter
  44.                         Dim ht2 As HtmlTextWriter = New HtmlTextWriter(sw2)
  45.  
  46.                         numoperario = reader.GetValue(0)
  47.                         nombre = reader.GetValue(1)
  48.  
  49.                         If numoperario <> "" Then
  50.  
  51.                             objCell1.Controls.Add(New LiteralControl(numoperario))
  52.                             objCell1.HorizontalAlign = HorizontalAlign.Center
  53.                             objCell2.Controls.Add(New LiteralControl(nombre))
  54.                             objCell2.HorizontalAlign = HorizontalAlign.Left
  55.  
  56.                             ' Create the a.
  57.                             ht1.AddAttribute(HtmlTextWriterAttribute.Href, "HorasOperarios.aspx?numOperario=" & numoperario)
  58.                             ht1.RenderBeginTag(HtmlTextWriterTag.A)
  59.                             ht1.Write("ver")
  60.                             ' End all the tags.
  61.                             ht1.RenderEndTag()
  62.                             linkDetalle = sw1.ToString()
  63.                             objCell3.Controls.Add(New LiteralControl(linkDetalle))
  64.                             objCell3.HorizontalAlign = HorizontalAlign.Center
  65.  
  66.                             ' Create the a.
  67.                             ht2.AddAttribute(HtmlTextWriterAttribute.Href, "HorasOperariosMes.aspx?numOperario=" & numoperario)
  68.                             ht2.RenderBeginTag(HtmlTextWriterTag.A)
  69.                             ht2.Write("ver")
  70.                             ' End all the tags.
  71.                             ht2.RenderEndTag()
  72.                             linkMes = sw2.ToString()
  73.                             objCell4.Controls.Add(New LiteralControl(linkMes))
  74.                             objCell4.HorizontalAlign = HorizontalAlign.Center
  75.  
  76.                             If i Mod 2 = 0 Then
  77.                                 objCell1.BackColor = Drawing.Color.AliceBlue
  78.                                 objCell2.BackColor = Drawing.Color.AliceBlue
  79.                                 objCell3.BackColor = Drawing.Color.AliceBlue
  80.                                 objCell4.BackColor = Drawing.Color.AliceBlue
  81.                             End If
  82.  
  83.                             objRow.Cells.Add(objCell1)
  84.                             objRow.Cells.Add(objCell2)
  85.                             objRow.Cells.Add(objCell3)
  86.                             objRow.Cells.Add(objCell4)
  87.                             Table2.Rows.Add(objRow)
  88.                             i = i + 1
  89.                         End If
  90.                     End While
  91.                     .Dispose()
  92.                 End With
  93.             End Using
  94.         Catch ex As Exception
  95.  
  96.             MsgBox("Error en BD" & Chr(13) & Chr(13) & ex.Message)
  97.             'mensaje("Error en BD" & Chr(13) & Chr(13) & ex.Message)
  98.         Finally
  99.             conn.Close()
  100.             conn.Dispose()
  101.         End Try
  102.         'End If
  103.     End Sub
  104. End Class

Alguien me puede echar una mano? Aunque sea al cuello....
__________________
Gracias por todo;

Un saludo
  #2 (permalink)  
Antiguo 26/04/2013, 01:06
Avatar de Malenko
Moderador
 
Fecha de Ingreso: enero-2008
Mensajes: 5.323
Antigüedad: 16 años, 3 meses
Puntos: 606
Respuesta: Pasar parámetro de boton del aspx a funcion javascript

Yo lo que veo, así por encima, es que cuando haces click llamas mediante AJAX a la página "Default.aspx". Pero no veo que obtengas lo que te devuelve esa página.

Exactamente que quieres hacer llamando por AJAX a esa página? Digo con la tabla que parece que genera.
__________________
Aviso: No se resuelven dudas por MP!
  #3 (permalink)  
Antiguo 26/04/2013, 01:27
 
Fecha de Ingreso: abril-2012
Mensajes: 449
Antigüedad: 12 años, 1 mes
Puntos: 7
Respuesta: Pasar parámetro de boton del aspx a funcion javascript

Cita:
Iniciado por Malenko Ver Mensaje
Yo lo que veo, así por encima, es que cuando haces click llamas mediante AJAX a la página "Default.aspx". Pero no veo que obtengas lo que te devuelve esa página.

Exactamente que quieres hacer llamando por AJAX a esa página? Digo con la tabla que parece que genera.
La pagina "Default.aspx" lo que hace es cuando accedes por primera vez, muestra todos los operarios que hay en una base de datos y esta pagina tiene un TextNombreBuscar que es donde puedes meter un nombre y al darle al boton Buscar buscaría los operarios correspondientes a ese nombre, tipo un buscador. Y me han dicho que para hacer ese buscador tengo que utilizar AJAX. De hay que cuando clico en el boton buscar quiero que llame a la función javaScript pasandole el parametro TextNombreBuscar.

No se si me he explicado, si no dime he intento explicarme mejor.
__________________
Gracias por todo;

Un saludo
  #4 (permalink)  
Antiguo 26/04/2013, 06:04
 
Fecha de Ingreso: abril-2012
Mensajes: 449
Antigüedad: 12 años, 1 mes
Puntos: 7
Respuesta: Pasar parámetro de boton del aspx a funcion javascript

La verdad es que es la primera vez que estoy intentando programar en AJAX y no tengo ni idea

__________________
Gracias por todo;

Un saludo

Etiquetas: aspx, bd, boton, conexion, funcion, javascript, sql, tabla, vb
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:06.