Maintained state of a web page - c#

In my web app I has a GridView and some other control. User is allow to sort and filter the grid view.They also allow to click the link and go to other page.Then they also can go to other more pages. But when they cum back to the first gridview page. The grid is same as what they left the page. For example paging ,sorting and other .
I found a solution about this . But I not really understand.
http://www.codeproject.com/Articles/7655/Persisting-the-state-of-a-web-page
Here is my coding WebForm1.aspx
protected void Button2_Click(object sender, EventArgs e)
{
PersistentStatePage abc = new PersistentStatePage();
abc.RedirectSavingPageState("WebForm2.aspx");
}
WebForm2.aspx
protected void Button1_Click(object sender, EventArgs e)
{
PersistentStatePage.RedirectToSavedPage("WebForm1.aspx", true);
}
Can anyone guild me some example?

That codeproject article is not really meant for this sort of scenario. It is about saving viewstate to a secure, on server location rather than sending it all to the user in a hidden input field which leads to a lot of bloat.
The best way to go about this would be session.
Create a custom object to store the info you want to persist and put it in session.
Check out the following for an article about using session http://asp.net-tutorials.com/state/sessions/
Then you don't have to worry about passing the details around all the other pages.

Related

C# web forms - How to pass data from master page to master page when redirect is on web form?

When on master page I have dropDownList with event. When event Raise I want to load/redirect webForm.aspx. In this webForm.aspx I want to load data based on dropDownList selected item. At the same time I want to load again dropDownList and select Item on which clicked was.
I created some solution, where I was using Response.Redirect("webform.aspx?courseID=4") on masterPage.
Is this good approach to this problem? I think there must be better, but I cant see it.
Thanks for answers.
One way is your solution: to use querystring.
The second way is: on dropdownlist's value changing you changing value of some session value (e.g. *Session["course_id"]), then Response.Redirect("webform.aspx") and restore by Session["course_id"] value.
protected void ddlCourses_SelectedIndexChanged(object sender, EventArgs e){
Session["course_id"] = ddlCourses.SelectedValue;
Response.Redirect("webform.aspx");
}
ABOUT YOUR SECOND QUESTION (HOW TO TAKE ROUTE VALUE FROM MASTER PAGE). I will give just brief explanation and a little example.
Create in your master page some method:
protected void IWantToWarkWithRouteValue(){
//here you will do something
//and to take value from routing, just use Session["myValue"] (I will explain this value later)
}
You should call this method in Page_Load, not in Pre_Init or Init
Then in your Course.aspx (caz' you using routing here) create the next method:
protected void SetCourseSessionFromTheRoute(){
if (Page.RouteData.Values["courseID"] != null)
Session["myValue"] = Page.RouteData.Values["courseID"].ToString();
}
And call this method in Page_PreInit:
protected void Page_PreInit(object sender, EventArgs e){
SetCourseSessionFromTheRoute();
}
Thanksgiving to ASP.NET page's life cycle, you may do anything you need.
You may read about page's lifecycle here
As I said, it is just an example. You may find a better solution.
Hope it helps.

Enter data in web form, output to a new page

I am using a simple <form> to collect data from a user. The user clicks a simple button to enter the data: <input type="submit" name="cmd" value="OK">. Currently the application does a simple post back, displays the filled in form, and under the form, displays the results.
The users now want the results to go to another page. Basically they want to change a variable and compare the results in different tabs. My first proposal was to keep the post back and then add a hyperlink using target="_blank" to push the results to a different tab but they do not like the two-clicks: OK button then hyperlink.
Is it possible to send the results of a form input to another page?
I am programming in C# using ASP.NET.
you can do this by postbackurl property in c#. thus helps you to access your previous page controls and there output on next page. you can also do this by using hidden field and post or get method. both options are good and reliable.
reference
Seeing that you are using ASP.Net, I would recommend you utilise the power of the code-behind process. One option that you can do in addition to the above responses is use a QueryString in your URL as you do a re-direct if that is available to you as a requirement. Example 1. Use a ASP Button
protected void btnOriginalPage_Click(object sender, EventArgs e)
{
string url = "NextPageViewer.aspx?result=" + resultText;
//You can use JavaScript to perform the re-direct
string cmd = "window.open('" + url + "', '_blank', 'height=500,width=900);";
ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindow", cmd, true);
//or you can use this - which ever you choose
Response.Redirect(url);
}
///On the next page - the one you have been redirected to, perform the following
protected void Page_Load(object sender, EventArgs e)
{
//extract the query string and use it as you please
int result = Convert.ToInt32(Request.QueryString["resultText"]);
}
Example 2. Use a Session variable and store your dataset/result in an user defined object or DTO - this is just a class with a bunch of setters and getters
Perform the click event on the ASP button except this time you would do something like this:
protected void btnOriginalPage_Click(object sender, EventArgs e)
{
ObjectWithInfo.Result = result;
Session["friendlyName"] = ObjectWithInfo;
Response.Redirect("NextPageViewer.aspx");
}
//On the next page - the one you have been redirected to, perform the following
//The good thing with Session variables is that you can access the session almost anywhere in you application allowing your users to perform the comparison they require.
protected void Page_Load(object sender, EventArgs e)
{
//extract the data from the object to use
int result = ((ObjectWithInfo)(Session["friendlyName"])).Result;
}

How to write a page whitelist in an asp.net website

I have a website and as one of security options (prevent path traversal) I am planning to use a white list of pages which can be accessed. But my problem is I don't know how to do it. Can you share some articles or simple of code how to create a white list?
How many pages does the site have? If there are not too many pages and if pages are not added/removed frequently you can manually create a list of pages. If you are using master pages you can set this up in Page_Load method.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Dictionary<string,string> allowedUrls = LoadAllowedURLs();
if (!allowedUrls.ContainsKey(Request.Path))
{
Response.Redirect("Some_default_redirect_page.aspx");
}
}
}
Now, if you do have a lot of pages you would need more sophisticated solution that uses web.config and more…
If I understand the question correctly, you would like to allow certain users access to certain pages on your website - have you considered ASP.NET Security Trimming?

