I have a page that above it I want to have a panel that automatically refresh every 5 minutes, and if a user has a message in his inbox show to him. What is the best solution for this?
Should I usel AJAX, jQuery, or JavaScript? My preferred solution is server side solution.
Since you are working with ASP.Net, you can also achieve this behavior by using a combination of the following:
ScriptManager or ScriptManagerProxy (if you have nested pages):
Manages the ajax calls
UpdatePanel: Determines what gets updated. Controls nested within <ContentTemplate> are subject to partial
updates
Triggers: Controls when the content is updated.
For your purpose, a Timer control can be used as a trigger to ensure that partial postbacks are triggered every 5 seconds:
<asp:ScriptManager ID="scriptManagerMain" runat="server"/>
<asp:Timer ID="timer" Interval="5000" runat="server"/>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Panel ID="panelToBeUpdated" runat="server">
<asp:Label ID="lblContent" runat="server" ></asp:Label>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer" />
</Triggers>
</asp:UpdatePanel>
I would use Jquery to send an AJax request to the server to fetch the updated content. Upon receiving the content I would use JQuery again to update the markup.
You can set this up to trigger every 5 min using setInterval in Javascript
Hard to give a specific example without code. But you basically need to load the new content with an ajax request that is triggered by a timer.
setInterval(function(){
$.ajax({
url: "someUrlThatServesUpdatedContent.aspx",
cache: false
}).done(function( html ) {
$("#results").html(html);
});
}, 300000);
The above is just a simple example to point you in the right direction.
Edit: Here is an example of how to do the ajax call without JQuery
https://stackoverflow.com/a/2792721/1059001
It's possible to do that with AJAX using method described by previous answers, but if you wish to have a server side solution I would recommend loading that part of the page in an iframe, with meta refresh:
<meta http-equiv="refresh" content="300">
This method however would make it difficult to communicate any events or user actions back to main page.
Related
I m new to .net, When i workout particular script in visual studio, I got the warning message like this:
Cannot unregister UpdatePanel with ID 'UpdatePanel1' since it was not registered with the ScriptManager. This might occur if the UpdatePanel was removed from the control tree and later added again, which is not supported.
Parameter name: updatePanel
.aspx file:
<asp:UpdatePanel ID="UpdatePanel5" runat="server"> <ContentTemplate>
<asp:Label runat="server" ID="lblInboxCount" CssClass="inboxCount"
Text="Inbox (0)"></asp:Label> </ContentTemplate> </asp:UpdatePanel>
this code for master page:
<asp:ScriptManager ID="ScriptManager2" runat="server">
</asp:ScriptManager>
May i know, what is ScriptManager and UpdatePanel? and what is the function in asp .net?.. I referred msdn and other some materials, but still i didn't get it.
can anyone help me to fix this?
Thanks,
The UpdatePanel is used to perform partial page refreshes. It will AJAX'ify controls contained within it, allowing partial rendering of the area.
When you use an UpdatePanel, you also need to include a ScriptManager. The ScriptManager manages the Ajax script libraries and script files. It also manages partial-page rendering, and client proxy class generation for Web and application services.
You have to add something like this in your ASP page:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
And something similar for UpdatePanel:
<asp:UpdatePanel id="UpdatePanel1" runat="server">
<contenttemplate>
</contenttemplate>
</asp:UpdatePanel>
UpdatePanel is used to prevent page refresh after a postback that occurs when you for example press a button. You just refresh that particular part of the page instead of the whole page.
I'm using CodeMirror in an ASP.NET web application. The web app uses UpdatePanel (ajax).
In ajax postback, I'm not able to get updated text from the CodeMirror textbox on server side and after the postback, the text gets reset. This WORKS if I don't use an update panel. What am I missing?
Below is the code mirror code:
editor = CodeMirror.fromTextArea(document.getElementById("<%=txtLua.ClientID%>"), {
matchBrackets: true,
theme: "neat",
pollInterval: 100,
continuousScanning: 500
});
<asp:UpdatePanel ID="upd" runat="server">
<ContentTemplate>
<asp:TextBox runat="server" ID="txtLua" Height="320" Width="600" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClick="btn_Click" Text="btn" />
</ContentTemplate>
</asp:UpdatePanel>
Is there an asp.net/C# sample for using CodeMirror? Any help is appreciated.
The short answer is: create a javascript event hook that fires early (before the UpdatePanel begins to do its work) and manually calls CodeMirror's .save() function.
The problem seems to arise because the auto-magic form.submit override that CodeMirror supplies is triggered after the ScriptManager has already passed the ViewState of the panel back to the server. This means the server only receives the TextBox in its initial state (CodeMirror hasn't put what you've typed into it yet). I checked the DOM events in Chrome and ScriptManager's hook was consistently ahead of the form.submit override that CodeMirror added.
I got around this by adding an .onclick to the submit button right after CodeMirror loaded. Using your example:
var editor = CodeMirror.fromTextArea(document.getElementById("<%=txtLua.ClientID%>"), {
leaveSubmitMethodAlone: true, //since you don't need this anymore, no reason to complicate your DOM
matchBrackets: true,
theme: "neat",
pollInterval: 100,
continuousScanning: 500
});
window['cmLocalStateEvent'] = function () { editor.save(); };
//saveButton = document.getElementById("<%=btn.ClientID%>"); //grab the save button
//if (saveButton) {
// saveButton.onclick = function () { editor.save(); }; //make it do what the submit action was going to do
//}
<asp:UpdatePanel ID="upd" runat="server">
<ContentTemplate>
<asp:TextBox runat="server" ID="txtLua" Height="320" Width="600" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClientClick="if(window['cmLocalStateEvent'])window.cmLocalStateEvent();" OnClick="btn_Click" Text="btn" />
</ContentTemplate>
</asp:UpdatePanel>
Now the .onclick is ahead of the ScriptManager hook, and will fire first. Infact, if you put an OnTextChanged= and AutoPostBack= on the TextBox it'll fire even before the button that you just clicked does.
Essentially, the trick is to get CodeMirror's save to apply before ScriptManager submits the ViewState back to the server.
EDIT:
I've found since posting this, that you'll run into a troublesome issue if your submit button is also inside the UpdatePanel (which yours is). The .onclick will not persist after the initial submit and you'll be reset to a clean button with no event hooks. My current working solution to that scenario is adding a window['cmLocalSaveEvent'] function and adding the client .onclick to the ASP template (so the panel refresh puts it back for you). Updated code above to reflect this.
This is how I managed to make it work, just in case someone else needs it. After instantiating editor, I'm keeping the textbox aligned every time the blur event occurs.
editor.on('blur', function () {
arguments[0].save();
});
how to fill textbox in asp.net while i am typing in another text
.. changes in one textbox will affect in another textbox auto.. and without refreshing my page.
Ok, try this. You will need the AJAX Control Toolkit. So read the article Installing AJAX Control Toolkit 4 in Visual Studio 2010 to see how to install it in Visual Studio.
Then you need to add a ScriptManager to your ASPX page. You will need to add the following code:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
What you then need to do, is add an UpdatePanel to your page. Inside this update panel, you need to place the textbox. This means that only the controls inside the update panel will refresh, and not the whole page. To do this, add the following code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!--Add your Textbox Control to update here: Textbox1-->
<asp:TextBox ID="Textbox1" runat="server" ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="Textbox2" runat="server" ReadOnly="True" ontextchanged="Textbox2_TextChanged"></asp:TextBox>
</ContentTemplate>
<Triggers>
<!--This is the textbox you will be typing text into: TextBox2-->
<asp:AsyncPostBackTrigger ControlID="Textbox2" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
The Trigger tells your page which control on the form needs to initiate the postback. Now in your .cs file, you need to add the event handler for the Textbox2 TextChanged event. Add the following code:
protected void Textbox2_TextChanged(object sender, EventArgs e)
{
// Set the text of textbox1 = textbox2
}
I hope that this helps.
No need for AJAX. JQuery is enough. The code will do it
$('#text1').bind('keyup', function(){
$('#text2').val($('#text1').val());
});
Assuming
text1 id of box writing into.
text2 textbox that text gets copied to
in .Net you will have to use client id to get the correct id so it may look like this
$('<%=text1.ClientID%>').bind('keyup', function(){
$('<%=text2.ClientID%>').val($('#text1').val());
});
Oh and wrap it up in $(document).ready as per standard. And of coures you need to include the JQuery library to your page.
No posting back or page refreshes at all. It's your lightest solution and easy to implement.
You will need to use Javascript to accomplish this. ASP.Net code runs on the server side, which means it cannot affect the page without a postback happening first. Read up on the OnTextChanged event and how to hook into it with javascript. There is a javascript library called jQuery which makes everything easier, though it isn't strictly necessary.
Use JQuery. You may need to make an AJAX call to the server if you are relying on a datasource such as a database to autofil this field.
I have a controller:
<asp:Button OnClick="MyFunction" runat="server" />
I want to be able to call MyFunction without the page reloading. Is this possible with ajax or something? If so how would I do it?
Checkout the ASP.Net instructional videos. Should cover most, if not all, of your questions.
AJAX Videos: The Official Microsoft ASP.NET Site
You have to download Ajax Extensions and install it.
After you do this place instead <asp:Button OnClick="MyFunction" runat="server" /> the following
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Your Text" OnClick="MyFunction" />
</ContentTemplate>
</asp:UpdatePanel>
MyFunction would be a c#/vb function written in your page behind code, not a javascript function. I specified this because i've seen many mistakes that people make regarding this one. OnClick attribute is for c#/vb function and OnClientClick is for javascript function.
For more information regarding how to add ajax functionality to an ASP .NET Page go here
One option is to use page methods.
Add a static method to your page, decorated with the WebMethod attribute:
[WebMethod]
public static void MyMethod(string someParam)
{
}
Enable page methods in your ScriptManager:
<asp:ScriptManager EnablePageMethods="True" ... />
And then you can call it from the client side, using JavaScript (that you can wire to the OnClientClick event of your button):
PageMethods.MyMethod("some value", successCallback, errorCallback);
For more details read the "Calling Static Methods in an ASP.NET Web Page" section on this page:
http://msdn.microsoft.com/en-us/library/bb398998.aspx
Yes you can use Make Client-Side Network Callbacks with Asp.net Ajax using this function you can call your code behind methods without reloading your page. To use this methods you should write your methods as static type.
You can refer this video
http://www.asp.net/ajax/videos/how-do-i-make-client-side-network-callbacks-with-aspnet-ajax
You have one more option using the jQuery Ajax.
The simplest way is to warp your code with an UpdatePanel
There are many examples if you google it.
<asp:Button runat="server" ID="btnShowModal" Text="Show"
OnClick="btnShowModal_Click" />
<asp:Button runat="server" ID="HiddenForModal" style="display: none" />
<ajaxToolKit:ModalPopupExtender ID="Modal1" runat="server"
TargetControlID="HiddenForModal" PopupControlID="PopupPanel" />......it may help u
I have the following:
<asp:UpdatePanel ID="upd" runat="server">
<ContentTemplate>
<script> alert("execute again"); </script>
<asp:LinkButton ID="go" runat="server" Text="Exec JS" />
</ContentTemplate>
</asp:UpdatePanel>
The first time the page renders the script is executed. If I click the button to cause a postback it doesn't execute again.
Is there a way to make it execute the scripts again?
Yo! There's an easy way to get the JavaScript to run again. Put the javascript on the HeadContent portion of your page:
<script>
function BindIt()
{
alert("execute again");
}
</script>
then on your page inside the update panel add portion with a call up to the function that you created for your javascript:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<script type="text/javascript">
Sys.Application.add_load(BindIt);
</script>
...
In a project that I was working on, I wanted all of the javascript on the page to run again, so I just put it all into a massive BindIt style function. It took me a while to figure it out and I tried many methods to get this to work but this was the only way I was ever able to get jquery to work with updatepanels
Since ASP is just pushing the raw data, not actually executing it you'll need to probably use the RegisterStartupScript method of the ScriptManager to programmatically inject the script to load after a partial postback.
EDIT
More directly, try looking in to ScriptManager.RegisterStartupScript() for UpdatePanels. e.g.
If the JavaScript is jQuery related, following link would definitely help:
http://weblogs.asp.net/hajan/archive/2010/10/07/make-them-to-love-each-other-asp-net-ajax-updatepanels-amp-javascript-jquery-functions.aspx
I have personally used the 2nd method mentioned in this article, and it worked for me properly.