Changing page contents on postback - c#

I have a survey, where you have to click through several pages with questions. I use a "Next" and a "Previous" button for doing this. I use a session for keeping tabs on my position. However, this is a problem.
I use the button_click event to increment the page counter, but since this fires after the page_load event, nothing happens on the first click, and for every click thereafter everything is one page behind because the questions are rendered before the counter is incremented.
Is there any way to solve this without using the query-string?
Comment response 10:15:
My understanding is that the following happens:
The first page loads and the counter is not set. The counter is set to 1. The first page is displayed.
The user clicks the "Next" button, firing a postback.
The page loads, displaying the same set of questions, because the counter is still on 1.
The buttons Click-event is run, incrementing the counter. However, the page is still displaying the old questions.
After this everything is one page behind because the questions are rendered before the click-event is fired, incrementing the page counter.
I'm probably not seeing something obvious here :|

Why don't you use the out-of-the-box Wizard control? This is what it is designed for. They have solved all of this issues that you are likely to have and it is entirely customizeable.
Scott Gu's blog
MSDN Walkthrough

Like Pavel said in the comment posting some code to clarify the question might help in seeing where you could have gone awry. There could be several things amiss, first check to make sure your logic is sound in your code. Go through step by step and see if it doing what you are wanting. Setting up break points is also a good idea to find the issue. It could be just a variable out of place (this has happened to me many of times and caused a lot of hair pulling).

I got past it by using the LoadComplete event to set all the "dynamic" text and controls. Since the LoadComplete event of the Page object fires after the postback-events of the controls, this works as I want it to.

Related

How do I reset Page life cycle from code behind?

I have a web page that is meant to be a product view for a computer. I have a div next to the product photo that contains the product information. The div is runat="server" and when the user selects a 'customize' button within the div, I have C# in the code behind button event handler that erases the current product information in the div and replaces it with drop down menus for the customization options.
I want to have a "Cancel Customization" button that allows the user to stop customizing and go back to the original product view page. I have tried using Response.Redirect("CurrentPageUrl"); but because I am redirecting to the page I am currently on, I guess it's a postback and not a full life cycle reset. The result is that the page reloads with the div just empty. What I really want to do is reset the page life cycle somehow, so it is as if the 'Customize' button was never clicked and the original page code, including the product info, on the server is completely reinitialized. I have looked around for how to do this but everywhere just seems to recommend Response.Redirect() which as I have said does not produce the desired behavior.
I realize I could just reinsert all of the original product information from the original page but this seems like a lot of code and I figure there must be a way to just re-initialize the Page?
Summary:
Page initializes with div filled with Product Info.
Customize button is clicked, button handler in C# code behind removes contents of div and replaces it with customization options.
desired behavior: 'Cancel customization' button is clicked, page is reloaded so product info is now back in div.
current behavior: 'Cancel customization' button is clicked, Response.Redirect("CurrentPageUrl") is called, page posts back and div is now empty.
I will be checking this post very often for the first few days so please don't hesitate to ask for more info I'll get back to you quickly. I apologize in advance if this question has already been asked, I did spend quite a bit of time looking for answers on S.O before posting this and as I said all I could find were recommendations for Response.Redirect() which is not what I'm looking for. I am developing the site using VS2019 ASP.NET framework web forms if that makes a difference.
Thanks for any answers!

Search Button on Customized Gridview Takes Two Clicks to Fire Event

I have a customized Grid control that is inherited from Gridview that has search and pagination. Everything works great except this one little thing. Whenever I use the pager to go to the next page, then I use the search, the event that handles the search does not fire on the first click, it takes a second click to get it to fire. Any ideas?
The code for the control is here:
Grid.cs
Important Note
I am aware that it is frowned upon to post large blocks of code... But, the entire control's code is posted to give the whole picture of how it is built. The control itself is a bit on the complex side as it performs searching, sorting, and pagination all server-side; and this code is a completely custom control that just INHERITS the GridView.
I AM NOT looking for someone to write a fix for me, just an idea of why this one situation may be happening!
With that said, to break things down a little more with the code... The search form (text box and buttons) are created dynamically and added in the CreateChildControls method. The search form works perfectly when the Grid is initially loaded, but after using the pagination, the first click of the Search button does not fire the method assigned to the Search button's command event, but the second click does. It seems as though something in the postback is not recognizing the button's command event has been triggered...
A trace of the calls to the methods show:
1st Click - Everything from ViewState is loaded, no postback events are called.
2nd Click - Everything from ViewState is loaded, postback events called.
I'm looking for ideas on where to go from here, as I've been trying everything I can think of page life-cycle wise to see if I can get this functioning properly. The only thing that has worked is setting EnableEventValidation to false on the page that implements the Grid control, and performing the "initial" data bind on every "Page_Load" (not just "if !IsPostback"). But, for obvious reasons, that is not an acceptable solution.
Found the issue... Posting here for anyone who might make a similar mistake.
In CreateChildControls(), I was executing the base before adding the search form to the control. A simple switch around to calling the base after adding the search form, and getting rid of the condition "if (this.HasControls())" resolved it. From what I can see, calling the base after adding any child controls allows those child controls to have their events/handlers properly registered.

