0

I am trying to pass some control IDs from code behind to html but I do not know the control names in advance as they are created dynamically and hence I cannot use something like this: MyTextBox = document.getElementById("<%= TextBox1.ClientID %>");

How can I make the following code work (if it is possible that is):

            TextBox test = ((TextBox)e.Row.Cells[7].Controls[0]);
            test.ID = "TextBox1";
            TextBox test2 = ((TextBox)e.Row.Cells[8].Controls[0]);
            test2.ID = "TextBox2";
            test.Attributes.Add("onChange", "javascript:MyFunc(TextBox1, TextBox2);");

JS function:

function MyFunc(TextBox1,TextBox2) {
            MyTextBox = document.getElementById("TextBox1");
            MyTextBox2 = document.getElementById("TextBox2");

            var splitDate = MyTextBox.value.split('/');
            var date = new Date(splitDate[2], splitDate[1] - 1, splitDate[0]);

            var day = date.getDate();
            var month = date.getMonth() + 1;
            var year = date.getFullYear() + 1;
            if (day < 10) {
                day = '0' + day;
            }
            if (month < 10) {
                month = '0' + month;
            }

            MyTextBox2.value = day + "/" + month + "/" + year;
        }

3 Answers 3

1

since the ids are variables, you should not enclose it with ""

var MyTextBox = document.getElementById(TextBox1);
var MyTextBox2 = document.getElementById(TextBox2);

Also you need to change

test.Attributes.Add("onChange", "javascript:MyFunc('TextBox1', 'TextBox2');");

because both TextBox1 and TextBox2 has to be passed as strings

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

5 Comments

I have tried that just now and it seems that it still does not recognize TextBox1. I get this: 0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference
can you add a alert(TextBox1) at beginning of the function and see what is alerted
It shows TextBox1 and then it breaks at: var splitDate = MyTextBox.value.split('/'); The textboxes are created dynamically when a user clicks edit in a gridview row and the code behind gets called from the RowDataBound function of the gridview
can you inspect your dom and see whether the id of the element is set properly
I have fixed it with this: test.Attributes.Add("onChange", "javascript:MyFunc(this, + TextBox2.ClientID + ");"); and getting rid of these lines: MyTextBox = document.getElementById("TextBox1"); MyTextBox2 = document.getElementById("TextBox2");
0

I think you can do like this,

 TextBox test = ((TextBox)e.Row.Cells[7].Controls[0]);
            test.ID = "TextBox1";
            test.Attributes.Add('class','TextBox1');
            TextBox test2 = ((TextBox)e.Row.Cells[8].Controls[0]);
            test2.ID = "TextBox2";
            test.Attributes.Add('class','TextBox2');
            test.Attributes.Add("onChange", "javascript:MyFunc(TextBox1, TextBox2);");

in JS:

function MyFunc(TextBox1,TextBox2) {
            MyTextBox = document.getElementByClass("TextBox1");
            MyTextBox2 = document.getElementByClass("TextBox2");

            var splitDate = MyTextBox.value.split('/');
            var date = new Date(splitDate[2], splitDate[1] - 1, splitDate[0]);

            var day = date.getDate();
            var month = date.getMonth() + 1;
            var year = date.getFullYear() + 1;
            if (day < 10) {
                day = '0' + day;
            }
            if (month < 10) {
                month = '0' + month;
            }

            MyTextBox2.value = day + "/" + month + "/" + year;
        }

1 Comment

With this I get: 0x800a1391 - JavaScript runtime error: 'TextBox1' is undefined
0
test.Attributes.Add("onChange", "javascript:MyFunc(" + TextBox1.ClientID + ", " + TextBox2.ClientID + ")");

try this if it help.

2 Comments

If I do it like this: test.Attributes.Add("onchange", "javascript:MyFunc(this, 'TextBox2');"); I can get the actual value of TextBox1 to work ok but TextBox2 still says undefined
Sorry, I have edited my answer the second one should be TextBox2

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.