0

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

2
  • so is there any way to retrieve this value in the codebehind or do I have to force a postback somehow? Commented Apr 3, 2013 at 16:13
  • Please kindly edit your original question instead of writing in comment area. Commented Apr 3, 2013 at 16:14

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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); 

6 Comments

"so is there any way to retrieve this value in the codebehind or do I have to force a postback somehow?" You can only get the new values after you post the page back
how can I force a postback in my javascript? I assume I would have to force that at the end of the GetLanguages() function then correct?
you can try forcing a post back in your textbox. Set the autopostback property to true.
You could also try to do get your form and submit it at the end of you javascript function. var form = documents.form["myForm"]; form.submit();
yes, this worked, but posting back on each call of this function is very ugly. The whole page refreshes each time a checkbox is clicked now :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.