I am attempting to construct my own date picker using code from several sources.
Why won't the calendar hide when visible?
myDate.ascx
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="myDate.ascx.vb"
Inherits="Website.myDate" %>
<asp:TextBox ID="dateText" runat="server" > </asp:TextBox>
<asp:Button ID="dateBtn" runat="server" UseSubmitBehavior="false" Text="x" />
<asp:Calendar ID="dateCal" runat="server" ></asp:Calendar>
myDate.ascx.vb
Partial Public Class myDate
Inherits System.Web.UI.UserControl
Protected Sub dateCal_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dateCal.SelectionChanged
dateText.Text = dateCal.SelectedDate ' Update text box'
dateCal.Visible = False ' Hide calendar'
End Sub
Protected Sub dateCal_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles dateCal.VisibleMonthChanged
dateCal.Visible = True ' For some reason, changing the month hides the calendar (so show it)'
End Sub
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dateCal.Visible = False ' Hide calendar on load'
End Sub
Protected Sub dateBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles dateBtn.Click
dateCal.Visible = Not dateCal.Visible ' On button press, toggle visibility'
End Sub
End Class
It won't hide when visible because Page_Load runs every time the page is loaded including postbacks (button clicks etc).
So you need to use the IsPostBack property to set the visiblity in Page_load:
if Not Page.IsPostBack then
dateCal.Visible = False ' Hide calendar on load'
end if
Sometimes getting the initialization right in page_load with IsPostBack can lead to a mass of strange conditions so use it wisely.
I think I'd be inclined to set the property in the ascx file rather than in Page_load.
First of all, if you want to toggle controls between page postbacks, you need to use ViewState. Check, is your page uses ViewState.
EnableViewState for the page must be set to true in your case.
Also, check your Page_load function.
On every loading of the page you are hiding your calendar
Page_load is calling every time before any button or calendar even rises.
So, you are changing visibility to true and then using changed visibility value in events:
dateCal.Visible = Not dateCal.Visible
That's making calendar always be visible when you are clicking on dateBtn
To make it clear to you i will order events in your code in the order they are calling:
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dateCal.Visible = False ' Hide calendar on load'
End Sub
Protected Sub dateCal_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dateCal.SelectionChanged
dateText.Text = dateCal.SelectedDate ' Update text box'
dateCal.Visible = False ' Hide calendar'
End Sub
Protected Sub dateCal_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles dateCal.VisibleMonthChanged
dateCal.Visible = True ' For some reason, changing the month hides the calendar (so show it)'
End Sub
Protected Sub dateBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles dateBtn.Click
dateCal.Visible = Not dateCal.Visible ' On button press, toggle visibility'
End Sub
Now you may see that every time when the page is loading, the page_load event is calling and hides the calendar.
You should either set Visible="false" in the ascx file for the calendar. Or call
dateCal.Visible = False
only when its first loading of the page (!IsPostback property)
so, in C# it will be
protected void Page_Load(object sender, EventArgs args) {
if (!IsPostback)
dateCal.Visible = false;
}
Related
i want to display text box value to another text box from one webpage to another in a button click.
i know the windows code But don't know the web application.
public qus as form = new question()
qus.txtname=txtname.text
On first page add button click event
Sub Btn_Click(sender As Object, _
e As EventArgs)
Response.Redirect("SecondPage.aspx?id=" + txtname.text, false)
End Sub
on second page set your text on page load.
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack
SecondPageTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub
Use the query string to pass the text value to other page in the button click handler, like this:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Response.Redirect("OtherPage.aspx?textValue='Value from other page'")
End Sub
Now in the Page_Load of the other page, pull the query string value out and assign it to the text box, like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Request.QueryString("textValue") IsNot Nothing Then
YourTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub
You have to write code on first page
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Response.Redirect("webpage2.aspx?textValue='value'")
End Sub
On second page
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack
SecondPageTextBox.Text = Request.QueryString("textValue").ToString()
End If
End Sub
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 DropDownList with an SelectedIndexChanged event listener
<asp:DropDownList ID="LoanOptionCombo" runat="server" AutoProstBack="True">
Listener
Protected Sub LoanOptionCombo_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles LoanOptionCombo.SelectedIndexChanged
' hello world, no code here yet
End Sub
My Page_Load event
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' parse a .txt file and populate the DropDownList
InitializeLoanOptions()
End Sub
When the form initially loads, the loan options are loaded into the DropDownList (currently 4).
Whenever the user selects a loan option for the DropDownList, the DrowDownList gets re-initialized somehow, adding the same 4 options to the list again.
Each time the user selects another option, the same 4 options are re-added to the list.
I'm assuming the Page_Load event is being called again because that's the only place where I'm actually adding the DropDownList items. No other place in code interacts with the DrowDownList.Items.
How can I listen to the SelectedIndexChanged event on my DropDownList but avoid re-initializing my whole form?
you need to change your Page_Load as below
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' parse a .txt file and populate the DropDownList
If Not IsPostBack
InitializeLoanOptions()
End If
End Sub
You can use IsPostBack property in page load event.
Only data bind to your drop down when your page is load.
check theses links
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx
http://www.aspnet101.com/2007/03/if-not-page-ispostback/
Check IsPostBack property on page_load.
If Not IsPostBack
// Load your drop drop list here....
End If
I want to let TextBox control TextChanged event fire only when there are more than one character in the TextBox.
Thank you.
Public Class ZTextBox
Inherits System.Windows.Forms.TextBox
Public Event ZTextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
'Add your custom paint code here
End Sub
Private Sub ZTextBox_TextChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
If Me.Text.Length > 3 Then
RaiseEvent ZTextChanged(sender, e)
End If
End Sub End Class
and you can use the following in your form
Private Sub ZTextBox1_ZTextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ZTextBox1.ZTextChanged
MsgBox(1)
End Sub
I think you'll need a custom control that inherits the textbox control with a custom event, do your checks at the internal event, and fire your custom event when you see appropriate.
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.