hi there guys!
I have a scenario that is presently giving me sleepless nights. I need to call an asp method from an "onclick" html event but the method is being called too early.

Public Sub CheckForSomeErrors()
            If Session("FliteDate") = "" Then
                Response.Redirect("../Errorpage.aspx?ErrorDetails='Flight Date' cannot be blank!! Click 'Previous' to re-Enter value")

            End If
            If Session("ReturnDate") = "" Then
                Response.Redirect("../Errorpage.aspx?ErrorDetails='Probable return Date' cannot be blank!! Click 'Previous' to re-Enter value")

            End If
            If Session("PreferedAirline") = "" Then
                Response.Redirect("../Errorpage.aspx?ErrorDetails=Please indicate your preferred Airline! Click 'Previous' to re-Enter value")

            End If

            If Session("GsmNo") = "" Then
                Response.Redirect("../Errorpage.aspx?ErrorDetails=Please your valid GSM No is of extreme importance! Click 'Previous' to re-Enter value")
            End If
    End Sub

this is the code in the asp form module we can call formx.

<table borderColor="green" height="150" cellSpacing="0" cellPadding="0" width="1020" border="0">
						<tr>
							<TD vAlign="top"><IMG id="navigationbuttons" height="150" alt="" src="../img/navigationbuttons.jpg" width="1005"
									useMap="#m_navigationbuttons" border="0" name="navigationbuttons"><map id="m_navigationbuttons" name="m_navigationbuttons">
									<area shape="RECT" alt="" coords="689,50,950,110" href="../default.aspx">
									<area onclick="<%CheckForSomeErrors()%>"  shape=RECT  alt="" coords="364,40,654,110" href="prePassger2.aspx">
									<AREA shape="RECT" alt="" coords="30,40,314,110" href="javascript:history.back();">
								</map>
							</TD>
						</tr>
					</table>

This is the html code calling the asp function from the onclick event of the html hyperlink.
So in effect the 'checkforsomeerrors' function should be called as the 'hotspot' or indicated area is clicked before the call to 'prePassger2.aspx' is evaluated. But in practice, the 'checkforsomeerrors' function is called each time formx is loaded which is not desirable. I had tried to trap which event is responsible for this (like 'onload' or 'onclick') but is yet to see it.

So in effect, if I am calling formx from formK, the 'checkforsomeerrors' function is called (that is the onclick event is fired) even before the formx is loaded giving undesirable results. The object is for the function to be called as the hyperlink to another page is clicked.
Please your ideas are welcome!!

Recommended Answers

All 2 Replies

when you put the <%CheckForSomeErrors()%> in your page, it will be executed every time the page is viewed... postbacks and all. Since this is ASP.NET, familiarize yourself with the validation controls. This can easily be done by adding a <asp:RequiredFieldValidator ... /> and done on the client. But if you so wish to have it done the way you are doing, you need to make that into a server control by adding "runat='server'" to the attributes section and change your code a bit.

Do yourself a favor, read this article and be a pro at this part!

http://www.asp101.com/lessons/validation.asp

Use the RequiredFieldValidators. It will check to make sure it is not null by default. It will not let them submit the form unless it is filled out! Much better.

commented: thanks shepregy! +2

Thanks for your response.
You know I was just not thinking straight! and sometimes it happens;)
I went this way because my requiredfield validator was not firing so I abandoned it for an 'easier ?' way. But I didn't remember to set the causesvalidation attribute to 'true' the default being 'false'. So that's why the requiredvalidator was not firing.

Secondly I found that the code was doing exactly what it was told to do!! silly me!:cool:
Thanks all the same

when you put the <%CheckForSomeErrors()%> in your page, it will be executed every time the page is viewed... postbacks and all. Since this is ASP.NET, familiarize yourself with the validation controls. This can easily be done by adding a <asp:RequiredFieldValidator ... /> and done on the client. But if you so wish to have it done the way you are doing, you need to make that into a server control by adding "runat='server'" to the attributes section and change your code a bit.

Do yourself a favor, read this article and be a pro at this part!

http://www.asp101.com/lessons/validation.asp

Use the RequiredFieldValidators. It will check to make sure it is not null by default. It will not let them submit the form unless it is filled out! Much better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.