Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript on asp.net 1

Status
Not open for further replies.

Crazyec

Programmer
Aug 28, 2001
80
HK
Hi Pros, I have a question on using javascript.
I want to use some client side javascript for checking user input valid data to the form.
but I am using asp.net web controls.

the source will like below
<input name="ctl16$txtEndDate" type="text" value="11-May-2006" maxlength="11" id="ctl16_txtEndDate" />

If I want to check it in the javascript, now I am hardcoding, using document.aspnetForm.ctl16$txtStartDate.value for checking.

I found ctl16 sometimes will change (sometimes ctl18...), any suggestion on that?
 
Crazy: Check the FAQ on java and see if the numeration of position may help (using (this);, and function (obj) approach, etc. (An of course try getElementbyID("txtbox"), etc.

This should be very straight forward. Do you want to do all of this in Java or part on the return to the server? Not quite sure - seems all java here should work just fine.
 
Thank you for your suggestion,
actually I have tried on that, but get some problems.

I do the checking on the submit button (web control) thru the OnClientClick.
But when I put following into the OnClientClick:

javascript: if (checkFormat(document.getElementById('txtStartDate').value) && checkFormat(document.getElementById('txtEndDate').value)){if (checkValid()){return true;}else {alert("The end date is eariler than the start date!");return false;}}else {alert("The dates are invalid!");return false;}

The checking is not work. Any idea on that? Thank you.
 
I know what's the problem now.
actually the id is changed everytime also.
it is not 'txtStartDate'... it becomes ctl16_txtStartDate
 
Looking at your javascript function:
Code:
function chkDate{
if (checkFormat(document.getElementById('txtStartDate').value) && checkFormat(document.getElementById('txtEndDate').value)){
  if (checkValid()){
    return true;
   }
  else{
    alert("The end date is eariler than the start Date!");return false;}
     }
  else{
    alert("The dates are invalid!");
    return false;
}
I could be wrong but I don't see how you differentiate between the two "else" statements (no IF condition).

Also, not sure how checkFormat() works - what is its function?
 
checkFormat is a javascript function for checking the date format, it works good before.

There are two If... and two else....

I think I know what's wrong, the ID is not correct, as the asp.net change the id into "ctl16_txtStartDate"

now is thinking of use addattribute in the page_load and use txtStartDate.ClientID to find the real control ID
 
ok Crazy - I see now; a little late, OK.

Try the add attribute route and see how that works for you. Keep us posted. Someone in the morning might see the solution right away for this so hang in there.
 
Finally I put these code in the Page_load sub
and it works!

cmdSearch.Attributes.Add("OnClick", "if (checkFormat(document.getElementById('" & txtStartDate.ClientID & "').value) && checkFormat(document.getElementById('" & txtEndDate.ClientID & "').value)){if (checkValid(document.getElementById('" & txtStartDate.ClientID & "').value, document.getElementById('" & txtEndDate.ClientID & "').value)){return true;}else {alert('The end date is eariler than the start date!');return false;}}else {alert('The dates are invalid!');return false;}")

Thank you, Isadore. You really give me good hints!
 
Good job Crazey. This will be a good post for someone trying to duplicate this procedure. I can sympathize, coding sometimes can drive you over the hill. See ya around.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top