how to go to previous screen - asp.net

I am using two kinds of buttons, on button takes the user to the next screen. Here I am using a session variable to take the user to the next screen:
protected void Buttondocumentdetails_Click(object sender, EventArgs e)
{
Session["Narration"] = TextBox2.Text;
Response.Redirect("~/TJFAQ0001.aspx", false);
}
This is working properly -- after clicking this button the user is sent to the next screen, which is is "TJFAQ0001.aspx". On this page I am using button to take the user to the previous screen:
protected void previous_Click(object sender, EventArgs e)
{
Session["Narration"] =null;
Response.Redirect("TJFAQOO1.aspx", false);
}
This is working, but all the data has been cleared. I want the same data when I click previous button. How can I do this?

First of all you can use Server.Transfer instead of Response.Redirect.
You can pass the page field data in a context variable and then pass it to the next page. When redirected to the previous page pass this back and do the necessary processing and then fill the data back to the controls.
Do not use Session variable in this context. You can add a context variable when using Server.Transfer.

You have to repopulate the data in TJFAQOO1.aspx page when it gets loaded on click of previous button as it is a new get request and postback kind of facility is not available.

Related

Page refreshes the form before data is sent

I have a form. On load it gets filled by Page_Load method with data. Now if user changes data and clicks the submit button the page refreshes reloads the original data and then onClick metod of the button gets called. Now it loads wrong (original) data from form not the data user put in.
How do avoid this ? Thanks.
In the Page_Load you need to wrap the data loading code in if (!IsPostBack){} like below:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Your code to load data
}
}

Display text only once per session in .net

