访问在Javascript中的ASP

访问在Javascript中的ASP

本文介绍了访问在Javascript中的ASP.NET控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的Javascript线在我的ASP.NET web应用程序:

 的document.getElementById('<%= myTextBox [0] .ClientID%GT;')。值=测试;

我怎么能访问myTextBox元素呢?比如我想改变这种服务器端阵列的第5个元素,我想一个服务器端的参数传递给我的功能,我该怎么办呢?

例如:

 服务器端:
ddl.Attributes.Add(的onChange,返回OnFoodChange(这一点,'+ I +'););
javascript函数:
功能OnFoodChange(myCmb,myIndex)

。值=测试; 给我的错误,它说,使用的document.getElementById(';%= myTextBox [myIndex] .ClientID%GT&LT')

这myIndex没有定义,我想是因为myIndex的使用方式与服务器端的参数,我该怎么解决呢?

这是我的全JavaScript函数:

 函数OnFoodChange(myCmb,myIndex){
               //警报(5);
尝试{
               变种Q =的document.getElementById('&下;%= HFFoodPrice.ClientID%GT;')。value.toString();
           无功Q2 = q.split(';');
           变种索引= 0;
           //警报(myCmb.selectedIndex.toString());           //变种E =的document.getElementById(myCmb);
           VAR strUser的= myCmb.options [myCmb.selectedIndex] .value的;           的document.getElementById('<%= myTextBox [0] .ClientID%GT;')。值= strUser的;           对于(VAR J = 0; J< q2.length; J ++){
               如果(Q2 [J]!=''){
                   VAR Q3 = Q2 [J] .split('');
                   {                   }
               }
           }
           }
           赶上(错误)
           {
           警报(err.message);
           }
       }


解决方案

发送,而不是发送指数asp.net控件ID像您期望可能不是文本框的这个控件的ID,

在code后面

  ddl.Attributes.Add(的onChange,返回OnFoodChange(这一点,'+ myTextBox [I] .ClientID +'););

在HTML

 函数OnFoodChange(myCmb,myTextBoxId){
 currentTextBox =的document.getElementById(myTextBoxId);
//你的code ...
}

I have following Javascript line in my ASP.NET web app:

document.getElementById('<%=myTextBox[0].ClientID %>').value = "test";

how can I access myTextBox elements? for instance I want to change 5th element of this server side array, I want to pass a server side parameter to my function, how can I do it?

for instance:

server side:
ddl.Attributes.Add("onChange", "return OnFoodChange(this,'" + i + "');");


javascript function:
function OnFoodChange(myCmb,myIndex)

using document.getElementById('<%=myTextBox[myIndex].ClientID %>').value = "test"; gives me error, it says that myIndex is not defined, I think because myIndex is used like a server side parameter, how can I solve it?

it is my full JavaScript function:

   function OnFoodChange(myCmb,myIndex) {
               //alert('5');
try{
               var q = document.getElementById('<%= HFFoodPrice.ClientID %>').value.toString();
           var q2 = q.split(';');
           var index = 0;
           //alert(myCmb.selectedIndex.toString());

           //var e = document.getElementById(myCmb);
           var strUser = myCmb.options[myCmb.selectedIndex].value;

           document.getElementById('<%=myTextBox[0].ClientID %>').value = strUser;

           for (var j = 0; j < q2.length; j++) {
               if (q2[j] != '') {
                   var q3 = q2[j].split(',');
                   {

                   }
               }
           }
           }
           catch(err)
           {
           alert(err.message);
           }
       }
解决方案

Send this control id of textbox instead of sending index as asp.net control id might not be as you expect,

In Code behind

ddl.Attributes.Add("onChange", "return OnFoodChange(this,'" + myTextBox[i].ClientID + "');");

In HTML

function OnFoodChange(myCmb,myTextBoxId) {
 currentTextBox = document.getElementById(myTextBoxId);
//Your code...
}

这篇关于访问在Javascript中的ASP.NET控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 13:12