Refresh all update panels on the page? - c#

I have some code that modifies a value that several controls in other update panels are bound to. When this event handler fires, I'd like it to force the other update panels to refresh as well, so they can rebind.
Is this possible?
Edit:
To clarify, I have an update panel in one user control, the other update panels are in other user controls, so they can't see each other unless I were to expose some custom properties and use findControl etc etc...
Edit Again:
Here is what I came up with:
public void Update()
{
recursiveUpdate(this);
}
private void recursiveUpdate(Control control)
{
foreach (Control c in control.Controls)
{
if (c is UpdatePanel)
{
((UpdatePanel)c).Update();
}
if (c.HasControls())
{
recursiveUpdate(c);
}
}
}
I had 3 main user controls that were full of update panels, these controls were visible to the main page, so I added an Update method there that called Update on those three.
In my triggering control, I just cast this.Page into the currentpage and called Update.
Edit:
AARRGGGG!
While the update panels refresh, it does not call Page_Load within the subcontrols in them...What do I do now!

What about registering a PostBackTrigger (instead of an AsyncPostBackTrigger) that will refresh every panel when a specific event fires.
Or add the trigger that already refreshes some UpdatePanels to the other UpdatePanels as well.

You can set triggers on the events in the update panel you want updated or you can explicitly say updatepanel.update() in the code behind.

This is a good technique if you want to refresh updatepanel from client side Javascript.

Page.DataBind() kicks off a round of databind on all child controls. That'll cause Asp.Net to re-evaluate bind expressions on each control. If that's insufficient, you can add whatever logic you want to make sure gets kicked off to an OnDataBinding or OnDataBound override in your usercontrols. If you need to re-execute the Page_Load event, for example, you can simply call it in your overridden OnDataBound method.

instantuate both view panels to a third presenter class, Then let the presenter class control both views. for example:
You could just pass over what you need the 'middle class' to do its job for example, in your main you could have;
PresenterClass.AttachInterface(mIOrder);
PresenterClass.DoSomeCalulation();
PresenterClass.drawPanel(1);
PresenterClass.AttachInterface(mIOtherOrder);
PresenterClass.DoSomeCalulation();
PresenterClass.drawPanel(2);
each view will have its own controls. So many differant ways you could do this.. alternitivly you could use the middle class to instantuate both your panels then in each of your panels you could have 'get methods' to retrive the data for processing.

Related

prevent the page to post back on button click while event will work

I have written a code in VB.NET in which there are around 300 asp controls and all of them are creating dynamically with more than 6 condition per control (like If control = dropdownlist then some code Elseif control = radiobuttonlist then some other code).
Now I want to write events for some controls but due to postback, when event is fired all of the controls are getting flushed.
When I set button1.onclientclick="return false" for button, the page stopped post back but the event also stopped working.
I have an option to save the values of controls in view state then recreating the controls and then refill the values to dynamic controls. This option will increase my line of execution.
Is there any other method though which I can prevent the page to post back on asp control event so that my asp control persists with entered values in it and also my event will work.
this is the Code1
this is the Code2
Create your dynamic controls OnPreInit Event of Page, Hope this solves your problem
override protected void OnPreInit(EventArgs e)
{
CreateDynamicControls();//Function which creates all dynamic controls
}
I have used JavaScript and Ajax to achieve my requirement.
I have called JS function for button's onclick and Textbox's onchange (like: btn1.Attribute.Add("onClick",JSFunction(); return false;) [return false is to prevent postback].
Then I used ajax post method to do my stuff on .vb page.

One time processings of usercontrol when it is wrapped inside updatepanel

Traditional way of initializing controls with the values from session state as below -
if (!IsPostBack)
{
if (Session["sessionId"] != null)
{
//initialize controls
}
}
Exhibits unexpected behavior when user control is wrapped inside update panel.
I need to populate usercontrol textBox values first time from session state. And subsequent loading of these controls values should be populated from viewState.
How do you handle this scenario. Do you think Page.IsAsync will be helpful for this ?
IsPostback property returns true even if postback fired from control, placed in an UpdatePanel. So it's ok to use IsPostBack property for control initializing on first page load.
By the way Page.IsAsync property serves for absolutely different purpose, not for detecting asynchronous postbacks. If you need to detect asynchronous postback from UpdatePanel, check ScriptManager.IsInAsyncPostBack property

How can I invoke OnInit event of UserControl?

my situation is a little complicated. What I'm trying to do is make reload UserControl (with dynamically changed control inside my UserControl). It's simple when I trying to do it OnInit or Page_Init event of my Page. But I need to do this inside a click event of button which by the way is ext.net type and have build in callback events.
So is there any way to invoke OnInit event of UserControl on event click raise?
If any more information needed pls feel free to ask in comments:)
Thanks for advance:)
I think you should manage this case differently.
OnInit is fired according to the webform life-cycle, in which each step has a specific purpose :
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx
During page initialization, controls on the page are available and
each control's UniqueID property is set. A master page and themes are
also applied to the page if applicable. If the current request is a
postback, the postback data has not yet been loaded and control
property values have not been restored to the values from view state.
You'd better not 'force' this concept, try to adapt your code to meet the flow constraints.

