I have a label and timer inside update panel which is also inside script manager. The timer interval was 1000 milliseconds. The label must return the current system date and time and it should be ticking. I used below codes and its working. It returns the current date and time and its ticking too, but the problem is the second/s that ticks skips some seconds. For example: the current time is 09:11:17, after a tick it becomes 09:11:20, it skips 2 seconds.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Label2.Text = Date.Now.ToString
End If
End Sub
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Label2.Text = Date.Now.ToString
End Sub
What must be the problem in this case?
Thanks in advance.
Related
I've pasted my code below. What I'm trying to do is filter a set of data using the day. The 'day' field is stored as the abbreviated day name, so "thu" for Thursday etc. This works perfectly fine, except once you've changed the date filter to something else, the option for the current day doesn't work.
For example, today is Sunday. When the page loads, it filters out all listings that are not for Sunday, as it should. On a small group of LinkButton controls, if you click any other day of the week, it will change and show the option for the selected day, again, as it should do. However, when you try and set the filter to Sunday again, or click the "today" button, it won't change.
Based on my code, can anyone see what is wrong? If it is of any help, my data is on a remote SQL server.
VB Code:
Dim DateVal As Date = DateTime.UtcNow()
Dim DateName As String = DateVal.ToString("ddd")
Protected Sub btnMon_Click(sender As Object, e As EventArgs) Handles btnMon.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Mon"
End Sub
Protected Sub btnTue_Click(sender As Object, e As EventArgs) Handles btnTue.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Tue"
End Sub
Protected Sub btnWed_Click(sender As Object, e As EventArgs) Handles btnWed.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Wed"
End Sub
Protected Sub btnThu_Click(sender As Object, e As EventArgs) Handles btnThu.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Thu"
End Sub
Protected Sub btnFri_Click(sender As Object, e As EventArgs) Handles btnFri.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Fri"
End Sub
Protected Sub btnSat_Click(sender As Object, e As EventArgs) Handles btnSat.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Sat"
End Sub
Protected Sub btnSun_Click(sender As Object, e As EventArgs) Handles btnSun.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Sun"
End Sub
Protected Sub btnAll_Click(sender As Object, e As EventArgs) Handles btnToday.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = DateName
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = DateName
End Sub
TSQL (If it helps)
SELECT Schedule.SlotID, RIGHT('00' + CONVERT(VARCHAR(2), Schedule.StartHr), 2) + ':' + RIGHT('00' + CONVERT(VARCHAR(2), Schedule.StartMin), 2) AS StartTime,
Schedule.ProgrammeID, Schedule.Day, RIGHT('00' + CONVERT(VARCHAR(2), Schedule.EndHr), 2) + ':' + RIGHT('00' + CONVERT(VARCHAR(2), Schedule.EndMin), 2)
AS EndTime, Programmes.ProgrammeName, Programmes.Description, Programmes.PresenterID, Presenters.PresenterName, Presenters.PhotoURL
FROM Presenters CROSS JOIN
Programmes CROSS JOIN
Schedule
WHERE (Schedule.Day = #FiltDay)
maybe it is not 100% related to your issue but you should not handle the datasource initialization on the event of a different control.
your need is to fill the sql parameter so you should handle the Selecting event of the datasource.
on click of your buttons set the proper value for your variable DateName and on selecting of the datasource fill the sql parameter using your variable.
acting this way you can easily spot the actual value fed into the call and identify the issue.
I have a button that sets a session variable when clicked. But for some reason, I have to click it twice in order for the save to actually happen. Is there anyway around this?
Thanks
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If CInt(Session("save")) <> 1 Then
'save something ...
End If
End Sub
Private Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
Session("save") = 1
End Sub
Page_Load runs before btnSave_Click. You can see more information about the ordering of events in MSDN.
In other words, when btnSave is clicked, the postback runs the Page_Load then the btnSave_Click method. To fix this problem, move the code 'save something ... into the btnSave_Click method.
I know this is a very basic question, but I cannot find the answer. There are lots of web pages that say page.init does not fire on a post back e.g. here: http://www.dotnetfunda.com/interview/exclusive/x3224-what-is-the-difference-between-the-pageinit-and-pageload-events.aspx. Please see the code below:
Public Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
MsgBox("Test Init") 'Line 5
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = True Then
MsgBox("PostBack") 'line 9
End If
MsgBox("Test Load")
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "Hello"
End Sub
End Class
The message box on line 5 and line 9 fire every time I click the button. This means that the Init event is fired on a postback. I have obviously forgotten something very basic.
From your reference page:
When you postback to any page, the Page_Init event doesn't fire.
This is totally wrong.
Page_Init is always fired - actually the page cycle is not change at all.
I have a label's value that is posted back from a previous page and I want to use this label to do a simple calculation in the current page, but when I do the calculation the page (refreshed) the value of this label and automatically deleted the value (Since there would be no value in postback when it refreshed).
Here is the code behind file:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label2.Text = Request.Form("Hidden1")
End Sub
and here where I want to use the label
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Stotal As String
Stotal = Val(Label2.Text) * 10
Label3.Text = Stotal
End Sub
How can I save the value in the page via view state or any other method? Thanks in advance
Unless you've disabled ViewState on your page, the problem isn't that your label's ViewState isn't being saved, it's that you're overwriting it in your Page_Load method on the postback since the form variable Hidden1 is no longer being posted to your page from the previous page. Try:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack
Label2.Text = Request.Form("Hidden1")
End If
End Sub
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = True Then
Session("x") = "ABC"
End If
End Sub
Protected Sub btnABC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnABC.Click
Session("x") = "ABC"
End Sub
Protected Sub btnCBA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCBA.Click
Session("x") = "CBA"
End Sub
Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
TextBox1.Text = Session("x")
End Sub
End Class
Three buttons-ABC,CBA and Show.
if you click on ABC and then Click on Show button The textbox shows "ABC"
but when I Clicking on CBA button And then Click on Show button The textbox shows again "ABC". IsPostback property will true on each time the page is posted to the server.
So the session reset the value.
how to overcome this issue ????
If you set the value in page_load(), this assignment occurs every time you load the page. Maybe you want to set this value only at the first call of the page:
If IsPostback = False Then
Session("x") = "Something"
End If
The second load of the page will not overwrite the value you set in button1_click.
When you press the show button it causes a postback to the server. The Page_Load method fires first, and you assign "ABC" into Session("x"). Then you're putting Session("x") into into the textbox.
What you'd probably want is this instead:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Session("x") = "ABC"
End If
End Sub
Besides what other people wrote above, I also recommend you to assign Session values during Page_InitComplete event. Because mostly developers work in Page_Load stage and some times assigning Session values as well may throw errors because of it. It is like which one is happening before or after. You can make mistakes and so on.