...

View Full Version : Javascript/Code-Behind variable not working?



blakemckenna
01-03-2012, 08:05 PM
I have a webpage where I have declared a variable as



<input type="hidden" id="Edits_OK" />


I set the value of this variable within a Javascript function to either "T" or "F". In my code-behind, I have a btnSubmit_Click() event that calls this Javascript function. In this code-behind event, I also check the value of the "Edits_OK" variable, however, the value is always "". The Javascript function will always put a value in this variable whether it's "T" or "F". I also declare the "Edits_OK" variable within the code-behind as this:



Private Edits_OK As New HiddenField



Here is the Javascript Function.



function CheckEdits() {
var msg;
document.getElementById("Edits_OK").value = 'T';

if (document.getElementById("txtName").value == '') {
msg = msg + '\nName';
document.getElementById("Edits_OK").value = 'F';
}
if (document.getElementById("txtOrganization").value == '') {
msg = msg + '\nOrganization';
document.getElementById("Edits_OK").value = 'F';
}
if (document.getElementById("txtSubject").value == '') {
msg = msg + '\nSubject';
document.getElementById("Edits_OK").value = 'F';
}
if (document.getElementById("txtNote").value == '') {
msg = msg + '\nNote';
document.getElementById("Edits_OK").value = 'F';
}

if (document.getElementById("Edits_OK").value == "F") {
var msg2;
msg2 = "Please provide the following information:\n";
error_occurred = true;
alert(msg2 + msg);
}
}


Here is the code-behind Procedure:


Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
If Edits_OK.Value = "T" Then
FormatEmail()
End If

Catch ex As Exception
lblErrMsg.Text = "btnSubmit_Click() Event - " & ex.Message
End Try
End Sub

Not sure why the value is always "". Please help!

Thanks!

Old Pedant
01-03-2012, 11:11 PM
In my code-behind, I have a btnSubmit_Click() event that calls this Javascript function.

HUH??? I assume you are talking about ASP.NET code-behind?

If so, you can't call a javascript function from *ANY* server-side code, ASP.NET or otherwise.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum