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;
}
Related
I've just developed a new GridView MultiFilter control ( CompositeControl ) that works like the image below:
I use ViewState for my control's properties so it keeps all values after postback. I want to save my control properties to a Session before redirect so I can load properties back to my control when my page loads again.
Does anyone have any suggestions about how this can be accomplished?
You have to do 2 things in this list page:
(1)Page load
(2)Search click
And 1 thing in detail (redirected) page:
(3)Pass some query string while back to list page
(1)while page load decide to load normal or searched (back from detail page) data
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["back"] != null)
bindDataFirst();
// same data load logic as present
else
bindDataForBack();
// you come back from detail page hence bind search & grid data
}
private void bindDataForBack()
{
strName = Session["SearchName"] == null ? "" : Session["SearchName"].ToString();
// check session search conditions & data and bind
//also bind grid by respective search parameters & search options (top side)
(2)search click store search data into session
public void btnSearch_Click(object sender, System.EventArgs e)
{
Session["SearchName"] = strName;// store search data into session variables
//bind grid by respective search parameters
(3)in redirected (detail) page set back button like:
public void btnBack_Click(object sender, System.EventArgs e)
{
Response.Redirect("ListPage.aspx?back=1");
I hope this may helps you. I suggest to implement this with one textbox & grid and then try with your present scenario.
Please mark this answer useful if this solve your problem.
You can assign data to Session the pretty much the same way you assign to ViewState.
It's a key value dictionary just like ViewState.
Ex:
Session["someKey"] = "An arbitrary value";
If you can redirect to another page using form submit then You can post your form to the required page using action attribute of your page. This way values of all controls will be available in the Request["KEY NAME HERE"]
<form action="anotherpage.aspx" id="frmData">
<!-- YOUR CONTROLS HERE -->
<input type="submit" value="Submit" />
</fomr>
you can also submit your form using JS
$("#frmData").submit();
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.
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 am using a stringbuilder object to concatenate some javascript together and then register it. My question is, is there a way using client side Javascript to clear that javascript when a postback occurs. I have found out that when registering arrays using the scriptmanager you have to empty down any dynamic arrays and basically I want to do the same thing with the javascript code that I am dynamically adding to the page? The javascript has a key ("randomlists") so we must be able to reference it!
ScriptManager.RegisterStartupScript(UpdatePanel_MyPublications, typeof(UpdatePanel), "randomlists", sb_javascript.ToString(), true);
Let me explain a little more. The javascript that I am registering contains some variables, in this case some other arrays which are dynamically populated. Currently the error is that these arrays are appended to rather than reinstantiated and re-populated (lots of 're' going on) and if I could simply clear and re-register the code all the problems would magically stop...
Thanks.
On post back re-registering the script using the same name, but different content (ie. different string builder) should work.
On the aspx page, add a button called "Button1", and use the following aspx.cs as a proof of concept. Clicking the button after page load alerts "1", and then posts back. Click the button again and you'll get an alert "2", showing that on post back, I was able to redefine the variable.
aspx.cs page:
protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "alert(test);");
var sb_javascript = new StringBuilder();
var sb_javascriptPostback = new StringBuilder();
sb_javascript.Append("var test='1';");
sb_javascriptPostback.Append("var test='2';");
if ((Page.IsPostBack))
{
ScriptManager.RegisterStartupScript(UpdatePanel_MyPublications, typeof(UpdatePanel), "randomlists", sb_javascriptPostback.ToString(),
true);
}
else
{
ScriptManager.RegisterStartupScript(UpdatePanel_MyPublications, typeof (UpdatePanel), "randomlists",
sb_javascript.ToString(),
true);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("PostBack");
}
}
If this doesn't work, please post your code so we can take a look at your specific scenario.
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;
}
}