loosing values in user controls after postback - asp.net

I have a page that has a user control in it. In the user control text boxes generate dynamically in a place holder and there is a button that will save all of them into the database. But here is the problem: When i click on save button, first, my page's page load event runs, then it loads user control, then user control page load event runs. so text boxes loose their content from last page. any idea?

Within your Page_Load, continue to always generate your textbox controls (regardless of IsPostBack). However within the control, only set an initial value if IsPostBack = false. That way when IsPostBack is true the textbox will take it's value from the ViewState instead.

Related

Can I fire button click event without firing pageload event?

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?

Can we Change the Page.IsPostback property set to false forcefully

I am dynamically loading the web user control into the ajax tab container, and with the help of ActivetabChanged event i am binding the relevant user control into that particular tab.
In each user control i have the data lists contains thousands of records, which i am binding on user control page load event.
I am putting the binding code into !page.IsPostback, now here comes my problem, when ever i changed the tab change event, Page.IsPostback returning true, and datalist does not bind, i force to put the code into !page.IsPostback becaue i have to fire the datalistCommandEvent.
So is there any solution for this problem?
You need to track whether tab has changed or not. Typically, you can store the previous active tab in the view-state and compare with the current active tab in page_load. If the tab has changed then bind the data-list from user-control in the active tab (by invoking public method in your user control that will do the binding of data-list).

Detecting self-postback in a dynamically created UserControl

I have a button and a panel. When the user clicks the button it loads a usercontrol and adds it to panel.Controls. I need to bind a grid in the usercontrol when the usercontrol first loads (Page_Load) but not when the user clicks a button inside the usercontrol, aka triggers a postback in the usercontrol. I can't use Page.IsPostback because it returns true when the user clicks the main button which loads the usercontrol. What can I do?
It's primitive, but you can check for any control that caused the postback by comparing the value coming from:
Request.Form.Get("__EVENTTARGET")
This returns the uniqueID of the targeted control; check this to determine which button caused the postback, and act accordingly.
You can give the buttons inside of the user control a CommandName and check the value of the CommandName. You can also check the type on object sender to determine the control that caused the post back.
Don't forget that you will have to re-add the user control on each post back after it is dynamically created (in an event that occurs before Page Load), so you will need to implement a mechanism to determine that the user control was added in the panel.

Why does drop down list in ListView in user control cause full page postback in page-level update panel?

I have the following scenario. I have a page that hosts several user controls. The user controls are all surrounded by a single update panel. All user controls have a save button on them. When the save buttons are clicked the page updates the update panel as expected.
Some of the user controls contain editable list views. Whenever an action is taken on these list views, the Update Panel is NOT refreshed, but the whole page posts back. How can I get these ListViews to also refresh the Update Panel? I cannot post the code because of NDA.
There are no javascript errors on the page reported by either IE8 or Chrome.
Check the autopostback property of the dropdownlist control, if the property is set to true it tries to postback every time the selection changes.
If this does not work for you, you may wish to try implementing manual update triggers in your controls code and setting the update panel to conditional, to firmly control when you want the panel to update.

How to control which user control to load in a way that allows user control events to fire

I dynamically load one of many user controls based on which tab the user clicks. I store the selected tab index in view state. The user control has a checkbox on it that fires an OnChanged event. My problem is that if I load the user control in Page_Load, the checkbox event doesn't fire because the control didn't get created in time. If I load the user control in Page_Init, the page doesn't know which user control to load I believe because the ViewState isn't loaded yet. How can I store which user control to load AND get the events on the user control to fire?
The best strategy I've found has been to create all the controls in the Page_Init event, and set their Visible property to false (in Page_Load in your case) if they aren't supposed to be present on the rendered page.
Edit
Another option is to determine which controls to load based on some other criterion (besides ViewState). For example, if you make the current tab one of your query string parameters, that data will be available on the request during Page_Init.

Resources