I have a text to be displayed to user only once per session i.e. only the first time they try to open a page. If they try to open again they are not supposed to see that text.
If they close the browser and open it again they should be able to see the text again.
I wanted to use session, but I am not sure how to use it.
I tried to use cookie which worked, but what if cookie is disabled, I guess this wont work. so I decided to go with Sessions.
Define OnSessionStart Event In Global.asax...like this...
void Session_OnStart(object sender, EventArgs e)
{
Session["showmessage"]="Show";
}
On pageLoad or Event where you want to check... Check Session...if you want to show your text in label do like this...
protected void Page_Load(object sender, EventArgs e)
{
if(Session["showmessage"].ToString()=="Show")
{
Label1.Text="Message";//i Supposed you wana Show Message in Label.You Can Write your Code to Show Message wherever you wnat show.
Session["showmessage"]=Not Show";//To Display Message Only One Time.
}
Note-: It will Display Message Every Time Your Session Expired.

asp.net: How to get a button to affect the page contents

In Page_Load I populate an asp:Table with a grid of images. I have a button that when pressed I would like it to repopulate the page with different images.
However it appears that when the button is pressed Page_Load is called again, followed by the script specified by the button. I thought that I could simply set a variable in the button script which is checked during Page_Load, but this will not work.
What is the most asp.netish way to approach this? Should I be populating my table somewhere other than in Page_Load, or should my button be doing something different?
Your button event gets called after page load. As such, you should put your button code in there.
I'm not terribly sure why you'd try to stuff all of your event code into Page_Load, but it's best to keep it separated.
GOOD
protected void Page_Load(object sender, EventArgs e)
{
MethodThatDynamicallyCreatesControls();
}
protected void MyImage_Click(object sender, EventArgs e)
{
MyImage.Property = newValue;
MyImage2.Property = newValue2;
PopulateTables(newValues);
}
BAD
protected void PageLoad(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
//Check to see if "MyButton" is in the Request
// if it is, it's the button that was clicked
if (Request["MyButton"])
{
MyImage.Property = newValue;
MyImage2.Property = newValue;
PopulateTables(newValues);
}
}
}
As rick said, it's all a matter of understanding the postback.
page_load gets fired every time the page is refreshed. however in many cases, you only want certain things to happen on the first time a page is loaded. in your case, you want the default images to load. by putting all 'one time' events for a page load in a
if (!Page.IsPostback )
{
}
it will only fire on the first time the page is loaded. this is what you want to load your first set of images.
Then in your button click event (which triggers a postback), the code within the if statement will not execute again. and you can load your second set of images in your button's event handler.
That button you're using should call a method in your code behind,so you can know that the button is was clicked, ex:
protected void Important_ButtonClicked(Object sender, EventArgs e)
{
//do what I want to do
}
<asp:Button id="Button1"
Text="MakeChanges"
OnClick="Important_ButtonClicked"
runat="server"/>
Actually I understand what your problem is now, seems like you just have values being set in your page load with no condition check in you page load, so every time you have a postback it refreshes the page to original state, the reason for that is because everytime you trigger a refresh(postback) on the page, the pageload method is invoked, so you need to set original setting in your page load,but have them in the condition, as
if(!Page.Postback) which gets triggered the first time you visit this page. Which means this is where your defaults go and if(Page.Postback) is where your always true things should go. ex:
protected void Page_Load()
{
// this is where I want things to always happen whenever the page is loaded
//for example, no matter what happens I want a certain image in the background
if(!Page.Postback)
{
//set my values for the first and only time
}
else //hint Page.Postback
{
//you can play with the page here to make necessary changes
//but Button click method will still be invoke so thats where button click
//changes should be made
}
}
A PostBack happend when the page is reload. The first page load, Page.IsPostBack has value false. When an event happend, Page.IsPostBack has value true.
So doing the thing like this will definitely works
void Page_Load()
{
if (!Page.IsPostBack)
{
//do your first time binding data
}
How to: Create Event Handlers in ASP.NET Web Pages
EDIT:
Your state change events are not going to fire correctly if you re-bind controls(ie:DropDownList) data on every postback.
void Page_Load()
{
if (!IsPostBack)
{
//load your data
}
}

My code says a checkbox isn't checked when it is... ASP.NET

I have some checkboxes on a page. I get them using FindControl() in an UpdatePanel after pressing a button trigger, but the checked value is wrong. How can I get the correct checked value?
If you have any code that sets the values of the checkboxes on your page, make sure it isn't executing on postbacks, like this:
protected void Page_Load(object sender, EventArgs e) {
// Only set the checkboxes on GETs, not on POSTs
if (! this.IsPostBack) {
this.EmailMeUpdatesCheckbox.Value = false;
}
}
Actions triggered within UpdatePanels still go through the page lifecycle (which is why you have access to all your Page's state), so it may be clearing the user's selections before getting to the code in which you examine the checkbox values.

how can I reload an update panel onclient side using javascript?

I have a gridview button that I programmatically created and I want to load an update panel on the client side with the sent data. I have a hidden value field that gets its data on the click of the gridview button and the dropdownlist in my updatepanel depends on that value.
while calling __doPostBack directly will work, it's not a perfect solution because the name of that function is strictly speaking an implementation detail of the .Net framework.
A better solution is to use ClientScriptManager.GetPostBackEventReference, which gives you a more resilient interface to the same functionality. Do note that GetPostBackEventReference and GetCallBackEventReference are not the same thing - the former causes a page reload (partial or full, depending on how your UpdatePanels are set up), while the latter doesn't.
The easiest way to do this is to call __doPostBack from client side.
On client side button1_onclick method, calls:
__doPostBack('<%=UpdatePanel1.ClientID %>','Refresh:0,1,2'); //refresh update panel
On page behind add the following event handler to capture the post back call:
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
string arg = Request.Form["__EVENTARGUMENT"];
if (string.IsNullOrEmpty(arg)) return;
if (arg.StartWith("Refresh")
{
//parse data first then do your thing here...
}
}
And of course don't forget to wire event to the above method:
protected void Page_Init(object sender, EventArgs e)
{
UpdatePanel1.Load += new EventHandler(UpdatePanel1_Load);
}
we use the __dopostback() method which simulates a postback and causes the updatepanel to refresh
__doPostBack('controlName','');
Don't forget that the control name is it's HTML ID (which may well contain dollars etc) and not just it's ASP.NET ID.
As far as I know you can either call this method and pass in the hidden value field, or the div that it is in.

Resources