aspx页面中的Javascript,包括母版页 [英] Javascript in aspx page including master page

查看:61
本文介绍了aspx页面中的Javascript,包括母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试从具有母版页的aspx页面中使用以下aspx页面加载中的代码访问javascript:

HtmlGenericControl toAddJavaScript = new HtmlGenericControl();
toAddJavaScript.TagName =脚本";
toAddJavaScript.Attributes.Add("type","text/javascript");
toAddJavaScript.Attributes.Add("src","js/CommonInformationScript.js");
this.Page.Header.Controls.Add(toAddJavaScript);
textCustomerName.Attributes.Add("OnKeyUp",返回GetCustomers());

该代码成功命中了javascript的GetCustomers()方法.在此方法中,我尝试使用以下代码获取文本框名称"textCustomerName"(服务器控件)的值:

var customerSubString = document.getElementById("textCustomerName").value;

但是此行创建了一些错误,customerSubString变量中没有值.

请指导我.

谢谢
Divya

Hi,

I am trying to access javascript from a aspx page having master page using below code in aspx page load :

HtmlGenericControl toAddJavaScript = new HtmlGenericControl();
toAddJavaScript.TagName = "script";
toAddJavaScript.Attributes.Add("type","text/javascript");
toAddJavaScript.Attributes.Add("src","js/CommonInformationScript.js");
this.Page.Header.Controls.Add(toAddJavaScript);
textCustomerName.Attributes.Add("OnKeyUp","return GetCustomers());

This code is hitting GetCustomers() method of javascript successfully.In this method I am trying to get value of text box name "textCustomerName" (server control)using below code :

var customerSubString = document.getElementById("textCustomerName").value;

But this line is creating some error and there is no value coming in customerSubString variable.

please guide me.

Thanks
Divya

推荐答案

var customerSubString=document.getElementById('<%=textCustomerName.ClientID%>')

It gave me null value in variable customerSubString but it works if I use complete id "ctl00_ContentPlaceHolder1_textCustomerName" in place of using ClientId Property.



我认为这意味着您是在js文件中完成的,就像我告诉您不要那样做.



I assume this means you did it in your js file, like I told you not to. It''s not being evaluated.


您有一个基本问题,与母版页无关.


You have a fundamental problem, and it has nothing to do with master pages.


Divymital写道:
Divymital wrote:

= document.getElementById("textCustomerName").value;

= document.getElementById("textCustomerName").value;



您在服务器端分配的名称不是在客户端使用的名称.该控件具有ClientID属性,您需要在服务器上访问该属性并将其插入到javascript中.这意味着脚本的一部分不能存在于js文件中.我的管理方式是让我的js文件通过名为clientSideCustomerNameId之类的变量访问ID.然后,我后面的代码发出js,以创建具有正确值的变量.



The name you assign on the server side, is not the name used on the client side. The control has a ClientID property, and you need to access that on the server and insert it into your javascript. This means that bit of script cannot live in a js file. The way I manage this is for my js file to access an id via a variable called something like clientSideCustomerNameId. Then my code behind emits the js to create that variable with the right value.




感谢您的答复.

当我使用代码时:

var customerSubString = document.getElementById(''<%= textCustomerName.ClientID%>''))

它为我提供了变量customerSubString中的空值,但如果我使用完整的id"ctl00_ContentPlaceHolder1_textCustomerName"代替ClientId属性,则它可以工作.

问候,
Divya
Hi,

Thanks for your reply.

When I used code:

var customerSubString=document.getElementById(''<%=textCustomerName.ClientID%>'')

It gave me null value in variable customerSubString but it works if I use complete id "ctl00_ContentPlaceHolder1_textCustomerName" in place of using ClientId Property.

Regards,
Divya


这篇关于aspx页面中的Javascript,包括母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