let say you have static method in which you want to access the dropdownlist selected value textbox value and some other controls value.. my problem is that i am always getting first selected value of dropdownlist even whatever value i select in dropdown
what i did i create a property of dropdownlist then get a value in page_Load event then pass this value into static method
Partial Public Class stocklist
Inherits System.Web.UI.Page
'Static or shared Property'
Private Shared _make As DropDownList
Public Shared Property MAKE() As DropDownList
Get
Return _make
End Get
Set(ByVal value As DropDownList)
_make = value
End Set
End Property
'Page Load'
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'ddlMake is the dropdownlist and MAKE is a Property'
MAKE = ddlMake
End Sub
'Static or shared Web Method'
<WebMethod()> _
Public Shared Function Select_Search() As SearchParameters()
Dim JSON As New List(Of SearchParameters)()
Dim dtst As New DataTable()
Dim myList As New dsStockTableAdapters.newSTOCK_LISTTableAdapter()
dtst = myList.GetData(MAKE.SelectedValue, "0", "0", "0", "0", DisplayType, "", "", "", "Any", "", "", "", "", "", 0, "", "", 0, "")
Try
For Each rdr As DataRow In dtst.Rows
Dim SRCH As New SearchParameters()
SRCH.CHASSIS_NO = rdr("CHASSIS_NO").ToString()
SRCH.MODEL = rdr("MODEL").ToString()
SRCH.color = rdr("color").ToString()
SRCH.TRANSMISSION = rdr("TRANSMISSION").ToString()
SRCH.DOOR = rdr("DOOR").ToString()
SRCH.MAKE = rdr("MAKE").ToString()
SRCH.Image1 = rdr("Image1").ToString()
SRCH.MODEL_DESCRIPTION = rdr("MODEL_DESCRIPTION").ToString()
JSON.Add(SRCH)
Next
Catch
Finally
End Try
Return JSON.ToArray()
End Function
End Class
Now a "MAKE" property always showing the first selected value which is 0 infect i am selecting the other value in dropdownlist but it`s always showing first value "0" while debuging.
how to access the dropdownlist selected value in static method???
Instead of trying to get the value from the server control in the static page method, have jQuery get the selected value and send it to your page method, like this:
$(document).ready(function() {
var selectedMake = $('#<%= MAKE.ClientID %>' option:selected").text();
var args = {
theMake : selectedMake
}
$.ajax({
type: "POST",
url: "YourPageName.aspx/Select_Search",
data: JSON.stringify(args),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
// Do something with result here
}
});
});
This requires a change to the static web method, in order to allow it to accept a parameter now, like this:
'Static or shared Web Method'
<WebMethod()> _
Public Shared Function Select_Search(theMake As String) As string
Dim dtst As New DataTable()
Dim List As New dsStockTableAdapters.newSTOCK_LISTTableAdapter()
dtst = List.GetData(theMake)
Return dtst
End Function
Now you do not have to attempt to find the value of the server control in a static method, because it was sent to the static method as a parameter.
Notes:
- The JSON.stringify function is part of the [JSON.js library]>>( https://github.com/douglascrockford/JSON-js )
- The example above makes extensive use of jQuery, make sure you have a reference in your page or master page, if you are using them, to the jQuery script file
- I am not sure what you are actually doing in your page method, because it is declared as returning a String, but it appears to trying to return a List, along with an unused Catch block, I deleted the unused Catch and Finally blocks and had it return the dtst object, feel free to correct this as needed.
UPDATE:
The only option you have server-side to allow access to the value in the Static
web method is to use Session
cache to store the drop down's selected value when it is changed via the drop down's SelectedIndexChanged
event, like this:
Sub Index_Changed(sender As Object, e As EventArgs)
Session("SelectedMakeValue") = ddlMake.SelectedItem.Value
End Sub
Now you must give the Static
web method access to the Session
cache, like this:
'Static or shared Web Method'
<WebMethod(EnableSession := True)> _
Public Shared Function Select_Search() As string
Dim dtst As New DataTable()
Dim List As New dsStockTableAdapters.newSTOCK_LISTTableAdapter()
Dim theMake As String = HttpContext.Current.Session("SelectedMakeValue").ToString()
dtst = List.GetData()
Return dtst
End Function
Note: You must use the fully qualified name for the
Session
object, which isHttpContext.Current.Session
; otherwise you will get errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.