Activating a ASP Multiview control's view from other page without querystring/session

I was looking for an alternative that may be used to activate a multiview control of other page without passing a querystring/session variable.
Basically, my Home.aspx page has a link that takes us to a specific page say "NewPage.aspx". The NewPage.aspx page has a multiview control that has three child views.
I want to click on the link of the Home.aspx and go to NewPage.aspx with MultiView1.ActiveViewIndex=1. Please remember that I do not want to pass any querystring variable as that link already contains some encrypted data as querystring and adding another variable can cause the data to corrupt. Maintaining a session isn't a solution as well because the application is quite big.
Any inbuilt method that can activate that view? (I don't seem to be talking practical but any help is really appreciated)
If you are asking about how navigate to this page I would wire up a button event (I prefer to do so in OnInit). Something like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.btnlinkclick.Click += new EventHandler(btnlinkclick_Click);
}
void btnlinkclick_Click(object sender, EventArgs e)
{
this.MultiView1.ActiveViewIndex = 1;
}
This should work for you.

what is the difference loading Web user control loading on page?

i try to load some user control on my Default.aspx page selecting dropdown control. i searched some data from net i 've learn 2 methods there is first one :
http://blah.winsmarts.com/2006/05/20/loadcontrol-a-usercontrol--and-pass-in-constructor-parameters.aspx
Second one:
http://www.csharpnedir.com/articles/read/?filter=&author=&cat=aspx&id=689&title=Kullan%C4%B1c%C4%B1%20Web%20Kontrollerini%20Daha%20Etkin%20Kullanmak
Secand one is simple:
protected void Page_Init(object sender, EventArgs e)
{
AdresBilgisi kontrol1=(AdresBilgisi)LoadControl("AdresBilgisi.ascx");
AdresBilgisi kontrol2 = (AdresBilgisi)LoadControl("AdresBilgisi2.ascx");
kontrol1.Ilce = "İlçe giriniz...";
kontrol2.PostaKodu = "90000";
phKontroller.Controls.Add(kontrol1);
phKontroller.Controls.Add(kontrol2);
}
which one do you prefer to loadASCx control to page? And Why? please give some detail pros and cons of 2 method(first and second)
Your second approach is the preferred one. Because you have more control of the UI and you can see the UI layout of your desired location where you put it on the page.
Regarding the first approach; it can be used unless there is some special need. e.g. If you want to load a user control at runtime depending on some situation/condition.

Categories