I have a website that returns search results from Twitter, written in C# ASP.NET. The search works very well.
When the user sees the results I want them to also have a 'Next Page' type hyperlink that will perform the search from the last previous result onwards (using Twitter's next_page data).
How can I preserve properties so that when a link is clicked it will run the search again with different parameters for the next results? I cannot use Form as there is one Form on the page already and MS limits to one Form per page (for runat="server").
Please help, this is driving me nuts.
PS I thought of including code, but not sure which bit to include, as it has more to do with how ASP.NET works as much as how my own coding is.
There's a hundred different ways to solve this problem. The ASP.NET infrastructure includes something called ViewState, which allows the page and its controls to persist arbitrary data and objects across page views.
There is a single <form>, but you can have an unlimited number of links and buttons which submit the form in different ways - each one can trigger its own method when the page posts back.
A simple way to leverage this in your case is to store the page parameter on the "next page" link. This is a very simple example that assumes you only need to know the page number, but it gets the point across:
<asp:LinkButton runat="server" ID="next_page" Text="Next Page" OnClick="NextPage_Click" />
...
void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LoadData(0);
}
}
void LoadData(int page)
{
//pull page of data from twitter & show on page
next_page.CommandArgument = (page+1).ToString();
}
void NextPage_Click(object sender, EventArgs e)
{
int page = int.Parse(((LinkButton)sender).CommandArgument);
LoadData(page);
}
In this example we set the CommandArgument property of the LinkButton to the page index we want. The LinkButton triggers the NextPage_Click method to be called, and ASP.NET's ViewState infrastructure allows the CommandArgument value is persisted.
There are two easy ways to do it:
Include a parameter in the url that is the href of the Next hyperlink, the url could look something like this:
http://mysite.com/myWebPage.aspx?currentPage=1
you can then access that parameter from the querystring in your code behind.
You can also save it to Session:
Session["currentPage"] = 1;
then upon postback you can check for it:
int currentPage = 0;
if (Session["currentPage"] != null)
int.TryParse(Session["currentPage"].ToString(), out currentPage);
with the Session it will automatically expire depending on your IIS setup, whereas using the querystring option it doesn't expire (although the user can mess with it so you will have to validate it before using it).
Have your properties save their values to the viewstate.
Something like this:
public int PageNumber
{
get
{
object value == this.ViewState["PageNumber"];
if(value != null)
{
return (int)value;
}
else
{
return 1;
}
}
set
{
this.ViewState["PageNumber"] = value;
}
}
Related
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;
}
I use RadTreeView in my master page :
but i face the following problem :
I lose my selections when i click on the nodes of the tree view .and every time i click on the node it makes (Response.Redirect("...")) which make the master entering the (!Page.IsPostback)
every time and bind the tree view again so i lose my selections .
How to fix this problem .
My .aspx :
<telerik:RadTreeView ID="rtv_cm" runat="server" OnNodeExpand="rtv_cm_NodeExpand"
OnNodeClick="rtv_cm_NodeClick" Skin="WebBlue">
</telerik:RadTreeView>
My .cs :
protected void Page_Load(object sender, EventArgs e)
{
if (Session["emp_num"] != null && !string.IsNullOrEmpty(Session["emp_num"].ToString()))
{
if (!Page.IsPostBack)
{
LoadRootNodes(rtv_cm, TreeNodeExpandMode.ServerSide);
}
}
else
{
Response.Redirect("Frm_login.aspx");
}
}
protected void rtv_cm_NodeClick(object sender, RadTreeNodeEventArgs e)
{
dt_childs = (DataTable)Session["dt_childs"];
IEnumerable<DataRow> result = from myRow in dt_childs.AsEnumerable()
where myRow.Field<string>("task_id").TrimEnd() == e.Node.Value.TrimEnd()
select myRow;
if (result != null)
{
if (!string.IsNullOrEmpty(result.ElementAtOrDefault(0).Field<string>("page_name")))
{
Response.Redirect(result.ElementAtOrDefault(0).Field<string>("page_name").TrimEnd(), false);
}
}
}
How I get The menu :
private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
{
dt = Menu_db.GetMenu(Session["emp_num"].ToString(), CMSession.Current.db_code);
IEnumerable<DataRow> result = from myRow in dt.AsEnumerable()
where myRow.Field<string>("task_id").TrimEnd() == "0"
select myRow;
if (result != null && result.Count()>0)
{
DataTable dt_roots = result.CopyToDataTable<DataRow>();
Session["dt"] = dt;
Session["dt_roots"] = dt_roots;
foreach (DataRow row in dt_roots.Rows)
{
RadTreeNode node = new RadTreeNode();
node.Text = row["task_name"].ToString().TrimEnd();
node.Value = row["task_id"].ToString().TrimEnd();
if (Convert.ToInt32(row["count"]) > 0)
{
node.ExpandMode = expandMode;
}
treeView.Nodes.Add(node);
}
}
}
HTTP is what's known as a "stateless protocol". Basically, it's as if the server has a severe case of Alzheimer's disease (no disrespect intended). The server answers your query, but then forgets you were ever there.
When HTTP was used primarily for fetching static documents, this posed no problem, but web applications require some kind of state. There are many ways this is accomplished, but ASP.NET typically makes heavy use of the "ViewState". The view state is simply an <input type="hidden"> tag which contains base-64 formatted byte code. (If you view the source of your rendered HTML page in the browser, you see it - named "__VIEWSTATE"). When the user resubmits the form (by clicking a button, etc), the viewstate data is sent back to server. Basically, it's like reminding the server about what it told you last time. This is how TextBoxes and GridViews are able to seemingly maintain their state between postbacks - they store their state in the "viewstate".
The problem with the viewstate, though, is that it can be easily lost. You must submit the form or the viewstate will not be persisted. If you use an anchor tag or Request.Redirect, you are side-stepping the form and hitting a web page all on your own and, in the process, you are not passing any of the viewstate along.
There is often no way to avoid this, so there are other ways to store your application's state. But, unlike the viewstate, this is something you must do manually for each and every value that you want to persist. You can store the data in a cookie, you can store the data on the server (using Server["key"] or Session["key"]) or you can persist the data in something more concrete, like a database or a text file. All of this must be done manually, however, and then you must reload the data when you reload the page.
In your case, you may want to guess which item was selected based on the current page (since the treeview items and pages seem to be linked) and set the selected item explicitly. If that's not feasible, you could try storing the selected item in the Session, using something like Session["Master_SelectedTreeViewItem"] = treeViewItem.Id;. Then, in Page_Load, get this value (careful, it may be null) and then set the corresponding treeview item as selected.
I would post more code examples, but you haven't provided the code where you are loading the treeview, etc, so it would be difficult to do.
Further Info
ASP.NET State Management Overview (MSDN)
The only way i can think of is storing the selection in session and on (!Page.IsPostBack), look for that Session key and update your selection after the binding.
The problem you face is because essentially, trough your Redirect, you go to a different html page, so ViewState is out the window, nevermind that it is the same address.
If you are binding the tree with urls that link to other page, append the selection using query string, then load the tree selection from the query string from the other page.
If you think that the second page you are redirecting to has same UI as the first, dont redirect instead load it on the same page with the new content for the selection, this will allow you to maintain the state in asp.net if the control supports view state.
I already had this problem, exactly same problem, needs to redirect on TreeViewItemClick.
I solve it storing the last selected item is session, and selection it again on page load.
Somethink like this
protected void rtv_cm_NodeClick(object sender, RadTreeNodeEventArgs e)
{
Session["LastClickedNode"] = e.Node.Value;
...
}
and in your Load method, verifi ir the node needs to be selected, like this.
private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
{
//... your code
if (Session["LastClickedNode"] !=null)
{
if (row["task_id"].ToString().TrimEnd() == Session["LastClickedNode"].ToString())
{
node.Selected = true;
}
}
}
i think this will solve your problem.
I'm trying to create a C# webpage that uses a GET method so I can bookmark form data, email it to other people, etc., but changing the form method to GET results in the querystring containing the VIEWSTATE. I can take out the runat=server tags (as mentioned here) but then I don't know how to set the values of my form fields. I don't mind manually persisting them, but I can't figure out how.
So, how do I either utilize the viewstate but keep it out of my querystring, or access the form controls without runat=server?
If you are using, for example, a search/filter form on your webpage that you want via GET, here's a pattern I've used multiple times in my own code. When the user submits a button to process what they've entered in the form, I let it hit my code-behind as a POST as normal. Then I manually construct a GET request and redirect to it:
protected void BtnSubmit_Click(object sender, EventArgs e)
{
var query =
"?filter1=" + TxtFilter1.Text +
"&filter2=" + TxtFilter2.Text +
"&filter3=" + TxtFilter3.Text; // etc.
Response.Redirect(query);
}
Then on load of the page, I process the entries in the query string to pre-populate the relevant form controls on the page:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TxtFilter1.Text = Request.QueryString["filter1"] ?? "";
TxtFilter2.Text = Request.QueryString["filter2"] ?? "";
TxtFilter3.Text = Request.QueryString["filter3"] ?? "";
// etc.
}
}
This allows me to have control over what actually goes into the QueryString without messing with the built-in form POST managed by ASP.Net (including View State). Also, when a user has filled out the form and pressed the button, they have a linkable url, and even though it was a POST, if they refresh the page they don't get that annoying message about re-sending data to the server.
I have a simple ListView paged by a DataPager giving a list of products. When we click on the product we open the product details page. On the details page we want to "return to product list" but of course we want it to remember what page the datapager was at.
Surely this was conceived as a natural design requirement - what is the simple out-of-the-box way to do this?
If we use the QueryStringField property of DataPager we get the page number in the URL so I hoped I could use the referrer URL in the back link, but I have found the Request.UrlReferrer to be unreliable (when I use F5 to debug the app in Internet Explorer for example, Request.UrlReferrer is always null).
I have found some success with storing the page number in a session variable:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["PagerIndex"] != null)
{
DataPager1.SetPageProperties((int)Session["PagerIndex"],
DataPager1.MaximumRows, false);
}
}
}
protected void DataPager1_PreRender(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Session["PagerIndex"] = DataPager1.StartRowIndex;
}
}
This method has some drawbacks:
Only works if QueryStringField is blank (so IsPostBack can be detected)
Session/cookie variable required for each pager
Question of how to reset the session/cookie variables arises
What is the "proper" way to do this?
Thanks
You can try my solution
Set the QueryStringField property of your datapager to a querystringfield say page.
<asp:DataPager ID="DataPager2" runat="server" PagedControlID="ListView1"
QueryStringField="page">
....
Note: I placed the DataPager outside of the ListView.
Put linkButton in your listview that will redirect you to detailspage and in its click event save the current page number in a Session
int integ;
decimal fract;
integ = DataPager2.StartRowIndex / DataPager2.PageSize;
fract = (DataPager2.StartRowIndex / DataPager2.PageSize) - integ;
if (fract > 0)
page = integ;
else if (integ > 0) page = integ - 1;
Session["page"]=page;
On the details page retrieve the page and pass back as a querystring to ListViewpage.
Automatically the datapager will take you to that page number, if not specified to page number 1.
Good Luck !
If you don't have any filters you can simply recalculate the page on which the product was.
Another option will be to encode the page (together with possible filter values for the list) in the URL of the product detail page and use them to generate an URL for the list that will be essentially the same as the one of the original list. Even something like the ReturnUrl approach used for login. Sharepoint does similar thing with its lists but I feel the URL can get too messy (and I am not a person that falls for the whole "clean URL" bullshit when I say it is messy it really is)
One more option would be to pass the product ID to the list page via the URL. The list page can then calculate the appropriate page. This will remove the noise from the URLs
I have a text string value that I'd like to persist from one web page to another without using query strings or the session/view states. I've been trying to get the ASP http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx">HiddenField control to pass information from one web form to a different form.
All the hiddenfield control examples that I've seen is to preserve round trips from the client to the server for the same form.
Is there way for a form to access the ASP controls (and their values) from the previously-rendered form? Or is the initial form simply disposed of in memory by the time the second form executes it's OnLoad method?
Quick answer is no. As others have noted, you can use Server.Transfer and then you can - however this is to be used with caution. It is a "server side redirect" eg.
Your user is on http://mysite.com/Page1.aspx they click a button and you perform a Server.Transfer("Page2.aspx"). Page2.aspx will be rendered in their browser, but the URL will still be Page1.aspx, this can cause confusion and mess up back/forward navigation.
Personally I would only use Server.Transfer as a last resort - in the world of the web, sharing data across pages generally means you need to use a storage mechanism; Cookie, QueryString, Session, Database - take your pick.
You can't get the previous page fields with Response.Redirect.
You can with cross page posting :
if (Page.PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null)
{
Label1.Text = SourceTextBox.Text;
}
}
If both pages live in the same application you can use Server.Transfer:
firstpage.aspx:
protected void Page_Load(object sender, EventArgs e)
{
Server.Transfer("~/secondpage.aspx");
}
secondpage.aspx:
protected void Page_Load(object sender, EventArgs e)
{
Page previousPage = (Page) HttpContext.Current.PreviousHandler;
Label previousPageControl = (Label) previousPage.FindControl("theLabel");
label.Text =previousPageControl.Text;
}
A somewhat better solution would be implementing an interface on your first page where you expose properties for the values needed by the second page.
I would presume that the Response.Redirect() sends a Location: HTTP header to do a redirect.
As HTTP is stateless, I'd also presume that these variables are inaccessible.
There are however, solutions.
Print a form with hidden fields, and use javascript to submit it
Redirect in the code internally (load up the thing it needs to get to manually)
Store the data in some temporary database table somewhere, and pass along a unique ID
However, from my experience, I can't understand why you might need to do this (other than re-submitting a form after a user authentication - which hopefully you should be able to use method 2 for
Remember, a Response.Redirect instructs the browser to issue another request to the server. So far as the server is concerned, this next request is indistinguishable from any other incoming request. It's certainly not connected to a previous form in any way.
Could you explain your aversion to storage in the session, so we can propose some viable alternatives?