When I add an custom UserControl to a Panel dynamically, the usercontrol loses all event handling

I've got aspx page which dynamically loads UserControl into a Panel object based on the input on a set of radio buttons. The UserControl successfully adds and displays properly to the Panel on postback and calls the Page_Load() event just fine in the UC but when I interact with the form in any way that would trigger an event, the event is not captured on the postback.
I've tried to add the event handling association in the Page_Load() which I know gets called as well as adding the association in the ASP.NET tag without any difference in result.
This is how I am adding the control (object names have been simplified):
private UserControl _control;
protected void RadioButtonGroup_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = (RadioButton)sender;
if (radioButton == RadioButton1Ctl)
{
_control = (UserControl1)LoadControl("~/Controls/UserControl1.ascx");
PanelCtl.Controls.Add(_control);
}
else if (radioButton == RadioButton2Ctl)
{
_control = (UserControl2)LoadControl("~/Controls/UserControl2.ascx");
PanelCtl.Controls.Add(_control);
}
}
As I said, the control gets successfully added by when I click any buttons or have any postback events which should be bound on the UC, the control gets removed from the page and events aren't fired.
Any help would be greatly appreciated.
This is happening because the controls are being added dynamically. I would suggest using the DynamicControlsPlaceHolder instead of a Panel. It will persist your controls for you when the page is posted back.
DynamicControlsPlaceHolder:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
The other alternative is to recreate the controls at every postback, before the ViewState is reloaded. I would suggest using OnInit.
The DynamicControlsPlaceHolder takes all of the hard work away, so that might be the best option for you.

ViewState, UserControl and IsPostback

Basically, I have a drop down list and a dynamically added user control. The user control loads a grid view depending on the choice that was made in the drop down list. The drop down list is not part of the user control.
Now, the question is, how do i simulate (isControlPostback = false) every time the user changes the selection in the drop down list? It looks like ViewState remembers the control.
Inside my user control I have:
protected bool IsUserControlPostBack
{
get
{
return this.ViewState["IsUserControlPostBack"] != null;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsUserControlPostBack)
{
ViewState.Add("IsUserControlPostBack", true);
//load stuff in the grid view and bind it
}
}
When the user changes the selection on the drop down list, i have a javascript confirm box, and the page posts back. So OnSelectedIndexChanged event for drop down list doesn't get triggered. I would like to remove to do something like this every time the selected index changes:
ViewState.Remove("IsUserControlPostBack");
You can make changes to the control in prerender event. When this event is fired all other actions are made.
Or you can do public property in user control and when setting required to value react on appropriately.
The ViewState you access in your user control is not the same one you access on the page. If you need your page to communicate with your user control, I suggest you add a public method on your user control for this purpose.
If, for some reason, you prefer
something similar to your ViewState
approach, you can try Context.Items.
Note that Context.Items is not
preserved between requests.
Add the control to the page sometime before OnLoad. E.g. OnInit. Between OnInit and OnLoad, the viewstate is loaded and postback events are run.
For anyone who is interested to know the answer:
I ended up implementing a public property inside user control and load the control inside the server drop down list SelectedIndexChanged event rather than OnInit. This eliminated the need for explicit Viewstate use.

Categories

Resources