How to not focus when using Ajax Timer Control in asp.net - asp.net

when i using ajax control but control tick every 1 secound and when i click other control the control not focus in selection my control when timer tick

That particular timer must be placed inside the update panel.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>

Here's another way to do it:
http://mattberseth.com/blog/2008/05/bug_bash_enablingdisabling_the.html
This is how I approached it (note that the server-side tags for Timer1 and the "txtSymbol" textbox are approached differently):
<asp:Content ID="HeadContent" runat="server" ContentPlaceHolderID="HeadContent">
<link rel="stylesheet" type="text/css" href="/Website1/Styles/lessantique.css" />
<script type="text/javascript">
function pageLoad(sender, args){
// fetch the timer components
var timer = $find('<%= this.Timer1.ClientID %>');
// fetch the INPUT element
var textbox = $get('Content1_txtSymbol');
$addHandler(textbox, 'focus', Function.createDelegate(this, function(){
// disable the Timer so we don't refresh the page
// while the user is entering the data
timer._update(false, timer.get_interval());
}));
$addHandler(textbox, 'blur', Function.createDelegate(this, function(){
// re-enable the Timer
timer._update(true, timer.get_interval());
}));
}
</script>
</asp:Content>
regarding the server side code for the timer listed in the previous response, that approach did not work for me since I have more than one update panel, where the text box is not in the same panel that uses the timer. In that case I followed the advice at http://forums.asp.net/t/1632054.aspx/1, where a new panel is implemented, as such:
<asp:UpdatePanel runat="server" ID="UpdatePanel3" UpdateMode="Conditional" ChildrenAsTriggers="false" >
<ContentTemplate>
<asp:Timer runat="server" id="Timer1" Enabled="true"
Interval="2000" OnTick="Timer1_Tick" />
</ContentTemplate>
</asp:UpdatePanel>
Despite the above not producing errors, the results were still unsatisfactory, therefore the client-side approach.

Related

update label inside updatepanel in asp.net

i need to be able to update the label text whenever i want without clicking on a button or something similar to that. the update happens based on server-client logic. i tried to use java script, but when i call the script from a function i wrote it doesn't work. now i'm trying to use updatepanel and no luck so far. i tried to use
UpdatePanel1.Update();
but it didn't refresh the label. the label defined inside the updatepanel:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label3" runat="server" Text="Label" AccessKey="l"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
is there a away to force it to refresh?
If you just want to update your label from javascript you can try this function:
<script type="text/javascript">
function updateLabel(newValue) {
$('#<%= Label3.ClientID %>').val(newValue);
}
</script>

why ajax loader image always shown using jquery and asp.net

with asp.net code below my ajax_loader image doesn't work well and always shown ..
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="lblname">
Name</asp:Label><br />
<asp:TextBox runat="server" ID="txbname"></asp:TextBox><br />
<asp:Label ID="lblemail" runat="server">
Email</asp:Label><br />
<asp:TextBox runat="server" ID="txbemail" /><br />
<asp:Label runat="server" ID="lblsugg">
Suggestion</asp:Label><br />
<asp:TextBox runat="server" Rows="3" Columns="20" ID="txbsugg" TextMode="MultiLine"></asp:TextBox>
<asp:Button runat="server" ID="btnsubmit" OnClick="btnsubmit_onclick" Text="submit" />
<asp:Label runat="server" ID="lblresultmsg"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<div id="loading">
<p>
<img src="Images/ajax-loader.gif" />
Please Wait</p>
</div>
and jquery code
$("#loading").ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide();
});
any suggestions !?
ajaxStart and ajaxStop works only for ajax request sent by jQuery, if you use other libraries or UpdatePanel, it won't help you.
jQuery only.
Whenever an Ajax request is about to be sent {With jQuery-gdoron}, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() method are executed at this time.
Example of jQuery ajax request that will fire the ajaxStart and ajaxStop:
$.ajax({
url: 'foo',
...
...
});
You could create a generic way to handle this by adding the following code to a common js include. Here's a quick and dirty example:
Note: Be sure you initialize it by calling SetupGlobalAjaxHandlers on your page load.
function SetupGlobalAjaxHandlers()
{
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
}
function InitializeRequest(sender, args)
{
if(typeof(OnPageInitRequest) != "undefined")
OnPageInitRequest();
}
function EndRequest(sender, args)
{
if(typeof(OnPageEndRequest) != "undefined")
OnPageEndRequest();
}
Then, in any page that includes the js file, you could optionally implement the OnPageInitiRequest() and OnPageEndRequest() methods. There, you could show/hide your loading indicator.
I do recommend, though, that you use an UpdateProgress control as you get the show/hide logic for free.
This technique opens up some possibilities for you, say, if you wanted to disable controls while a partial postback is occurring.

Timer Tick in Asp.net?

