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.
Related
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.
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).
I have an ASP.NET web form on which I'm displaying a list of database items via user controls, generating the user controls dynamically - working fine.
Also on the page, I have an asp:dropdownlist filled with items that can be added to this database list. Along with this dropdown I have a button 'ADD'. My intent is that the user chooses and item, clicks add, and then the list of user controls on the form will include this new item.
I have all this working.
My issue is that the user control has a button 'DELETE', which removes the selected item from the list. That works, EXCEPT when I add a new item. Because my 'add' button event is always fired after Page_Load, even if I regenerate the list of user controls, the internal user control click events won't fire because the controls weren't created as part of Page_Load.
I need to know what I'm doing wrong, or best practices here, any advice. I tried to be precise in the description of the problem, but if I've not succeeded, let me know and I can add more details.
SIMPLE RESTATE: I need to know how to add a dynamically created user control to a page via a page button click event, and still have the user control internal click(etc) events firing.
Thanks for any help.
EDIT: Based on the feedback from the gentlemen here, and doing some further research related to their suggestions, I ended up implementing a solution based on what's presented on this page:
http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx
Here's a snippet showing how I dealt with this. This snippet resides in my PreInit event handler. Not exactly an elegant weapon for a civilized age, but sometimes a blaster is all you've got to use.
'Look to see if button for adding a new client number has been
'clicked. If so, call the sub to add the item NOW, so that it
'is able to have it's internal click events fire.
For Each control_string As String In Request.Form
Dim ctl As Control = Page.FindControl(control_string)
If (ctl IsNot Nothing) AndAlso (ctl.ID = "cmdAddClientNumber") Then
Me.AddClientNumberToList()
Exit For
End If
Next
On the button handler, you initially add the UserControl to the Page. OnPreInit (which will next be fired when the user clicks Delete on the UserControl), you should re-add the UserControl - so that it exists, and can handle the Delete button event.
You will need to devise your own state tracking mechanism to determine that you need to add the UserControl during PreInit. I generally use ViewState, as evidenced by this seemingly similar post.
Similar question:
How to create and use custom control with UpdatePanel added to the page programmatically
Dynamic control should be re-added to the control tree OnPreInit, see documentation:
PreInit - Create or re-create dynamic controls.
ASP.NET Page Life Cycle Overview
I'm using asp.net user control in my web page. I used asp.net repeater control on the user control & created one property of user control.And depending on the value of the propery i loaded the datasource property of repeater control on user control at page load event.
Then i used the userc ontrol on the another asp.net web page & set the property on the page load event. after that it is loading the datasource of repeater control of the user control.
Again I have set the user control property on the text changed event of the text box which is placed on the another web page. I want to again load the datasource of repeater control of the user control depending on the value entered in the text box. How to handle this?
Use OnTextChanged event to bind you repeater, after event fire use __doPostback event and pass some value to it, so that when its postback you can find that value and identify that this post back is done from textchange in IsPostBack event or even you can pass the page event in __doPostBack which will fire automatically then that event for example public void BindRepeaterOnTextChange(){}
check this link for more information
http://wiki.asp.net/page.aspx/1082/dopostback-function/
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.