Reputation: 453
I have a javascript function that adds text to an asp:textbox. But once I try to save the textbox in the C# Codebehind, the textbox.Text property is still holding the original value, not the updated value. Here's the code
Javascript
function GetLanguages(e)
{
var newLang = e.nextSibling;
var checkedValues = '';
var chkEng = document.getElementById ("<%=chkEnglish.ClientID %>");
var chkFr = document.getElementById ("<%=chkFrench.ClientID %>");
var chkList1 = document.getElementById ("<%=chkTopLanguages.ClientID %>");
var arrayOfCheckBoxes = chkList1.getElementsByTagName("input");
var txtLangValue = document.getElementById("<%=txtLANG.ClientID %>");
if(chkEng.checked)
checkedValues = "English";
if(chkFr.checked)
{
if(checkedValues.length > 0)
checkedValues += ";";
checkedValues += "French";
}
for(var i=0;i<arrayOfCheckBoxes.length;i++)
{
var checkBoxRef = arrayOfCheckBoxes[i];
if(checkBoxRef.checked)
{
var labelArray = checkBoxRef.parentNode.getElementsByTagName('label');
if ( labelArray.length > 0 )
{
if ( checkedValues.length > 0 )
checkedValues += ";";
checkedValues += labelArray[0].innerHTML;
}
}
}
txtLangValue.value = checkedValues;
}
CodeBehind
List<string> lstItemsChecked = new List<string>(txtLANG.Text.Split(';'));
foreach (string language in lstItemsChecked)
{
foreach (DataRow row in dsTopLanguages.Tables[0].Rows)
{
if (row["Language"].ToString() == language)
{
if (strLanguages.Length > 0)
strLanguages += ";";
strLanguages += row["LanguageID"].ToString();
}
}
}
The txtLANG.Text.Split call results to the original value of the textbox, not the value updated via javascript
Upvotes: 0
Views: 833
Reputation: 453
uggh, figured out what was wrong. Long day, way over complicated things. I forgot to wrap my data load code with if(!IsPostback){} so it was reloading the original record data before saving the values to the database. Sorry!
Upvotes: 1
Reputation: 561
And you are sure that the javascript function is being called and that the textbox is getting its value updated?
Have you tried adding the following at the end of your JavaScript function to make sure the value of the text box is being updated?
alert(txtLangValue.value);
Upvotes: 0