Link to home
Start Free TrialLog in
Avatar of QPR
QPRFlag for New Zealand

asked on

Event only fires once

I have 2 image buttons that when clicked change their respective calendars to visible.
A date is selected. The calendar reverts to hidden, the date populates a textbox and that text box becomes visible.
Then the user does this same process on the 2nd image button.
These represent a start date and an end date. I have a compare validator that ensures end date is > start date.
All of the above works fine.
However if end date > start date and the user is prompted to repick their dates... clicking on the image buttons will not show the calendars again. Nothing happens. There is if postback code.
Here is my code behind. Can anyone see why the events are only fired/handled the first times?

Partial Class _InputRound
    Inherits System.Web.UI.Page

    Protected Sub imgPin1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgPin1.Click
        CalDate1.Visible = True
    End Sub

    Protected Sub CalDate1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalDate1.SelectionChanged
        txtStartDate.Text = CalDate1.SelectedDate
        CalDate1.Visible = False
        txtStartDate.Visible = True
    End Sub

    Protected Sub imgPin2_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgPin2.Click
        CalDate2.Visible = True
    End Sub

    Protected Sub CalDate2_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalDate2.SelectionChanged
        txtEndDate.Text = CalDate2.SelectedDate
        CalDate2.Visible = False
        txtEndDate.Visible = True
    End Sub

End Class
Avatar of sandip132
sandip132
Flag of Japan image

Here is you code:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Visible = False
    End Sub

    Private Sub ImageButton1_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Calendar1.Visible = True
    End Sub

    Private Sub ImageButton2_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
        Calendar2.Visible = True
    End Sub

    Private Sub Calendar1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        TextBox1.Text = Calendar1.SelectedDate
        TextBox1.Visible = True
        If (TextBox1.Text <> "" And TextBox2.Text <> "") Then
            Button1.Visible = True
        End If
    End Sub

    Private Sub Calendar2_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calendar2.SelectionChanged
        TextBox2.Text = Calendar2.SelectedDate
        TextBox2.Visible = True
        If (TextBox1.Text <> "" And TextBox2.Text <> "") Then
            Button1.Visible = True
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (DateTime.Compare(CDate(TextBox1.Text), CDate(TextBox2.Text)) > 0) Then
            TextBox1.Visible = False
            TextBox2.Visible = False
            Button1.Visible = False
           ''''''''''''''''''''''''''''''''''''''''''''''''Please select dates again.
        End If


    End Sub
Avatar of QPR

ASKER

Is this replacing my existing date compare validator control?
I still don't understand why my imgPin1 event is not firing. The event does nothing except toggle the calendars visibility
Avatar of QPR

ASKER

Why am I setting the the buttons visibility to false
Also, when I try your way....
I click on the imagebutton to bring up the calendar and validation fires even though I haven't submitted yet.
Avatar of QPR

ASKER

apologies ignore the line below - it does this regardless if I use yours or my version...


Also, when I try your way....
I click on the imagebutton to bring up the calendar and validation fires even though I haven't submitted yet.
>>>There is if postback code.

Try removing this.
Avatar of QPR

ASKER

apologies (again) that was a typo in my intial post. That should read there is NO if postback.
Avatar of badalpatel
badalpatel

u just have to add one more requiredfieldvalidator in ur panel span6..i think that will do it..and if u want to do this with javascript then tell me i will help u out...

 <asp:Panel id="span6" runat="server" visible="false">(please specify) <asp:TextBox runat="server" ID="txt6Other" />
                     <asp:RequiredFieldValidator ControlToValidate="txt6Other" ID="RequiredFieldValidator1" runat="server" ErrorMessage="*Required" />
                     </asp:Panel>
sorry last comment is not belongs here...by mistake i put it here
Please create one new application, put 2 imagebuttons, 2 calenders, 2 textboxes abd a button with default(IDs).

and use my code in code behind, with this you can track where the actual peroblem occurs....
HIi QPR

yes while the comparevalidator is true the page wil not postback and calendar control need the postback..thats why the page is not showing there..
what u can do is to put a javascript to end ur problem..u put one label instead of compare validator and make it visible true when the startdate<enddate or any of ur condition...this will check ur textbox value when u click on button while submitting ur form..

i m showing code of javascript

function checkdate()
{
if(Date.parse(document.getElementById("txtStartDate").value) < Date.parse(document.getElementById("txtEndDate").value))
            {
                document.getElementById("lblerror").visibility = 'visible'
               return false;
            }
   return true;
}

u can call this function on ur button click by writing below line in page_load..
btnLoadChart.Attributes.Add("onclick", "return checkdate();")
Avatar of QPR

ASKER

Thanks, can you help with this bit
btnLoadChart.Attributes.Add("onclick", "return checkdate();")

I put it here but get errors
<body onload="Button1.Attributes.Add("onclick", "return checkdate();")">

error = name and attribute must be separated by an equals sign
Avatar of QPR

ASKER

sorry! I realised that was code behind *blush*
Avatar of QPR

ASKER

Still no good, now when I click the image to toggle the calendars visibility I get 1 less validation error (compare) but I still get the other 2... startdate required and end date required. I don't want to hassle the user with these messages until they hit the submit button.
Making the calendar visible is causing a postback which is firing validation.

How can I prevent this?
Avatar of QPR

ASKER

solved!
I just needed to set the "causes validation" property on the image button to false.
Now when it postsback to toggle the calendars visibility it doesn't check the form values.
AND it let's me retoggle the calendar following validation
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial