Hi, I've form, on it I've a dropdown list wiht different values. I'm sending the values of the form via ajax to the code behind (using a pagemethod) to make some cheks and then an insert on my DB. The problem I've is that I'm obtainning the value "undefined".
What should I do to obtain the value of the dropdown list? Thanks in advance.
<WebMethod()> _
Public Shared Function IntroNoMovido(ByVal idvehiculo As String, ByVal fecha As Date) As String
The value of idvehiculo it's undefined.
If instead of string I put integer I obtain this error:
{"Message":"undefined no es un valor válido para Int32.","StackTrace":" en System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)\r\n en System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n en System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n en System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n en System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n en System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n en System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.Exception"}
I fill my dropdonw list like this (this is does it fine):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dsTodo As New DataSet
Dim cs As ConnectionStringSettings
cs = ConfigurationManager.ConnectionStrings(ObtenerString.ObtenerConex)
Dim connString As String = cs.ConnectionString
Dim dbConnection As New SqlConnection(connString)
Dim cmd As New SqlCommand()
Dim da As New SqlDataAdapter()
cmd = New SqlCommand("SPAdmin_Select_Vehiculos", dbConnection)
cmd.CommandType = CommandType.StoredProcedure
da.SelectCommand = cmd
da.Fill(dsTodo)
Me.ddlIdVeh.DataValueField = "id_vehiculo"
Me.ddlIdVeh.DataTextField = "matricula"
Me.ddlIdVeh.DataSource = dsTodo
Me.ddlIdVeh.DataBind()
Me.ddlIdVeh.Items.Insert(0, "-------------------------")
Me.ddlIdVeh.SelectedValue = 0
End Sub
Ever than a programmer makes an extra hour god kills a kitten
kilofafeure
Participant
1507 Points
753 Posts
Obtainning the value of a dropdown list with jquery to send it via pagemethod
Feb 15, 2012 03:23 PM|LINK
Hi, I've form, on it I've a dropdown list wiht different values. I'm sending the values of the form via ajax to the code behind (using a pagemethod) to make some cheks and then an insert on my DB. The problem I've is that I'm obtainning the value "undefined". What should I do to obtain the value of the dropdown list? Thanks in advance.
My aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="IntroNoMovido.aspx.vb" Inherits="IntroNoMovido" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxControlToolkit" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="Scripts/jquery-ui-1.8.17.custom.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#btnIntroducir').click(function () { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "IntroNoMovido.aspx/IntroNoMovido", data: "{ idvehiculo: '" + $('#tbidvehiculo').val() + "',fecha:'" + $('#tbfecha').val() + "'}", dataType: "json", success: function (data) { if (data.d == "0") {} else if (data.d == "1") {} else if (data.d == "2") {} else if (data.d == "3") {} else if (data.d == "4") {} else if (data.d == "5") {} else if (data.d == "6") {} } }); }); }); </script> </head> <body"> <form id="formIntroNoMovido" runat="server"> <asp:ScriptManager id="ScriptManager5" EnablePartialRendering="true" EnablePageMethods="true" runat="server"></asp:ScriptManager> <div id="divIntroNoMovido"> <fieldset> <legend>Introducir No Movido</legend> <table> <tr> <td>Vehículos</td> <td><asp:DropDownList id="ddlIdVeh" runat="server"></asp:DropDownList> </td> </tr> <tr> <td>Fecha </td> <td><asp:TextBox id="tbfecha" runat="server" /> AAAA/MM/DD</td> <AjaxControlToolkit:FilteredTextBoxExtender id="FTb2" TargetControlID="tbfecha" FilterType="Custom" FilterMode="ValidChars" ValidChars="1234567890/" runat="server" /> </tr> <tr> <td></td> <td><input type="button" id="btnIntroducir" value="Introducir" /><input type="reset" id="btreset" /></td> </tr> </table> </fieldset> <asp:Label Text="" Enabled="true" id="lblOkIntroNoMovido" runat="server" style="color:Green" /> <asp:Label Text="" Enabled="true" id="lblErrorIntroNoMovido" runat="server" style="color:Red" /> </div> </form> </body> </html>My code behind:
<WebMethod()> _ Public Shared Function IntroNoMovido(ByVal idvehiculo As String, ByVal fecha As Date) As StringThe value of idvehiculo it's undefined.
If instead of string I put integer I obtain this error:
{"Message":"undefined no es un valor válido para Int32.","StackTrace":" en System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)\r\n en System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n en System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n en System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n en System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n en System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n en System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.Exception"}I fill my dropdonw list like this (this is does it fine):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim dsTodo As New DataSet Dim cs As ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings(ObtenerString.ObtenerConex) Dim connString As String = cs.ConnectionString Dim dbConnection As New SqlConnection(connString) Dim cmd As New SqlCommand() Dim da As New SqlDataAdapter() cmd = New SqlCommand("SPAdmin_Select_Vehiculos", dbConnection) cmd.CommandType = CommandType.StoredProcedure da.SelectCommand = cmd da.Fill(dsTodo) Me.ddlIdVeh.DataValueField = "id_vehiculo" Me.ddlIdVeh.DataTextField = "matricula" Me.ddlIdVeh.DataSource = dsTodo Me.ddlIdVeh.DataBind() Me.ddlIdVeh.Items.Insert(0, "-------------------------") Me.ddlIdVeh.SelectedValue = 0 End Subkilofafeure
Participant
1507 Points
753 Posts
Re: Obtainning the value of a dropdown list with jquery to send it via pagemethod
Feb 16, 2012 08:21 AM|LINK
Solved, the problem was here:
data: "{ idvehiculo: '" + $('#tbidvehiculo').val() + "',fecha:'" + $('#tbfecha').val() + "'}",it should be:
data: "{ idvehiculo: '" + $('#ddlIdVeh').val() + "',fecha:'" + $('#tbfecha').val() + "'}",