I am developing one sharepoint webpart, in this i have 2 date time control, one is for From date and another is for To date, And i wrote one javascript function to validate the date entered by the user. If the To date is less than the from Date, it will alerts. and i added this JS function to the button click as a attribute. But this function is not executing while i am clicking.
i am adding my code with this:
void Registerscript()
{
string jc = @"<script> function DateMsg()
{{
var Fromdate = document.getElementById('{0}').value;
var Todate = document.getElementById('{1}').value;
if(Fromdate != '' && Todate != '')
{{
if(Date.parse(Fromdate) > Date.parse(Todate))
alert('From Date should be earlier than To Date.');
}}
}}</script> ";
jc = string.Format(jc, dtFromdate.ClientID + "_dtFromdateDate", dtTodate.ClientID + "_dtTodateDate");
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", jc);
}
I called this function in Createchildcontrol method of the webpart..
And i added this code on OnLoad event of the webpart
btnView.Attributes.Add("onclick", "DateMsg();");
Please help me for resoving this issue…
FYI: This btnView(Button control) is dynamically created at runtime..
Thanks in advance…
I would go in different way, allowing easier debug.
Have the function in the .aspx file:
And output only the ID from the code behind:
Leave the button onclick unchanged, and now you might see what’s wrong – keep us updated.