updating updatepanel's based on texbox focus lost in asp.net - c#

I have an asp.net page with two user controls. Each of which are in separate updatepanel's One user control has three textboxes. I want to update the second updatepanel based on change of text/ focus out in first user control. How can I access both user control's textboxes, and other controls in page and update the updatepanel on change of text ?
updatepanel1
user control1
textbox1
textbox2
textbox3
updatepane2
usercontrol2
label1
Regards,
Asif Hameed

Whoa, UpdatePanels! That takes me back. Triggering UpdatePanels to "postback" asynchronously from the client has always been a bit of a kludge. The common way was to register an AsyncPostBackTrigger with a hidden button's click event and then explicitly call its click event on the client side. However, a solution with a few less layers of indirection is to call the ASP.NET AJAX library's __doPostback() JS function.
Assuming you're using jQuery (which may be far-fetched considering you're still using UpdatePanels!), you can add an event handler to the 'focusout' event of your UserControl1 to trigger the asynchronous postback of your UpdatePanel2. I would recommend putting this JS outside of one of your UpdatePanels.
$('#userControl1').on('focusout', function() {
__doPostback('UpdatePanel2UniqueId', '');
});
I dug up a good article that explains the technique of using __doPostback in a bit more detail.
Easily refresh an UpdatePanel, using JavaScript

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.

How to call a full postback after the execution of the button click event inside an user control?

Okay,
I have this scenario:
There is a user control with an update panel within it. There is a button within that update panel with proper postback trigger being set. The button_click event is also defined well. I need to call a full postback of the parent aspx page once the "button_click" event is completed. Under ideal case, all the form submission events such as postbacks occur before event based methods are executed. This means my page will first be reloaded then the button click event will be executed. I want something like to reverse this operation. First Button_click event execution then one postback after that on the aspx page(this page calls the user control-> and this user control has the updatepanel with button in it).
Any possible way out would be highly appreciated.
I don't think there's a way to change ASP.NET's lifecycle, like the one you described. A (dirty) way of postbacking the parent page is however to put a hidden button on that page, and call it via javascript in the UC. (via ScriptManager.RegisterStartupScript)

Click event doesn't fire in UpdatePanel even with IsPostBack check

I'm developing simple WebForms application, where I'm trying to catch simple click event of Button ASP.NET Control from the UpdatePanel.
Button wasn't added in MarkUp part of project. It was added dynamically from the CodeBehind and also event was added dynamically too to the static class.
Here is code:
http://ideone.com/bnntkb (CodeBehind only, because MarkUp holds just only the ScriptManager and UpdatePanel controls ).
First of all, I think the issue related to the Page.IsPostback and I have tried to use:
if (Page.IsPostBack) PageSetup();
But nothing happens, it just not firing the .Click event either.
Why do I have such a problem and how to fix it?
Thanks!
Please see what happens when you put PageSetup() into the Page Init procedure. Dynamically created controls are supposed to be created there.
Maybe some UpdatePanel initialization takes place earlier in the Page Life Cycle.
Also, dynamically created controls must be created each time the Page is created, even in PostBacks.

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.

User control losing state

I have a user control that I am explicitly calling from an aspx page. In page_load of the aspx page I have the following:
myControl = (DynamicTable)Page.LoadControl("../inc/DynamicTable.ascx");
Then in my code where I want it to execute the control, I have this:
pnlESDDEnrolled.Controls.Add(myControl);
where pnlESDDEnrolled is the panel I am loading it into for display.
So, I execute the aspx page, it links off to the user control, populates the control, returns back to the aspx page and the page displays with the user control in the middle of it. All is well.
The problem comes in when updates are made on the user control. Keep in mind, that other data is updated on the page as well, and the update button resides on the page, not the control. Anyway, when the update button is pushed, the button_click event is fired on the page, but the updates that I made on the user control are lost. Since the page loaded the user control and then the usercontrol executed the page unload method, the page has no knowledge of the user control anymore. Thus, when the update button on the page is pushed, I guess I am not really sure what happens with the updated data on the user control. All I know is that it is lost. I have been working on this for a huge amount of time, any help would be much appreciated
You need to check for IsPostBack in your user control, too.
As your page loads, the Page's Page_Load event starts, then your UserControl's Page_Load will begin, execute, then its events, then controls returns to the page's Page_Load, then all of its events. Of course, if you are using Pre_Init on the page or overriding OnInit in your controls, those events execute before the Page_Load.
So, if you are dynamically creating items on your UserControl, check for IsPostBack and places those events an override OnInit function in the UserControl. Then add your programmatic reference to that UserControl in the Page_Init of the page.
I guess while you're talking about user control state these are realised as Properties in the code?
If so, then you're probably going to have to use Viewstate to persist the UserControl's state so that when the page comes back, the UserControl will be re-populated first with what it previously had when it was being rendered. This is as simple as changing any public state properties to use ViewState as the backer.
I'm not a great fan of viewstate, but sometimes it has to be used.
However there may be more at work here - as your instincts are telling you, this might also be a problem with the fact that this usercontrol is loaded dynamically.
In which case, it all depends at which part of the page lifecycle you're doing it. You need to get this control into the page's tree before render (pre-render might be possible), as it's at this point that that page 'captures' its controls collection for the purposes of maintaining state.
Try and move the insertion of this control into the page structure as early as you can in the lifecycle. The earlier the better - so Page_load would be fine.
Equally - it might be you're already doing this - in which case I'd need more information (perhaps a reproducable scenario) in order to help fix it.

Categories

Resources