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

[SOLUCIONADO] Recoger valor de texto escrito en un textbox al clicar en un boton

Estas en el tema de Recoger valor de texto escrito en un textbox al clicar en un boton en el foro de .NET en Foros del Web. Hola, Tengo el siguiente codigo en un aspx: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código aspx: Ver original <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Ajax.aspx.vb" Inherits="AJAX1.Ajax" %>   <!DOCTYPE html PUBLIC ...
  #1 (permalink)  
Antiguo 29/04/2013, 02:26
 
Fecha de Ingreso: abril-2012
Mensajes: 449
Antigüedad: 12 años
Puntos: 7
Pregunta Recoger valor de texto escrito en un textbox al clicar en un boton

Hola,

Tengo el siguiente codigo en un aspx:
Código aspx:
Ver original
  1. <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Ajax.aspx.vb" Inherits="AJAX1.Ajax" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title>AJAX</title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.         <div>
  12.             <asp:TextBox ID="TextNombreBuscar" runat="server" aling="Left" Width="250px"></asp:TextBox>
  13.             [B]<input id="btnBuscar" type="button" value="Resultado" onclick="Ajax.obtener_operarios(TextNombreBuscar.value,obtener_operarios_CallBack)" />[/B]<br>
  14.         </div>
  15.         <div id="txtResultado" Width="100px" />
  16.         </div>
  17.     </form>
  18.  
  19.     <script type="text/javascript">
  20.         function obtener_operarios_CallBack(response) {
  21.             alert("entra");
  22.             if (response.error != null) {
  23.                 alert("Se presentó un error ");
  24.                 return;
  25.             }
  26.             target = "txtResultado";
  27.             document.getElementById(target).innerHTML = response.value;
  28.         }
  29.    </script>

y el siguiente código en el .aspx.vb:
Código vb.net:
Ver original
  1. Public Class Ajax
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.         'Register Ajax.NET methods from this class
  6.         AjaxPro.Utility.RegisterTypeForAjax(GetType(Ajax))
  7.     End Sub
  8.  
  9.     <AjaxPro.AjaxMethod()> _
  10.     Public Function obtener_operarios(ByVal nombre As String) As String
  11.  
  12.         Dim conn As New MySqlConnection
  13.         Dim nombreBuscar As String
  14.         [B]nombreBuscar = UCase(Request.QueryString("nombre"))[/B]
  15.         conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("conexionMySQL").ConnectionString
  16.         Dim mysql_comando As New MySqlCommand("select distinct noperario, operario from cdp_operaciones where operario like '%" & nombreBuscar & "%'order by operario asc", conn)
  17.         Dim reader As MySqlDataReader
  18.         Dim da As New MySqlDataAdapter(mysql_comando)
  19.         Dim objRow As New TableRow()
  20.         Dim numoperario, nombreoperario As String
  21.  
  22.         Try
  23.             Using conn
  24.                 'se abre la conexion
  25.                 conn.Open()
  26.  
  27.                 With mysql_comando
  28.                     reader = .ExecuteReader()
  29.                     Dim j As Integer = 1
  30.                     While reader.Read()
  31.  
  32.                         numoperario = reader.GetValue(0)
  33.                         nombreoperario = reader.GetValue(1)
  34.  
  35.                     End While
  36.                     .Dispose()
  37.                 End With
  38.             End Using
  39.             Return (numoperario + nombreoperario)
  40.         Catch ex As Exception
  41.             MsgBox("Error en BD" & Chr(13) & Chr(13) & ex.Message)
  42.             Return "0"
  43.         Finally
  44.             conn.Close()
  45.             conn.Dispose()
  46.         End Try
  47.         Me.TextNombreBuscar.Text = ""
  48.     End Function
  49. End Class

Lo que quiero es, cuando se le de al botón "btnBuscar" que vaya a la función "obtener_operarios" y recoger el contenido del "TextNombreBuscar" . Lo estoy haciendo de esta manera:

Código aspx:
Ver original
  1. <input id="btnBuscar" type="button" value="Resultado" onclick="Ajax.obtener_operarios(TextNombreBuscar.value,obtener_operarios_CallBack)" />
Código vb.net:
Ver original
  1. Dim nombreBuscar As String
  2. nombreBuscar = UCase(Request.QueryString("nombre"))

Pero no me coge el valor del TextNombreBuscar.

Alguilen me puede echar una mano?

__________________
Gracias por todo;

Un saludo
  #2 (permalink)  
Antiguo 29/04/2013, 03:02
 
Fecha de Ingreso: abril-2012
Mensajes: 449
Antigüedad: 12 años
Puntos: 7
Respuesta: Recoger valor de texto escrito en un textbox al clicar en un boton

Ya esta solucionado, me estaba liando yo misma.

Estaba recogiendo un valor que no se recoge así ya que la función al declararla le digo que tendrá un valor de entrada y no lo estaba cogiendo. Y también le he puesto que me pase el valor de esta forma:

Código aspx:
Ver original
  1. <input id="btnBuscar" type="button" value="Resultado" onclick="Ajax.obtener_operarios(getElementById('TextNombreBuscar').value,obtener_operarios_CallBack)" /><br>
Y lo recojo así:
Código aspx.vb:
Ver original
  1. Public Function obtener_operarios(ByVal nombre As String) As String
  2.  
  3.  
  4.         Dim conn As New MySqlConnection
  5.         Dim nombreBuscar As String
  6.         'se apunta a la cadena de conexion guardada en el archivo Web.config
  7.         nombreBuscar = nombre
  8. .......

__________________
Gracias por todo;

Un saludo

Etiquetas: bd, boton, conexion, escrito, recoger, sql, textbox, valor, 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:33.