I have a button on my page and and few textboxes which alters their context on page_load event. when I open the page for the first time it loads random data from a database to textboxes with a function in pageload and also I create those textboxes dynamically. what I want is to make some changes on textboxes texts and click the button than send new context of textboxes to database. But when I click on button, PageLoad event fires again and It regenerates the textboxes and the context of them alter naturally since they are newly created textboxes. I tried to use "if(!Page.isPostBack)" in pageload but then the textboxes and related objects are not being created and they become null.
how can I overcome this problem?
best thing I can think of is to find some way to fire buttons click event without firing PageLoad event, but I dont know how can I do it.
any help would be appreciated;)
From Comments
Can you regenerate the Textbox on each Page load Execution? and data assignment to these Textbox should be under !Page.IsPostback. All you have to do is to keep the IDs of the Textbox in ViewState only first time page load and next regeneration of Textbox will have the previous stored ID and you also have to store the ID corresponding data in ViewState. Makes sense?
Related
I have a textbox called 'Value' (among other textboxes) on an ASP page. I have an event handler hooked up to the textbox so that when the textbox contents are changed, the event handler runs (control.textchanged event)
I have another command button ('Save') which saves the textbox value to the database and then calls window.close() via javascript. The problem is that when the textbox is changed and the user clicks on the 'Save' button without clicking off the textbox, the value is somehow saved in the database but the javascript does not run. Then, the user will think the value did not save because the window did not automatically close, and when they click 'save' again, the value in the textbox saves for a second time and then the javascript runs which closes the window.
It is very similar to this issue (http://forums.asp.net/t/523981.aspx?Pending+Postback+Troubles) but i do not understand the solution provided there.
Can anyone provide a method which may prevent the record being created twice in this instance?
Thanks,
c
I'm creating a simple custom pager for repeater (or anything else, it doesn't actually matter because all I want from it is current page number). Pager has 3 public properties: PageSize, MaxResults and PageIndex.
First is set in markup, second is set on the page in Page_Load or EventHandlers when various buttons are clicked and repeater is databinding again. Third is decided by user when clicking on page numbers.
Page numbers are dynamically created LinkButtons with OnCommand set to method that launches my pager's OnPageChanged event to inform webpage that it need to reload data.
The problem is that in order to create correct number of page linkbuttons I need to know MaxResults. I will know it not sooner than in Page_Load and most probably even later. But as far as I know if I want my linkbuttons to fire events they need to be created before Page_Load when I don't know MaxResults...
Now I'm creating linkbuttons in Page_PreRender and event's don't launch. What is the solution for this? It looks like GridView's built in pager is also done with LinkButtons and it also can't know count of data before databinding but the events are firing.
I am trying to update controls in the of a ListView control after handling the ItemCommand event.
My ListView displays line items of a purchase order in the as html table rows along with a TextBox to enter a new quantity and a Button to update the quantity. My ListView then displays sub-totals, discounts, and a grand total of the line items above in the as html table rows as well. On initial load, I set the values of the controls in the ListView's in the LayoutCreated event handler.
When a new quantity is entered and the button to update is clicked, I handled the event in the ItemCommand event handler. I update the quantity of the specific line item. I then re-bind my ListView to the underlying collection and call DataBind(). The problem is, LayoutCreated is not fired this time around, only on initial load.
My work around is to just pull those controls out of the and address them as static controls, but I like having them inside because my table markup can be fully contained in the and my can show cleanly without having to juggle the static controls' display properties.
Is what I am asking possible? Thank you for any help you can provide.
I'd reccomend handling the DataBound event of the ListView (instead of the LayoutCreated event), and setting the values there. That will get called everytime you re-bind the ListView, as well as when it loads for the first time, which (from your description) is what you want to do.
I have an updatepanel. Inside that i have some controls for selecting search criteria. For search button onclientclick iam calling javascript functon for validation and in onclick iam calling the mathod to populate the gridview. On clicking search first time it is not loading the grid and clicking the second time it populates.
Remove from the updatePanel your code and checks for errors on the first click, then place it again.
Its probably have to do with your viewstate that is not populate correctly so your first click gets an error that you do not see because of your updatePanel.
My understanding of the order of page events is this:
Page : Load
Control : DataBind (for a GridView or whatever)
Control : Load
Control : Clicked (for a Button)
Page: PreRender
Control : PreRender
(There are lots of others - but these are the ones I'm interested in)
The important thing to notice here is that the button's click event comes after the gridview's bind event. If the button causes a change to the data, the GridView displays the old data. I could rebind the control in the PreRender event, but that seems totally ugly.
This must be a very common pattern (a button that updates data). How do I put this together so that the GridView binds to the data after the Button click changes it?
The answer was in the Button Click event, after the data has been changed, call DataBind() on the page to cause the GridView (and anything else that needs it) to rebind. I didn't realise you could do that.
Thank you Ocdecio & Mufasa - I would mark your answers as helpful, but I got no rep yet.
ASP.NET does, by default, lots of binding and rebinding. Rebinding after a click event is normal.
The only reason the button click event comes after GridView bind is because you programmed your page to do that . I don't see any problem in binding the control in the PreRender event, in fact that is the only way to take action after a control event (such as Button onclick).