What i want:
pass values to public properties in a Page and call that specific instance, i cant use query variables coz that can be manipulated, also there are a lot of properties so keeping in session would be memory intensive, is there any way to assign the properties to the page and response.redirect to the page.
I had a look at the following post but dint get much of any idea.
Response.Redirect to Class that inherits from UI.Page?
Does anybody know any solution thanks
If you are not worried about SEO on the second/new page you could try Server.Transfer
http://msdn.microsoft.com/en-us/library/ie/ms525800(v=vs.90).aspx
Yes you can use query by encrypt it. Do not make you life so difficult with complex thinks, just encrypt it, and decrypt it with error checking and that's all.
here is a sample code for encrypting query strings:
http://www.codeproject.com/Articles/33350/Encrypting-Query-Strings
you can also make a cross-page postback that check for the valid of the data, see this example:
Cross-page postbacks and back again retaining data from source page
Related
Unfortunately because of the wide use of the word "hashtag" and "httprequest" i couldn't find any search results that gave me an answer on whether something like this is even possible.
If i have a url like this:
/Orders/Product#12345
The HttpRequest class shows me that the FilePath, RawUrl, and all other members that show the url as
/Orders/Product
It just gets rid of the hashtag, and i can't find a place to view it.
Is there any way for me to be able to see what hashtag is at the end of the URL from the codebehind? I know i could easily make this a QueryString parameter, but i like the way this looks better, so if there's a way to do it, i'd like to find out what it is :)
Thanks in advance!
It just gets rid of the hashtag, and i can't find a place to view it.
That's because it doesn't get sent to the server. It's not part of the request - it's only relevant on the client side. If you need to do anything clever with it, you'll need to write some Javascript to access it.
Browser is not required to do GET request in case of navigation to bookmarks on the same page (what #12345 is). So you may reconsider using it for normal requests.
As others have stated this value is not sent to the server. While it's possible to send the value to the server using JavaScript, in this instance you should make it a query string parameter.
If you are using it in a view:
Typeahead
I want to define a variable or object like session, to stored data about each user, and i want to access this object in many pages,
could u please tell me if there is a way.
thanks
You have onlyy a few choices, really. URL parameters, hidden form inputs, cookies, session (be careful in a load-balanced scenario) or just store/retrieve stuff from a database. RaveDB is bloomin' brilliant for this because it's so fast and document-based.
You can store data in a cookie and then in your codebehind parse the specific cookie into something like a System.Collections.Generic.Dictionary
But you should use sessions.
Edit
IF if's a KeyValuePair<String,TValue> you can set Session[kvp.Key] = kvp.Value;, if not Session["KVP"] = kvp;
If u really don't want to use session use Database mind that this is a slow method...
i've had a few situations where I wanted to minimize/eliminate session storage (due to users being on a horrible wifi connection). to handle this situation I used an encrypted query string with only their ID in the string. in the base page I would decrypt the string and pull information I needed from the database. This information would be populated into objects that I defined and since the objects were in the base page I could access the information from any pages that inherited it.
I am working on a ASP.NET/C# Website.
I am reading data from a database, saving it in a Dictionary
Dictionary<string, decimal> Results
and then binding it to a ASP.NET chart
PieChart.Series["Series"].Points.DataBind(Results, "Key", "Value", string.Empty);
I want to change the Label of a Point when I click a button.
protected void Button_Click(object sender, EventArgs e)
{
PieChart.Series["Series"].Points[0].Label = "abc"
}
But the problem when I click the button, a PostBack happens and the data saved in The "Results" Dictionnary is lost as well as the Chart.
Is there a way to , not lose the data when a postback happens without having to read from the database all over again?
Thank you for any help.
Yes Make use of ViewState to preserve data between postback.
public Dictionary<string, decimal> Results
{
get { return ViewState["Results"]; }
set { ViewState["Results"] = value; }
}
Note:
Check for the Null value of viewstate otherwise it will throw an Exception or Error
Some responders have suggested storing the data in ViewState. While this is custom in ASP.NET you need to make sure that you absolutely understand the implications if you want to go down that route, as it can really hurt performance. To this end I would recommend reading TRULY understanding ViewState.
Usually, storing datasets retrieved from the database in ViewState really hurts performance. Without knowing the details of your situation I would hazard a guess that you are better off just loading the data from the database on every request. Essentially, you have the option of a) serializing the data and sending the data to the client (who could be on a slow connection) or b) retrieving the data from the database, which is optimized for data retrieval and clever caching.
You can put the data in ViewState or Session to then be able to pull it out "on the other side".
A better solution than using the ViewState and passing this data back and forth from the client may be to create a client id that each is passed back and forth and keep a cache of this data on the server side, keyed by this id. That way you do not need to send this data from client to server each time, only the other way around.
This still sounds wrong to me, but it is a better solution than some of the other answers that would involve so much overhead due to the data back-and-forth.
In fact since you're only changing display information and, from your question, I don't believe you're actually processing this data in any way, it seems to me that this is really a job for some sort of javascript alongside of your ASP.NET page. I have never done this, but some basic googling did turn up some articles about this.
Instead of using ViewState, why dont you move your code for assigning value to the dictionary to a function and then call that function from page_load on every postback. This way you will not lose data from your chart.
I often use asp:HiddenField to store small amounts of non-secure data on the client side.
In the Page_Load() function you can set the hidden field value, and then you can read it back in functions that get called later.
Do not use this for secure data as the client user can do a View Source to see the data in the page if they wish.
I looking ways to redirect the page from the server side code and pass couple variables into the new page ( i can pass the same data through query string or session but i dont want to do that) what is the best way to do this?
Thanks in advance!!!
You have a few options:
QueryStrings (you don't want to use this)
ASP.Net Session (you don't want to use this)
Multiple hidden forms with variables (http://msdn.microsoft.com/en-au/magazine/cc164151.aspx)
Using the Button PostBackUrl attribute.
Expose page properties/variables as public - dirty.
ASP.Net Cache
NCache (http://www.alachisoft.com/ncache/)
Server.TransferRequest (http://msdn.microsoft.com/en-us/library/aa344901.aspx)
. Can handle any additional headers you place inside the request (post data).
Heading
I want to pass some variables from one page to another but I don't want them to appear in the URL. When I use a form to post the an ActionResult it works fine. However, when I do this
return RedirectToAction("Trackers",new{page = 1, orderby=desc});
I get the URL:
http://examplesite.com/Trackers?page=1&orderby=desc
Is there any way I can get the following URL instead but still pass the page and orderby variables "behind the scenes"?
http://examplesite.com/Trackers
Thanks
TheLorax
I'm not sure it's a good idea to do what you're suggesting, however, you might be able to use the TempData (same place as the ViewData) to save your information before the redirect. I believe that after your next request, anything in the temp data is cleared out.
You could pass a Model object containing the page number and the orderby instead of an anonymous object, and have your view inherit the model class of the object you are passing. Your model object with the page number and orderby then becomes available to the view, without employing a query string
This is not, however, the preferred method of doing this. Your model objects should be reserved for their intended purpose, which is passing actual data to your view.
When you use RedirectToAction method the browser will recieve an http redirect response from the server and then will do a GET request to the new URL.
I think there is no way to make the browser make a POST request when redirecting.
Also it is better to pass these variables through the query string in order for the user to be able to bookmark the page.