C# javascript将控件名作为函数参数传递

C# javascript将控件名作为函数参数传递,c#,javascript,asp.net,function,C#,Javascript,Asp.net,Function,我试图将一些控件ID从代码隐藏传递到html,但我事先不知道控件名称,因为它们是动态创建的,因此我无法使用类似这样的内容:MyTextBox=document.getElementById(“”) 如何使以下代码正常工作(如果可能): JS功能: function MyFunc(TextBox1,TextBox2) { MyTextBox = document.getElementById("TextBox1"); MyTextBox2 = doc

我试图将一些控件ID从代码隐藏传递到html,但我事先不知道控件名称,因为它们是动态创建的,因此我无法使用类似这样的内容:
MyTextBox=document.getElementById(“”)

如何使以下代码正常工作(如果可能):

JS功能:

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;
        }
函数MyFunc(TextBox1,TextBox2){
MyTextBox=document.getElementById(“TextBox1”);
MyTextBox2=document.getElementById(“TextBox2”);
var splitDate=MyTextBox.value.split('/');
var日期=新日期(拆分日期[2],拆分日期[1]-1,拆分日期[0]);
var day=date.getDate();
var month=date.getMonth()+1;
var year=date.getFullYear()+1;
如果(第10天){
天='0'+天;
}
如果(月<10){
月份='0'+月份;
}
MyTextBox2.value=天+“/”+月+“/”+年;
}

由于ID是变量,因此不应将其括在

你也需要改变

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

因为
TextBox1
TextBox2
都必须作为字符串传递

我想你可以这样做

 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);");
在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;
        }
函数MyFunc(TextBox1,TextBox2){
MyTextBox=document.getElementByClass(“TextBox1”);
MyTextBox2=document.getElementByClass(“TextBox2”);
var splitDate=MyTextBox.value.split('/');
var日期=新日期(拆分日期[2],拆分日期[1]-1,拆分日期[0]);
var day=date.getDate();
var month=date.getMonth()+1;
var year=date.getFullYear()+1;
如果(第10天){
天='0'+天;
}
如果(月<10){
月份='0'+月份;
}
MyTextBox2.value=天+“/”+月+“/”+年;
}

如果有用的话,试试这个。

我刚刚试过,但它似乎仍然无法识别TextBox1。我发现:
0x800a138f-JavaScript运行时错误:无法获取未定义或空引用的属性“value”
能否在函数开头添加一个
警报(TextBox1)
并查看什么是警报编辑显示TextBox1,然后它在以下位置中断:
var splitDate=MyTextBox.value.split('/')当用户单击gridview行中的edit时,文本框会动态创建,并且gridview的RowDataBound函数会调用隐藏的代码。您是否可以检查dom并查看元素的id设置是否正确?我是否已使用以下命令对其进行了修复:
test.Attributes.Add(“onChange”,“javascript:MyFunc(这,+TextBox2.ClientID+);”;
并去掉这些行:
MyTextBox=document.getElementById(“TextBox1”);MyTextBox2=document.getElementById(“TextBox2”);
通过这个我得到:
0x800a1391-JavaScript运行时错误:如果我这样做:
test.Attributes.Add(“onchange”):
“,”javascript:MyFunc(这是,'TextBox2');”;”;
我可以让TextBox1的实际值正常工作,但是TextBox2仍然说未定义对不起,我已经编辑了我的答案第二个应该是TextBox2
 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);");
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;
        }
test.Attributes.Add("onChange", "javascript:MyFunc(" + TextBox1.ClientID + ", " + TextBox2.ClientID + ")");