I'm new to ajax and using visual studio 2005 and framework 2.0. A simple Ajax sample always takes to the page load event for a button click. No deployment and all just running in debug mode takes me to the Page_Load event. Don't know what is the problem? I have checked the values of UpdatePanel1.IsInPartialRendering and ScriptManager1.IsInAsyncPostBack which is false.
Here is my code,
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" >
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
<asp:Button ID="Button1" runat="server" Text="PostBack" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString()+UpdatePanel1.IsInPartialRendering+ScriptManager1.IsInAsyncPostBack;
}
Google and stackoverflow doesnt help me so far. So any kind hearts help me...
Control Inside Update Panel Cause asynchronous postback.
What is Asynchronous Postback?
From Refrence To MSDN
An asynchronous postback behaves much like a synchronous postback. All the server page life-cycle events occur, and view state and form data are preserved. However, in the rendering phase, only the contents of the UpdatePanel control are sent to the browser. The rest of the page remains unchanged.
Now if it cause all event on server than what is the use of Partial Rendering...
Partial-page rendering removes the need for the whole page to be
refreshed as the result of a postback. Instead, only individual
regions of the page that have changed are updated. As a result, users
do not see the whole page reload with every postback, which makes user
interaction with the Web page more seamless
I'm a bit rusty on my Web.forms, but as I recall, that is by design. The Page_Load does fire when an AJAX update panel sends a request to the server.
Related
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel2" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Button ID="btnBlock" Text="BlockCalls" runat="server"
OnClick="btnBlock_Click" Enabled="True" Width="100px" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnBlock" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
When i click on btnBlock pageload even fires. I don't want page refresh when user clicks on button
So, If you put the trigger on button click it is normal when you click it to postback and refresh the page.
If you want to postback without page refresh you should have something like this:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel2" runat="server">
<ContentTemplate>
<asp:Button ID="btnBlock" Text="BlockCalls" runat="server"
OnClick="btnBlock_Click" Enabled="True" Width="100px" />
</ContentTemplate>
</asp:UpdatePanel>
Either way your Page_Load event will be fired, but you can do this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//this is page load
}
else{
//this is PostBack
}
}
You still ALWAYS get a partial page post-back, and the page load event DOES fire each time with update panel - just like it does for any button outside of the update panel.
So, your page load event should have IsPostBack = false for first page setup code.
So code inside IsPostBack=False code stub only runs first time.
So, while the whole page will not re-fresh, and whole page is NOT re-posted? You still get what is called a "partial" page post back - and while only stuff inside panel is posted back to the server? Standard form events STILL fire each time including page load.
You need to build that page to handle post backs, and introduction of a update panel does NOT fix this requirement.
If your page can't survive a post-back, then it will not handle a update panel. So, a update panel does NOT prevent a post-back, but results in what we call a partial post-back.
So, say if you pop up say a jQuery.UI (or bootstrap) dialog, and do a post-back? Then that dialog will get blown up and out due to the page post back. As noted, update panels STILL do a post-back, but not a full page, and hence the term partial post-back, and thus any server side button or whatever inside of that panel that does a post back? Well, the page load event still fires each time.
So, while some jQuery and some controls can (and will) go zonkey with a full page post back? And using a update panel can help? Keep in mind that server side events in that update panel still result in post-backs - just not full page ones.
I plan on instantiating objects (classes) to do a lot of math calculations based on a user's input. My gut feeling is that I can load an animated wait-GIF on an output URL (page), then define the new class, set the parameters, start a thread, and then reload the blank page when done. Wouldn't ASP.NET already have things like animated wait GIFs already bundled? Also, by default, wouldn't GC (garbage collection) be done on the closed object (class) when done? This way, the ASP server would merely have multiple threads if there were multiple users. Just some questions, since most of my experience is WinForms.
Yes that is right. Do all the processing inside the button_click. A class is being instantiated for you in the page. Asp.net will handle all the threading for simultaneous users.
While you haven't mentioned what kind of web-framework you are using, personally i would perform this type of calculation behind an AJAX request. Use an and an control to provide feedback to the user (wait-GIF animation) until the request has completed. These are features that are already bundled in a standard ASP.NET site. Do NOT instantiate your own threads in an ASP.NET application. ASP.NET is already multi-threaded and is able to handle simultaneous requests to your application. Your sample HTML code might looks something like this:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
html code goes here. somewhere you might have a button which triggers a postback that will trigger the start of your math computations...
<asp:LinkButton ID="Button1" runat="server" OnClick="Button1_Click">Start Computations</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
somewhere else on the page you include your updateprogress tag. You can see that i've associated this tag with the update panel id that is triggering the postback.
<asp:UpdateProgress DisplayAfter="100" runat="server" ID="udp" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
put your animated gif here using an <img /> tag
</ProgressTemplate>
</asp:UpdateProgress>
Use a server control to re-display your computed text back on the page when it's been calculated.
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label id="lblResults" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
and the code behind for your button click
protected void Button1_Click(object sender, EventArgs e)
{
//do your computations here
lblResults.Text = "the answer";
UpdatePanel2.Update();
}
Good luck!
I have a C# .net 4.0 website project with a fairly complicated filtered search page on it. There are multiple UpdatePanels that are added within a Repeater. When one UpdatePanel does a postback - all the other UpdatePanels also postback at the same time.
This becomes a problem because there can be lots and lots of UpdatePanels dependent on the number of items the user chooses to view. I know UpdatePanels are not ideal - I didn't write this but have to try and fix it quickly!
There is LandingPage that holds an UpdatePanel with a Repeater control inside. Within the repeater is a user control called Article. The Article control contains some HTML and a second user control called Save. The Save control has an UpdatePanel too.
The problem I have is that only the first btnSave event gets raised. So if I click "btnSave" it works but all subsequent button click events do not fire.
I have also noticed that ALL instances of the UpdatePanel in the Save control postback at the same time - is this normal?
So a simplified view of the page is like so:
LandingPage.aspx
<asp:UpdatePanel ID="pnl" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Repeater ID="resultsRep" runat="server">
<ItemTemplate>
<uc:Article id="Article1" runat="server" />
</ItemTemplate>
</asp:Repeater>
<asp:Button id="btnLoadMore" runat="server" Text="Load More" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnLoadMore" EventName="Click" />
</Triggers>
<asp:UpdatePanel>
Custom User Control "Article"
<asp:PlaceHolder ID="ArticlePanel" runat="server">
<!-- Assorted HTML stuff here -->
<uc:Save id="Save1" runat="server" />
</asp:PlaceHolder>
Custom User Control "Save"
<asp:UpdatePanel ID="ctl" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="True">
<ContentTemplate>
<asp:LinkButton ID="btnSave" runat="server" OnClick="btnSave_Click" CausesValidation="False" Text="Save" />
</ContentTemplate>
</asp:UpdatePanel>
Thanks in advance as always.
EDIT
After further investigation, using Firebug console I found that the subsequent postbacks don't occur because the following error is thrown:
505|error|500|Invalid postback or callback argument.
Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page.
For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.
If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.|
So it seems somehow I am posting back something dodgy?
The problem was down to the use of nested UpdatePanels. I don't know where the error itself actually comes from but after trial and error I figured out that the parent UpdatePanel wasn't configured properly.
The parent UpdatePanel should have been like so :
<asp:UpdatePanel ID="pnl" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
The difference being UpdateMode="Always" and ChildrenAsTriggers="true". The UpdateMode tells the parent UpdatePanel to refresh when any of the children reload. The ChildrenAsTriggers attribute allows child UpdatePanels to cause the refresh of the parent.
So now it works - mostly. I still have the issue of every single UpdatePanels posting back everytime. Its really inefficient but I can't seem to stop it.
In my project I have placed a dropdownlist in an updatepanel.what I wanted to do is to select a value from dropdownlist and use it in a session.
but whatever I do, it will always give me null value because of not checking "Enable AutoPostBack".and when I do this, it will refresh the page so this isn't what I wanted.
It sounds like you may not be using the UpdatePanel feature properly. If you have the UpdatePanel set to update when children fire events, only the UpdatePanel should refresh, not the entire page. The code below seems to behave similar to what you are seeking. When changing the drop down, only the update panel posts back to the server and when you refresh the page, you can get the value out of the session.
ASPX CODE
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
Current Time: <asp:Label ID="lblTime" runat="server" /><br />
Session Value: <asp:Label ID="lblSessionValue" runat="server" /><br />
<br />
<asp:UpdatePanel ID="upSetSession" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlMyList" runat="server"
onselectedindexchanged="ddlMyList_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem>Select One</asp:ListItem>
<asp:ListItem>Maybe</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlMyList"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
this.lblTime.Text = DateTime.Now.ToShortTimeString();
if (Session["MyValue"] != null)
this.lblSessionValue.Text = Session["MyValue"].ToString();
}
protected void ddlMyList_SelectedIndexChanged(object sender, EventArgs e)
{
Session.Remove("MyValue");
Session.Add("MyValue", this.ddlMyList.SelectedValue);
}
In order to get anything stored to Session, you have to submit it to the server.
Perhaps some more details on why you don't want the UpdatePanel refreshing would be helpful, and what you are trying to accomplish using the value in Session.
EDIT: Based on your comments, it seems to me that the solution would be to store the current .ascx file in Session, and set your DropDownList to have autopostback enabled.
So, on your handling of the "Next" and "Back" buttons, store an indicator for the correct .ascx to Session.
During your postback handling of the dropdownlist event, you could simply ensure that the current .ascx file is still being shown, by checking session for the correct file to show. When the result is returned to the client, nothing will appear to have changed, because the UpdatePanel is smart enough to realize it's the same content, and you will have successfully dealt with the dropdownlist value.
It sounds like you're doing way more work than you need to here. Have you looked into using an ASP.NET Wizard Control? http://msdn.microsoft.com/en-us/magazine/cc163894.aspx or just Google it.
If you still want to do it your way, you have to submit to the server (either with no autopostback + manual submit button click, or by enabling autopostback) since the Session is a server-side concept. HTTP is a stateless protocol, so the only concept of state has to be done outside of HTTP's domain. This means you're stuck storing state on the server (for instance, in the session) or, much more restrictively, on the client's computer (such as in a cookie).
thanks a lot I solved problem by controlling variables in Page_Load event.
If Label1.Text = 1 Then
Dim tempcontrol2 As Control = LoadControl("Page1.ascx")
PlaceHolder1.Controls.Add(tempcontrol2)
ElseIf Label1.Text = 2 Then
Dim tempcontrol2 As Control = LoadControl("Page2.ascx")
PlaceHolder1.Controls.Add(tempcontrol2)
End If
thank u for all answers
I have a couple of dropdown boxes on a normal ASP.Net page.
I would like the user to be able to change these and to have the page Pseudo-post back to the server and store these changes without the user having to hit a save button.
I don't really need to display anything additional as the dropdown itself will reflect the new value, but I would like to post this change back without having the entire page flash due to postback
I have heard that this is possible using AJAX.Net...
Can someone point me in the right direction?
Add a reference to System.Web.Extensions and System.Web.Extensions.Design to your website. Then put a scriptmanager on your page and wrap your ddl in an updatepanel. Do whatever you want on the back-end.
For example...
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="yourDDL_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
protected void yourDDL_SelectedIndexChanged(object sender, EventArgs e)
{
// do whatever you want
}
Depends upon your Ajax Framework, Ra-Ajax have a sample of that here...