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..
Related
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>
I have this code in form:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="Text1" runat="server"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="Text2" runat="server"></asp:TextBox>
<asp:Button ID="Submit" runat="server" OnClick="Submit_Click"
Text="Click Me"/>
</ContentTemplate>
</asp:UpdatePanel>
When I click on Submit button, the all form is posted back on server (Text1 and Text2). I want to post back only textbox in UpdatePanel (Text2). Is it possible?
Thank you for responses.
EDIT (Solution):
I think, I have solution using Javascript:
function onSubmit() {
var text1 = this.document.getElementById('<%=Text1.ClientID%>');
text1.disabled = true;
return true;
}
....
<asp:Button ID="Submit" OnClientClick="onSubmit();" ..../>
It cause that Text1 will be disabled before PostBack. It seams that ASP.NET do not post back values from disabled textboxes. Hovewer, this works only without UpdatePanel. Of course, Text1 is empty after postback.
Well, the UpdatePanel controls what get's posted back. You cannot change that behavior. It should be posting the form data for the UpdatePanel only (I believe), plus the entire ViewState. It has to post the entire ViewState for the page lifecycle to work.
I'm trying in ASP.NET to use a HTTPHandler to display an image and update every 5 sec.
This httphandler simply renders the current time into a Bitmap.
In aspx side, the Image is inside a AJAX UpadtePanel, and I'm using a timer to refresh the image every 5 sec.
My problem is that :
in IE9, the image is not updated at all. My HttpHandler is requested only once.
in chrome, the image is updated but it "blinks", ie every 5s it is cleared for few secs then displayed, then cleared again etc...
For debugging purpose I have also added inside the UpdatePanel a label, filled by a random value. It is well updated in both web browsers.
Here is the code in my aspx page :
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer runat="server" ID="UpdateTimer" Interval="5000" OnTick="UpdateTimer_Tick" />
<asp:UpdatePanel runat="server" ID="TimedPanel" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UpdateTimer" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Image ID="TheImage" alt="httpHandler" src="getImage.ashx?id=1" runat="server" />
<asp:Label ID="Label" runat="server" Font-Size="XX-Large" ></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
In my HttpHandler, I did not forget to disable caching like this :
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetNoStore();
cache.SetExpires(DateTime.MinValue);
And to prevent my IE9 web browser caching the image, I also tried to change ImageUrl each time the timer is invoked in the aspx.cs file, like following. But my HttpHandler is still called only once when the page is displayed for the first time :
protected void UpdateTimer_Tick(object sender, EventArgs e)
{
Label.Text = ((System.Environment.TickCount / 100.0) % 360).ToString("F2");
TheImage.ImageUrl = "getImage.ashx?id=" + System.Environment.TickCount.ToString();
}
I hope my problem is well explained.
What do I do wrong?
Thanks in advance!
Some suggestions:
Add Enabled="True" to your timer.
Add the source code to your getImage.ashx to your post.
Use the asp.net tags like ImageUrl in your <asp:Image /> instead of the native html tags.
Use a div instead of an img (i.e. <asp:Panel />)
change the update mode of your UpdatePanel to Always
<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?
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.