0

I have this code:

 Dim main_id As int 

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    main_id =1
End Sub

<WebMethod()> _
Public Shared Function BindPersonnel(ByVal product_id As String) As String
   Dim project_id AS int16
   project_id=main_id 'this doesn't work

End Function

On page load, I set the value of variable main_id and I need a way to somehow share the main_id with webmethod function, how can this be done? I have not tried session variable and I don't want to use session variable. I found this link Accessing a common variable in WebMethod and jQuery but I can't figure out how this is going to solve my problem. I also read some posts about using hidden field, which requires me to go two trips. I would love to avoid that.

Community
  • 1
  • 1
loveprogramming
  • 538
  • 2
  • 5
  • 22

1 Answers1

0

WebMethods are independent of the other variables on the page. If you want to access main_id, you could declare it as Private Shared main_id As Integer, but that would then cause all of your users to have access to the same ID value, which you probably don't want.

Perhaps the easiest way to do this is to store the value in SessionState, and enable sessionstate access in the WebMethod. On the other hand, this removes the asynchronous-like functionality that you are looking for (it may not be an issue).

SessionState will give you the ability to have a per-session value (avoiding the use of the Shared solution mentioned above).

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles    Me.Load
    Session("main_id") = 1
End Sub

<WebMethod(EnableSession:=True)>
Public Shared Function BindPersonnel(ByVal product_id As String) As String
   Dim project_id As Integer = CInt(HttpContext.Current.Session("main_id"))
End Function
ps2goat
  • 8,067
  • 1
  • 35
  • 68
  • ps2goat, I am trying to avoid using session as it is difficult to maintain. I have seen this post http://stackoverflow.com/questions/10329492/accessing-a-common-variable-in-webmethod-and-jquery which uses class. I wonder if there is anything similar to this that I can do. – loveprogramming Aug 19 '14 at 02:00
  • @loveprogramming, so are `product_id` and `project_id` two different things? Can't you just pass both of those values in via jQuery? – ps2goat Aug 19 '14 at 02:04
  • @loveprogramming: webmethod is part of the ancient ASMX web service technology and should probably be avoided. – John Saunders Aug 19 '14 at 02:04
  • @ps2goat, yes they are two different things. I simplify main_id by setting its value to 1 but actually there are some manipulation involved in my actual code. I can pass it from jquery but then I need to set the value in a hidden field and get value from the hidden field using jquery. I just thought there must be simpler way to get its value. – loveprogramming Aug 19 '14 at 02:11
  • What do you think of this post: http://stackoverflow.com/questions/10329492/accessing-a-common-variable-in-webmethod-and-jquery. Is there a way I can create a class and let the web method access the value through it? – loveprogramming Aug 19 '14 at 02:13
  • I would just pass the `data` in via jQuery, if you can. Put the value in a `hidden` input on the html/aspx side, somewhere you can get it out. Then your ajax request's `data` would look like `data: { "product_id": myProductId, "project_id": myProjectId}`. I'm not sure how that works with the web forms side, though. If you stringify the data (`data: JSON.stringify({"product_id": myProductID ...})`, you can pull the values from `Request.Form` the way you might from `Request.QueryString` – ps2goat Aug 19 '14 at 04:17