I have an asp.net page for a simple blog which looks like
<asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine">Default Text</asp:TextBox>
<asp:LinkButton ID="lbSend" runat="server" Text="Send" OnClick="lbSend_Click" />
<script type="text/javascript">
//some javascript to click btnSave in update panel for every five minutes.
</script>
<asp:UpdatePanel ID="upSave" runat="server" Style="display: none;">
<ContentTemplate>
<asp:Button ID="btnSave" runat="server" Text="Button" OnClick="btnSave_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
And in the code behind
protected void lbSend_Click(object sender, EventArgs e)
{
//Gets UserID from session and saves it to table A with txtContent
}
protected void btnSave_Click(object sender, EventArgs e)
{
//Gets UserID from session and saves it to table B with txtContent
}
Everything works okay. I fill the form and insert the content into tables with lbSend or btnSave. JavaScript is also clicking btnSave. However when it takes long to fill txtContent, about 40 minutes or more, the page is like expired. Clicking lbSend refreshes the page in an asynchronous way and everything you have written is gone. It is like the page have sleeped and wakes ups when I click. And lbSend works normal when you click it again.
What is the reason of this "sleep" altough btnSave continues to communicate with server? And how can I prevent it.
You are doing an Ajax post. Since it is an asynchronous post your browser happily moves on and you don't notice a problem until there is an error. If it was not Ajax then the whole page would have refreshed and you would have seen an error - assuming you display errors in a friendly format.
While implementing Ajax you should implement your own error handler so you can display to the user that an error has occurred. Also, you should implement a floating div or dialog that prevents the user from using your web page until the Ajax request has completed. I realize 40minutes is really really bad for a simple insert but I will leave that for your DBA to fix.
Let's talk about the floating div or dialog or "modal popup". You want to open it in the before_Ajax_send function and close it in your after_Ajax_receive function. Please refer to your Ajax library documentation for more information on these functions.
When you implement this dialog box it will enhance your user experience because it will let the user know that your application is doing something so please hold your horses.
Hope this helps.
Related
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'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.
I'm trying to fire an event when a user clicks a hyperlink. But it would complain that the event is not defined:
<asp:HyperLink ID="HyperLink1" onmouseover="btnSubmit_Click" runat="server">www.google.com</asp:HyperLink>
<asp:Button
id="btnSubmit"
Text="Submit"
Runat="server" />
protected void btnSubmit_Click(object sender, EventArgs e)
{
btnSubmit.Text = "clicked a link!!!";
}
I see several problems.
You do not have any sort of click event setup on your hyperlink. You do have a "onmouseover" but based on MSDN's documentation there is no click event for that control.
You have a button defined, but no events associated with that button.
You have a function that appears to be an event handler, but the naming convention suggests that it is associated with the button that has no events.
Can you provide more detail of what you are trying to do? I assume the c# code you have posted resides in the code behind?
Update:
Try changing your code to this -
<asp:LinkButton ID="lb_Link" OnClick="btnSubmit_Click" Text="www.google.com" runat="server" />
Obviously this will not redirect you, but based on what your code does, it doesn't sound like you want a redirect...
The event you're trying to trigger is a server side event. You need to use client side code for what you want to do. Plus, there is no property known as onmouseover, you can add it as a client side event from code behind
HyperLink1.Attributes.Add("onmouseover","yourClientFunction");//this can be done in page load
Note: I am brand new at ASP.NET. I'm actually going to training on Monday, but we have a hot project that required me to dive in and get done as much as I can.
I've got a textbox on my page, and I would like it to call an event on the server whenever it is changed. I don't care if "changed" means changed and loses focus, or just whenever a keyup happens. Either case will work.
The issue is that, for some reason, whenever I change my textbox and then remove focus, the server-side method is not getting called. Viewing through the debugger in Chrome, I don't even see any sort of AJAX call being made that would inform the server that a textbox was changed.
My Code:
ASCX File
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="tempTagBuilder" runat="server"
CssClass="depBuilder_tempTagBuilder"
ontextchanged="tempTagBuilder_TextChanged" AutoPostBack="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
ASCX.cs File
//whenever the text is changed
protected void tempTagBuilder_TextChanged(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Hit");
}
Anyone have a good idea of what my issue might be?
Update 1:
I got it working (somewhat). I had to go into the updatepanel's properties and add the textchanged event to the triggers collection. However, now whenever it sends the update it is emptying out the other textboxes on my page!
It doesn't fire because it's in an update panel most likely. Seems the updatepanel requires you to set a trigger for the event.
Problem with textbox inside updatepanel - not causing OnTextChanged event
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