Using Page.IsPostback Within a User Control Wrapped in an Update Panel - c#

I have a main page into which i load a user control with a grid and add/edit link buttons.
If I bind the grid by setting the datasource and calling the databind() method in the page load event then it sets properly. However, I want to keep the selected row between postbacks, so I wrap the bind code in "if (!Page.IsPostBack) {}" as usual. My problem is the page load always registers it as a postback and my code never runs.
I am using the 2.0 framework, and my grid is an 2008.1 Infragistics for the 2.0 framework.
I thinking this must be something simple.... or hoping anyway!
Thanks in advance

If you place your control into an UpdatePanel, then you should check for Page.IsCallback instead of Page.IsPostBack.

The two ways I found round this were:
to load the user controls when the page is first loaded and then hide them until user selected what they need to see.
to load a new page into an iframe on the main page allowing it to have its own page control meaning when its loaded in at first its not a postback.
Not the greatest, but gets by.
Thanks for the help.

I have mixed feelings about necroing a thread this old, but the question is still relevant, and there weren't any great solutions offered, so though I would add what I recently did to solve the same issue:
I had a similar problem with a site I was building. My solution was to add a method to the user control called "OnFirstLoad" that does all of the stuff I would have wrapped in an "if not Page.IsPostback" block. I then call the "OnFirstLoad" method from the hosting page the first time the control is loaded into the control tree. This way the control itself doesn't have to worry about whether or not this is a postback, and the main page can initialize it as needed.

Related

Can I call an event method of a parent from within an UpdatePanel on a UserControl?

I have the following scenario that I need to implement.
I have a GridView with a list of records. When a user selects a record, a pop-over screen appears within the browser showing the contents of the record. The contents are rendered from a UserControl, and within the UserControl there is an UpdatePanel that is used for loading/reloading data as it is manipulated and saved.
What I need to occur is that after each save on my UserControl, the GridView on the parent page needs to refresh as to reflect any changes on the record being shown in the pop-over editor control.
My plan is to setup an event on my UserControl that the parent page will subscribe too. When a save operation is executed from within the UserControl, it will call the event, and the parent page should update itself.
However, since the UserControl operates within an UpdatePanel, and is only doing a partial-postback, I am suspect that my events will not work properly.
Should this work as I hope or will I need to find another way of telling my parent page to refresh the master GridView after a record is saved?
If I need to to call an update in a different manner, what is the best approach for this scenario? I ask because I'd rather not build-out some complex saving/updating operation only to find out that I've made a few miss-assumptions and need to re-code the whole update and reload process.
I have done that before I just don't remember exactly how as I haven't been working with Web Forms in a little while. You have the right idea. The event will get raised on the parent page but you will have to do another PostBack/Partial PostBack afterwards to update the grid from the event handler on the parent. Also you will have to make sure the properties on the UpdatePanel have been set up right.
Another option is doing a full postback when you close the PopUp. If the popup is big enough that it prevents the user from seeing the grid it wont make a difference whether the record on the grid is updated when the Modal is closed rather than when the data is saved.

Add User Control multiple times dynamically - ASP.NET

I have an User Control (Web Forms) which i need to be added multiple times on the page. I have a button that is supposed to adds the controls again and again up to 10 instances. Also in the user control there is a remove button that should remove itself when clicked. This is what i tried but after adding one instance nothing happens. Meaning it adds one instance of the control and then stops working ...
MyControl ctrl = (MyControl )LoadControl("~/Controls/MyControl.ascx");
ctrlHolder.Controls.Add(ctrl);
Please notice that i have the ctrlHolder ajaxyfied using telerik AjaxManager.
Thank you
I think i will change the approach and use the Telerik's RadListView control which suits my needs better. I can add as much controls as needed calling the ShowInsertItem() method and keep them saved into session. Sorry for bothering with the question and thanks for reading.

How to get the information from a dynamically created TableRow C#/Asp.Net?

Alright so in the project I am working on I am making a hard coded asp:panel on the page and then filling it with:
A Drop Down list
An asp:Table, with an asp:TableHeaderRow
Add and Cancel Buttons
When the selectedIndexChanged event happens with the DropDownList the tablerow gets created then filled with 6 or so tablecells which then get asp:Textboxes placed inside of them.
Eventually they all get added to the tablerow which then gets added to the tablerowcollection.
That all works just fine,but when I go to add the information from these dynamically created TableRows with a foreach loop they are no where to be found but the TableHeaderRow is.
Also when I click add and let it go through the process I get no null exceptions and the only thing left after it is the header, all my rows disappear.
Anyways I feel that I am just missing something really dumb. I've looked through a lot of forums, posts, MSDN and never found an answer to my problem. Any help would greatly be appreciated!
You need to add your dynamic rows to your Table in Page_Init not Page_Load. If you are creating it in Page_Load your new rows won't be added to ViewState and you will see this problem.
Unfortunately you are adding them in response to a server side event which can get tricky. You still need to add the rows in Page_Init but when the page posts back and you at in Page_Init then the selectedIndexChanged event hasn't yet fired. It's too early in the page lifecycle.
If you want to know if it has fired at Page_Init the only way I have found is by examining Request.Form("__EVENTTARGET") collection at that point. This contains the control ID of the control that has triggered the postback - in your case this will be the dropdown list. The control that fired the event will be there but the ID will be qualified i.e not MyControID but ctl_MasterPageContentHolderID_NamingContainer1_MyControlID` or the like - so you will have to take it into account when looking for it. Once you have identified that that event has fired then you can add the rows. Once they are added there then they won't disappear.
As I say it is tricky to get working but I have done this successfully in the past. Generally dynamic controls can be very hard to work with for just this reason. You may want to consider alternatives. Best of luck with it anyway though.

Gridview, is there an event, how would I determine that it has finished being rendered?

Gridview, is there an event, how would I determine that it has finished being rendered? That's basically it, I want to adjust the height of some other controls on the page and I want to pick up this event.
Sorry I should have been more explicit in stating that this is actually a web page not a winform. In the end I managed to solve the problem by registering a function with the page load which is called after the controls have been drawn and they calling the gridview size and resizing the other controls to fit. It works and that is that.
Thanks.
The GridView inherits from Control so you can use any of the events, I would try PreRender, it seams it will provide you with what you need
To see all the GridView events available to you;
In Design Mode, select the gridview and click F4.
In the properties window there is a 'lighting' tab which lists all gridview events you can attach to.
Assuming you are in winforms, the best place to do it would be after you bind data to your grid, in the DataBindingComplete event.
Managed to solve it, see details above.

How to access Form controls on Postback from Dynamically built form

I'm adding controls at run-time to the Page Form object.
Each control has an Id.
On postback, I want to be able to access these controls and their values.
Currently, when I do a postback, the form has no controls in the Form.Controls Collection.
How can I rectify this?
Is this only possible if you add the controls to the page every time the page loads?
Dynamically added controls need to be added with every page load or else they will be lost.
However the viewstate of these controls can be maintained as long as they always get added with the same ID.
I believe you have to add the controls dynamically in order to access them on postback. So if you add a textbox dynamically, your event handler can't retrieve its value unless you add it again.
EDIT: One workaround I used was to add a predetermined set of server controls to the page and then use JavaScript to hide/show those elements. You avoid postbacks and you avoid the unnecessary tom-foolery associated with retrieving values from dynamically added server controls. Of course, this limits you to a predefined number of controls.
This was a big pet peeve of mine with ASP.NET web forms and is a factor in my decision to explore ASP.NET MVC - no more viewstate/postback mess.

Categories

Resources