Read hidden field values from another page - asp.net

I am developing a website using ASP.net.
In my home page I have 14 tree view controls. They were placed under Jquery tabs. So when a user click a tab it only appears one treeview. So in tree node changed event currently what I am doing is passing the values from query string to another page. this query string doesnt have any sensitive data. I have to pass what is the selected Tab Id and selected tree node Id.
Response.Redirect(String.Format("~/Display.aspx?tab=1&type={0}",treeview1.SelectedValue), true);
But you know URL is ugly. I dont want to use Friendly URL thing now.
So what I did was I put 2 hidden fields values in my home page and I set them when I click the tree node.
So then from the source page I used this code to acess this.
if(Page.PreviousPage!=null)
{
string tab= Page.PreviousPage.FindControl("hftab").UniqueID;
string Type= Page.PreviousPage.FindControl("hfType").UniqueID;
}
But Page.PreviousPage always return NULL. So how to solve this probelem without using sessions and query string. I just want to pass two non sensitive data behind the scenes.
IF this is about Cross page postback thing How to do this cross page post back in node change event? Is it ok to do a Cross page post back for to get only 2 values that I needed?

The method you are trying is only possible with "Cross page posting" mechanism. When you set PostbackUrl property of a server control like button, and set the PreviousPage property of Page directive on destination page; then only you will get Page.PreviousPage values.
Here are some reference:
1. http://www.codeproject.com/Tips/604553/Postback-and-Cross-Page-Posting-in-ASP-NET
2. http://www.aspdotnet-suresh.com/2010/05/cross-page-post-backing-in-net.html

Related

Passing values that aren't related to inputs using MVC?

I have page where users can select from one or more images. When they are done I would like them to navigate to the next page and what is displayed would be based on the selection from the previous page.
"Selecting" just means that they click the image and it has a CSS class added to it. When they click the link to navigate to the next page I'd like to collect the images that have been selected and pass that information along using either TempData or Session.
In most of the examples I have seen either inputs or the query string is used to pass information from the View to the Controller. How can I pass which elements have a particular class to my controller when a link is clicked?
If you're using a link click, I'd probably just append the selected images to the query string. I'm assuming you don't mind exposing this query string to end users.
I'm sure that's probably not the answer you were looking for. But as you stated I think you're only to legitimate options are passing the values through the query string or using hidden inputs and posting the page to your action by intercepting the link click event.
You said you do not want to post back to servers. You cannot access TempData or Session without posting back to server, so they are out of scope.
You only have client side option, so you want to collect a user's selected items in array.
Once the user clicks to Next Page, you create a query string like this ?ids=1-2-3-4 and retrieve those value at next page.
Other thoughts: Long URL likes this is a bit ugly, and URL has maximum length limit depending on browser. If I'm you, I'll post back to server to collect the selected values. Then use TempData (or some persistent storage).

Usercontrol not retaining value

I have a usercontrol with a dropdown, a textbox and a search button in the master page. I select a value in the dropdown, fill in text in the textbox and click on the button. On button click it redirects to another page but all the values are reset. How can I get the value selected in the dropdown and the text in the redirected page?
You need to post the values to the other page. You can for example pass the values in the url
redirectedUlr.aspx?value1=1&value2=2 etc...
Or you can store the value in the Session
Session["passedvalues"] = YourValues
Their is several way to handle this
You can pass information between pages in various ways, some of which depend on how the redirection occurs. The following options are available even if the source page is in a different ASP.NET Web application from the target page, or if the source page is not an ASP.NET Web page:
Use a query string.
Get HTTP POST information from the source page.
The following options are available only when the source and target pages are in the same ASP.NET Web application.
Use session state.
Create public properties in the source page and access the property
values in the target page.
Get control information in the target page from controls in the
source page.
For more information and examples, you can read the following article :
http://msdn.microsoft.com/en-us/library/6c3yckfw(v=vs.100).aspx

Setting visible property on .NET controls