.Net form controls drop by one "line" after selection

Yes, I realize that's a bit of a vague title but I'm having a hard time stating the problem. I have a .Net .aspx page that has a Master page, some Ajax, and an updatepanel. My problem occurs on 2 different pages but in both cases I'm either selecting a radio button or a checkbox when the behavior occurs. Immediately after selection the entire page moves down. It does not scroll but instead it is like an extra tag was inserted into the source. I have done HTML source comparisons before and after this change and nothing is different. I can only assume it is related to the updatepanel but I cannot determine where this may be happening.
I'd be happy to provide more information if you can direct me towards a solution.
Thanks!
I am not entirely sure if this will do it and there is not enough detail here to be sure, but have you tried setting RenderMode to inline on the UpdatePanel? Maybe that will do the trick. Otherwise go take a look at Fiddler and see what is coming back from the server. Alternate recommendation (if none of the above work) is to just get your result in Json and change the markup yourself with jQuery or something like it.
Incredible. This was exactly the problem. I've modified my code as follows and form stays in place after selection. This was a simple case of selecting a radio button and having it auto-populate a dropdownlist.
[asp:UpdatePanel ID="panelValidation" runat="server" ChildrenAsTriggers="true" UpdateMode="Always" RenderMode="Inline"]
[ContentTemplate]
[asp:ValidationSummary ID="ValidationSummary1" runat="server"]
[ContentTemplate]
[asp:UpdatePanel]
Thanks!

ASP.Net Form Databound problems

I know there is a similar problem on this forum, but the solutions did not really work for me. I am populating form controls with fields from a few different data sources, and the data shows up great.
I have an ImageButton control, which has an OnClick Event set to grab all of the data from the form. Unfortunately, when I click the button, it seems as though the page is reloading first, and THEN is executes the OnClick call. The data that was hand-entered, or hard-coded seems to be pulled fine from the controls it was entered in, but anything that was pulled from a datasource is not able to be read. Any ideas. this is the last hurdle in a project that I have been working on for 6 months.
Are you talking about drop downs or gridviews? When are you binding data, on page load?
Good design will have you bind your data upon page load but only in
if(!isPostBack){
dropdown.databind()
gridview.databind()
}.
Otherwise it will rebind every page load. If its not reloading you can get selected values from those controls if thats what your looking for.
An alternative is to set your data source and databind in your aspx page with a datasource object. That automates the above automatically.
Have you enabled viewstate on your controls? Posting code samples would go a long way to helping solve your issue.
When you click on a bottom in asp.net at first all the page Events take place like Page_Load and ... and then the event happens ( in this case Click).
But because everything is loading again, I think that you have a !isPostback in your code that you use to bind the data, you should remove that in order to get your data every time.
Or if it is not the solution please post some code and more description for the problem
Actually, it is hiddenfields, dropdowns, labels, and textfields. I just tried doing the binding in the init, and the load, but no dice. When I tried binding it on !isPostBack only, none of the fields showed up.
I think one of the main problems is that the dataset I am getting is from a method call to an API. I receive the data fine, but it comes in programmatically, then I have to do all of the control-setting programmatically as well. Would you like to see the code for ideas? Thank you for helping, no one is working today!
Jason

Formview Being Cleared

My problem is that all the textbox's my formview are getting cleared when I hit the submit button.
I currently have a page with a small section that has an update panel around it. This small section adds an address to my databse. To the left of this form there is a gridview that is tied into the formview. So if i click on an item in the gridview its contents fill the address section(formview) with the correct data.
When I hit add the data gets validated in the c# code behind, and if all the info is correct the address is inserted. If there is an error the entire form is cleared and the error message(label) is displayed.
I have already done this many times in other pages, but none have had the gridview tied to the formview, and they have all worked. I tried removing the gridview and the form still erases itself.
Is there some reason that .net thinks it should be clearing the form? When in other cases it decides it won't? If so what are these cases, or what general tips should I try to solve this?
in the page_load are you using if(!Page.IsPostback) { ... } so if it's a postback nothing gets re-bound?
is the ViewState enabled?
Yup many hours later I found that a single panel that was wrapped around the section had a EnableViewState="false" added to it. Sad part is that I know I didn't add that because I didn't even know what it was until craig here mentioned it. Visual Studio must have added it sometime.

Categories