whats is the equivalent of these in asp?
it doesn't work for me!
lbl_date.Text = FormatDateTime(Now, DateFormat.LongDate)
lbl_time.Text = FormatDateTime(Now, DateFormat.LongTime)
lbl_date.Text = DateTime.Now.ToLongDateString();
lbl_time.Text = DateTime.Now.ToLongTimeString();
The thing to remember here is that all your vb code does is generate an html document, and nothing else. Once that job is done, the page class you're working in is even sent off to the garbage collector and the processor thread you were in is repurposed to serve another http request. Therefore trying to set a label based on a timer event is just plain nuts — your timer will likely be disposed before it ever has a chance to tick.
Instead, you want to do this particular job in javascript. Look into javascript's setTimeout() method.
There is no Timer control in ASP.NET as such, but AJAX timer.
In that case you have to put these labels inside AJAX update panel as follows
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="2000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="lbl_date" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="lbl_time" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
and in code behind for Timer1_Tick event put
lbl_date.Text = FormatDateTime(Now, DateFormat.LongDate)
lbl_time.Text = FormatDateTime(Now, DateFormat.LongTime)
this should work..

UpdatePanel, Timer and TexBox Problem

<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Always" >
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="300" ontick="Timer1_Tick"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
</ContentTemplate>
</asp:UpdatePanel>
Problem is that TextBox1’s cursor is not blinking it is static but you can write in it. It gives the impression that it freezed. Why cursor is not blinking?
You should set your timer interval near as 1000 milliseconds. That should solve your blinking problem.
When the post-back completes you can run a piece of JavaScript which will remove the focus from the textbox (e.g. it will no longer accept text) and then you can immediately re-focus on the textbox (so it will accept text again). This may "reset" the cursor so that it displays properly.
Try adding this to your ASPX, preferably outside of the UpdatePanels:
<script type="text/javascript">
function fixTextBoxFocus()
{
var textBox = document.getElementById("<%= TextBox1.ClientID %>");
textBox.blur(); //Remove the focus from the text box.
textBox.focus();//Re-focus on the textbox.
}
</script>
And then, in your code-behind (replace MyPage with the name of your page's class):
protected void Timer1_Tick(object sender, EventArgs e)
{
this.ClientScript.RegisterStartupScript(typeof(MyPage), "fixTextBoxFocus", "fixTextBoxFocus();", true);
}
Now, when the partial-post-back occurs, this script will be executed each time. Give it a try and let me know if it helps to fix the issue.
Placing a timer inside an updatepanel will result in a repeat refresh of that update panel. SInce you have placed the timer interval as 300 milliseconds. That may be the problem.
What you need to think about -
Do you really want to place the timer in the update panel?
Do you really need to keep the interval as 300 milli seconds.
Will it not be possible to move the textbox outside the updatepanel?

Show loading message on SelectedIndexChanged event of drop down list

I'm trying to show message "Loading..." when a user select an item in the dorp down list.
Mark up:
<asp:Label ID="lbl_LoadingMessage" runat="server" ></asp:Label>
<asp:DropDownList ID="ddl_Chapter" runat="server" AutoPostBack="True">
</asp:DropDownList>
Code behind:
Protected Sub LoadMessage()
lblLoading.Text = "Loading..."
End Sub
Protected Sub ddl_Chapter_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl_Chapter.SelectedIndexChanged
LoadMessage()
Dim redirectURL As String = "~/chapter.aspx?bid=" & BookId.ToString
Server.Transfer(redirectURL)
End Sub
The method I'm using above is not working. When I select a new item from the drop down list, it works as expected except the message "Loading..." is not showing at all. Any suggestion or code sample? Thank you.
You will have to do this on the client side using javascript.
At the moment, your dropdown menu is causing a postback. when the drop down menu is changed, the page post backs then the entire page life cycle is run through. When the event ddl_Chapter_SelectedIndexChanged is run, you set the text of the loading label, but you never reload the page (which would have your loading message) - instead you server.transfer to a new page.
If you use jQuery, you could set the labels text value as soon as the dropdown is changed
something like:
$('#the_full_renedered_ID_of_ddl_Chapter').change(function () {
$('#the_full_renedered_ID_of_lbl_LoadingMessage').html("Loading...")
});
Or use javascript:
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" /></script>
<script type="text/javascript">
$(document).ready( function() {
$("#<%= ddl_Chapter.ClientID %>").change(function()
{
window.alert("Loading");
});
});
</script>
The whole event will execute before the page re-rendering will take place.
If you are going to be doing extra processing between the LoadMessage() and the Server.Transfer try using AJAX UpdateProgress panel and adding your "loading..." message to that and add your dropDownList to a UpdatePanel.
This way depending on what code needs executing in your SelectedIndexChanged event it will show the "loading..." message before it via a partial page postback.
e.g
<asp:ScriptManager id="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdateProgress id="UpdateProgress1" runat="server" associatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<p>Loading....</p>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel id="UpdatePanel1" runat="server" childrenAsTriggers="true">
<ContentTemplate>
<asp:DropDownList id="DropDownList1" runat="server" autoPostBack="true">
<asp:ListItem selected="True" value="1">Book 1</asp:ListItem>
<asp:ListItem value="2">Book 2</asp:ListItem>
<asp:ListItem value="3">Book 3</asp:ListItem>
<asp:ListItem value="4">Book 4</asp:ListItem>
<asp:ListItem value="5">Book 5</asp:ListItem>
</asp:DropDownList>
<asp:Label id="lblSelected" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
This way the "loading..." message will be displayed for the duration of the processing of what you are trying to achieve in your SelectedIndexChanged event. If this is simply for display reasons javaScript would be your best bet.

Resources