.NET newbie here.
I have a page with a number of sections to it. I want to create another page to have hyperlinks to this page, and hyperlinks to the sections within it.
I want all the sections to be visible if the user has clicked on the main page link, but only the section that the user has asked for if (s)he has clicked on the section hyperlinks.
How do i set the visible property of the non-requested controls (in asp:Panel's) from the link on the previous page, so they don't appear on the page when a different section is requested?
many tia
mcalex
You have the following options
Wrap each section controls inside an asp.net panel server control
When you click the hyperlink on the main page, pass the panel to enable via querystring or session as a parameter like on your linkbuton
Response.Redirect("~/myPage.aspx?secId=info")
Then on your redirected page set all panels Visibility=false inside pageLoad check for querstring value & set the panel's visible property to true or false
if(!Page.IsPostBack)
{
string panelToEnable= Request.QueryString["secId"];
switch(panelToEnable)
{
case("info"):
panelInfo.Visible=true;
break;
case("details");
panelDetails.Visible=true;
break;
default:
panelMaster.Visible=true;
break;
}
}
Alternatively you can place the section links inside the redirected page & further simplify things (remove querystring etc.)
You could inspect the Request.ServerVariables["HTTP_REFERER"]
However, if the user refreshes the page, this will change.
You have 2 ways to do this issue:
Use query string: pass your different parameter values on the query string. In the destination page/control, check the query string and make the section visible/invisible as your requirement
Use session data: use a specific key for storing the mode. When you click on the link button of other pages, use the call back function (in CSharp) to set the session data and redirect to the destination page. This page will check the data that stored in session state and make the section visible/invisible.
Pros & Cons:
The 1st solution can be used for pure HTML link but the parameter is shown in query string so user can change it to display the other sections.
The 2nd solution can not be used for pure HTML link but it prevents user changing the values in session state. So it is more security.

Sending data between asp.net page & Popup page?

I have a form where user selects 'fruit's from a popup form. I am using javascript to open the popup and query string to pass the text control id & hidden field id to the popup window.
How can I securely passed these parameters ? I am thinking about using Session but that would require a postback.
Edit
You should try passing parameter using query string.. but try using Encrypted Querystring values to secure your data.. http://www.codeproject.com/KB/aspnet/urlquerystring-encryption.aspx
you can use any of following methods.
Use a query string.
Get HTTP POST information from the source page.
Use session state.
Create public properties in the source page and access the property
values in the target page.
Get control information in the target page from controls in the
source page.
for more information,
http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx

posting an action from a partial view within a jquery dialog

I have an index page with a jquery tab loaded. Within one of the tabs I open a partial view company.ascx. Within that I have 2 RenderActions' One loads the company header and the other loads the branch information.
<% Html.RenderAction("Compheader", "Home"); %>
<br />
<br />
<% Html.RenderAction("BranchList", "Branch", new { Id = Request.QueryString[0], pdate = Request.QueryString[1] }); %>
Within BranchList I display a table of branches each of which has a delete button associated to it. There is also an add button on the branch list. Both these buttons open a jquery dialog that open partial views (acsx) within it. The dialogs have a submit post within them.
When the user clicks on submit on the insert/add or delete view I want to be able to refresh the BranchList action, which will get the new branchlist and display it.
Right now on post within the delete or insert I response redirect to the index page which refreshes the whole page. Can somebody tell me how I can accomplish this using Html.BeginForm and ajax posts in a clean way instead of the response redirect.
You are accessing QueryString directly within your view and that means that you are not using any of the goodness of ASP.NET MVC framework. You should get these values in action method (using the matching parameter names as the QueryString variables in the action method's constructor) and then pass these values from action method to the view (using a view model or ViewData) so that you don't have to access QueryString directly inside the view.
Now coming to your question, I think you are doing it right. If you are getting the right behavior from your application, then you should not change the post-redirect behavior of the application.
You are posting the data from the partial views and then doing a redirect. This is a valid pattern, also known as GPG (Get, Post, Get) pattern. This is advantageous compared to simply sending the user to their "Posted" page as it avoids from letting the user post the same data twice in case they refresh the page.
Hope it helps.

Resources