[英]Access Control value in static or shared Method in asp.net
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 我所做的是我创建dropdownlist的属性,然后在page_Load事件中获取一个值,然后将此值传递给静态方法
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. 现在,“ MAKE”属性始终显示第一个选定值为0的感染,我正在下拉列表中选择另一个值,但调试时它始终显示第一个值为“ 0”。
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: 不要让jQuery从静态页面方法中的服务器控件中获取值,而是让jQuery获取所选值并将其发送给您的page方法,如下所示:
$(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: 这需要更改静态web方法,以便允许它现在接受参数,如下所示:
'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 )
JSON.stringify函数是[JSON.js库] >>( 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
上面的示例大量使用了jQuery,请确保您的页面或母版页中有jQuery脚本文件的引用(如果正在使用它们)
- 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.
我不确定您在page方法中实际上在做什么,因为它被声明为返回String,但是它似乎试图返回一个List,以及一个未使用的Catch块,我删除了未使用的Catch和Final块,并它返回dtst对象,可以根据需要随时进行更正。
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: 您可以在服务器端允许访问“
Static
Web方法中的值的唯一选项是,当通过下拉列表的SelectedIndexChanged
事件更改下拉列表的选定值时,使用Session
缓存存储该选定值,如下所示:
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
Web方法访问Session
缓存的权限,如下所示:
'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
;注意:必须为
Session
对象使用完全限定名称,即HttpContext.Current.Session
; otherwise you will get errors.否则会出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.