I am almost done with my project and I'm having trouble finding what is causing this problem.
I have on my form for this example a checkbox, two textbox's and a button. I was trying to implement a simple javascript function that if a user checks the check box it will fill the two textboxes with the current date and time. I know my javascript works fine but when the button is pressed it resets my controls back. Here is the code to further explain my problem:
My Javascript code as follows:
Here are my controls in the web form and how they are setup:
I have this in my page load in my codebehind:
After stepping through all my code I see the attributes are set up correctly. The javascript function fires correctly when you check and uncheck the checkbox. Here is what is happening. The form loads with my textboxes being "disabled" and the checkbox is unchecked. When I click on the checkbox it enables the textboxes and fills in the date/time. When I click on the button it submits causing the page to postback my textboxes become "disabled" again. I just can not figure out what is causing this problem and I am stumped.
So any tips, suggestions or something I did wrong please let me know and I appreciate it. Have a nice day.
I have on my form for this example a checkbox, two textbox's and a button. I was trying to implement a simple javascript function that if a user checks the check box it will fill the two textboxes with the current date and time. I know my javascript works fine but when the button is pressed it resets my controls back. Here is the code to further explain my problem:
My Javascript code as follows:
Code:
<script language="javascript">
function checkbox(){
var dateVar = new Date();
var timevar = dateVar.getHours() + ":" + dateVar.getMinutes();
if(document.Form1.chkPhone.checked){
document.Form1.txtCallBackDate.style.backgroundColor='#ffffff';
document.Form1.txtCallBackDate.readOnly= false;
document.Form1.txtCallBackDate.value = formatDate(dateVar);
document.Form1.txtCallBackDate.disabled = false;
document.Form1.txtCallBackTime.style.backgroundColor='#ffffff';
document.Form1.txtCallBackDate.readOnly = false;
document.Form1.txtCallBackTime.value = timevar;
document.Form1.txtCallBackTime.disasble = false;
}
else{
document.Form1.txtCallBackDate.style.backgroundColor='#ffff80';
document.Form1.txtCallBackDate.readOnly = true;
document.Form1.txtCallBackDate.value = "";
document.Form1.txtCallBackDate.disabled = true;
document.Form1.txtCallBackTime.style.backgroundColor='#ffff80';
document.Form1.txtCallBackTime.readOnly = true;
document.Form1.txtCallBackTime.value = "";
document.Form1.txtCallBackTime.disabled = true;
}
}
</script>
Here are my controls in the web form and how they are setup:
Code:
<asp:TextBox ID="txtCallBackDate" Runat="server" CssClass="TextBox" AutoPostBack="False" ReadOnly="True" BackColor="#FFFF80"></asp:TextBox>
<asp:TextBox ID="txtCallBackTime" Runat="server" CssClass="TextBox" AutoPostBack="False" ReadOnly="True" BackColor="#FFFF80"></asp:TextBox>
I have this in my page load in my codebehind:
Code:
private void Page_Load(object sender, System.EventArgs e){
// Put user code to initialize the page here
if (!Page.IsPostBack){
chkPhone.Attributes.Add("onclick","checkbox()");
}
}
After stepping through all my code I see the attributes are set up correctly. The javascript function fires correctly when you check and uncheck the checkbox. Here is what is happening. The form loads with my textboxes being "disabled" and the checkbox is unchecked. When I click on the checkbox it enables the textboxes and fills in the date/time. When I click on the button it submits causing the page to postback my textboxes become "disabled" again. I just can not figure out what is causing this problem and I am stumped.
So any tips, suggestions or something I did wrong please let me know and I appreciate it. Have a nice day.