Clear Fields Button (redirect not working)? - c#

I have a webform with a "clear all fields" button, but it's not working properly. When I click the button on the rendered page, it directs me to another page on the local server, but not the one I have listed here.
protected void btn_Clear_Click(object sender, EventArgs e)
{
Response.Redirect("~/ContentRequest/PR_Event.aspx", true);
}
I'm not sure what I can do...

You can simply use Request.RawUrl to redirect back to current page.
Response.Redirect(Request.RawUrl, true)
However, if you want to just clear fields, you might want to use jQuery or javascript at client-side to avoid a round trip to server.

Related

ASP.Net C# - Redirect to a page

I've got a 'menu' within a Master page which includes a number of image buttons with click events.
Within each click event, I want to redirect the user to a specific (Child) page. However, if the user is already on the correct (Child) page, I don't want to refresh the page and lose the entered data.
In the example below, I want to redirect the user to browse.aspx however, if the user is already on browse.aspx, I don't want to refresh it.
Is there a better way to do this than the following?
protected void ibtnBrowse_Click(object sender, ImageClickEventArgs e)
{
if (!Request.Url.Segments[1].ToString().Equals("browse.aspx"))
{
Response.Redirect("~/browse.aspx");
}
}
How about disabling the Image button on the page?
e.g.
When you are on browse.aspx, in code behind browse.aspx.cs you can disable the button.
ImageButton iBtn = (ImageButton)Page.Master.FindControl("ibtnBrowse");
iBtn.Enabled = false;
By having server side click events the page will always "refresh" when clicking on these buttons. However, if you are simply looking to avoid doing an unnecessary redirect in your code you can use:
HttpContext.Current.Request.Url.AbsoluteUri
or
HttpContext.Current.Request.Url.AbsolutePath
If you decide to use javascript to switch between pages you can use location:
location.replace("http://www.w3schools.com");
location.assign("http://www.w3schools.com");

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;
}

viewstate disappears on response.redirect

On my asp.net c# page I have two text boxes(start and end dates) with ajax CalendarExtenders. The user selects a start date and then an end date. On selecting the end date, I bind my grid as shown below;
protected void calEndDate_TextChanged(object sender, EventArgs e)
{
BindGrid();
}
In the grid I have a command button with the following code
protected void gvAllRoomStatus_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Manage")
{
GridViewRow row = gvAllRoomStatus.Rows[Convert.ToInt16(e.CommandArgument)];
int BookingID = Convert.ToInt32(row.Cells[1].Text);
DataClassesDataContext context = new DataClassesDataContext();
Session["BookingID"] = BookingID;
Response.Redirect("CheckIn.aspx");
}
}
When the user goes to that page and clicks the back button all the selected dates and the gridview data disappears. Any ideas why the viewstate is disappearing?
ViewState belongs to the current Page.
Have a look: http://www.codeproject.com/Articles/37753/Access-ViewState-Across-Pages
Yes, we can access the viewstate variables across pages. This is only
possible if Cross Page Posting or Server.transfer is used to redirect
the user to other page. If Response.redirect is used, then ViewState
cannot be accessed across pages.
So you could use Server.Transfer instead or use the Session.
Viewstate to look at it in a very simplified way is to see it as a carbon copy or cache of the last state of the page you are currently on. Therefore doing a redirect to any page, even the same page itself, is essentially a fresh start. The viewstate no longer applies as for all intent and purpose, you are on a new page.
As Tim suggests in his post, either store the required data as a session variable or use a server.transfer.
Take a look here for a nice overview of viewstate:
http://www.codeproject.com/Articles/37753/Access-ViewState-Across-Pages
In my opinion the issue you have is because you make auto post backs with calEndDate_TextChanged using Ajax.
After your submit, when you press the back button the browser can not remember neither can save what you have change with all that auto post data with Ajax calls, and you lose them.
For me remove the Text Change auto post back, remove the Ajax because you do not needed and make a regular full post back when the user submit their data.
Then when you make back with the browser, browser load the previous state and most of the browsers remember what and all the input of the user. Also on that back the viewstate is the same as previous because did not have change from the Ajax.

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.

Submit value of a TextBox without reloading page

I'm working on an ASP.NET based TicTacToe game. The problem I have with it is that:
The game is played between two users. When the first one types 'x' in the TextBox I want the 'x' to be shown on the second player's computer without reloading the page.
I don't know if some code will help but here is the way I did it without reloading(the user must reload the page manually... dumb):
protected void TopLeft_TextChanged(object sender, EventArgs e)
{
Application.Lock();
GameBoard gameBoard = new GameBoard();
gameBoard.board[0, 0] = char.Parse(this.TopLeft.Text);
Application["TopLeft"] = gameBoard.board[0, 0];
Application.UnLock();
}
And then, on page pre render:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Application.Lock();
if(Application["TopLeft"] != "0")
{
this.TopLeft.Text = Application["TopLeft"].ToString();
}
...
And so on...
I'd be very thankfull to anyone who can help!
You will need to use AJAX to do this. I recommend looking at some of the AJAX capabilities that jQuery offers but you can also look at the AJAX Toolkit from Microsoft.
Here is documentation for AJAX in jQuery:
http://api.jquery.com/jQuery.ajax/
I feel this is much "lighter" than what Microsoft offers out of the box. You can find out more about the Microsoft AJAX toolkit here:
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/
You are asking about Partial Page Update.
First, you need to place the client TextBox or what ever other controls that you need to reload inside an UpdatePanel.
Then, you need to call the UpdatePanel.Update to update those controls whenever you need.
Check out AJAX. This will require client scripting to submit and detect updates without submitting or updating the entire page.
Note, however, that this is a fairly advanced topic and will not simply be a little snippet of code you can add. I would recommend a good AJAX/JavaScript/jQuery book.

Categories