2

I am using __doPostBack to send data from JavaScript to code behind page in ASP.NET application.

The JavaScript is as follows,

$('.window_month td').on('click', function () {
    var idName = this.id;
    var selectedid = idName.substring(1);
    $('#hidden').val(selectedid);
    __doPostBack(idName, '');
});

I am using the variable like this in .cs page,

string s = Request["__EVENTTARGET"];

I want to use this __EVENTTARGET value outside of page_load and assign it to a global variable in ASP.NET.

How can I do this?

nrvbha
  • 139
  • 4
  • 19
  • can you show the .aspx markup and the related javascript. also you can create a javascript that uses the `__doPostBack()` method that references an event in your .cs file as well also `Request["__EVENTTARGET"]` is an object so I would also suspect that you would need to cast that object to a `(string)` if you plan to assign it's contents to a string variable. I would also use a `Session[""] ` variable personally to hold that information or a hidden field.. Can you report on if you can step into that code or not and report back if there is a issue or not ..? – MethodMan Feb 05 '15 at 16:33
  • checkout this post as well and remember `Google is your best friend` use it sometime http://stackoverflow.com/questions/2234542/how-to-intercept-any-postback-in-a-page-asp-net – MethodMan Feb 05 '15 at 16:38
  • There is no issue in assigning Request["__EVENTTARGET"] value to string variable s. I just want to access this string variable outside of page_load and assign it's value to a global variable. – nrvbha Feb 05 '15 at 16:40
  • 1
    then you will need to have a static property or store it in a Session variable because if your are doing postbacks on the page load the public string variable would be overwritten and set to `string.Empty` on every page load.. I would personally initialize a `HttpContext.Current.Session["Target"] = string.Empty` in the `Global.asax` page in the OnSessionStart method and then assign it and from there you can access it application wide.. does that make sense..? :) – MethodMan Feb 05 '15 at 17:07
  • What do you mean by "global variable"? That doesn't have a specific meaning in C#. Do you just mean a class level field? – mason Feb 05 '15 at 17:29
  • 1
    @MethodMan. Yes, it makes sense. And it is solved using Session. Thank you very much. – nrvbha Feb 05 '15 at 17:56

2 Answers2

0

Used Session and it worked. Thanks all.

nrvbha
  • 139
  • 4
  • 19
0

then you will need to have a static property or store it in a Session variable because if your are doing postbacks on the page load the public string variable would be overwritten and set to string.Empty on every page load.. I would personally initialize a HttpContext.Current.Session["Target"] = string.Empty in the Global.asax page in the OnSessionStart() method and then assign it and from there you can access it application wide.. does that make sense..? :)

MethodMan
  • 18,625
  • 6
  • 34
  • 52