如何在自定义控件上添加javascript(服务器控件) [英] how to add javascript on custom control(server control)

查看:58
本文介绍了如何在自定义控件上添加javascript(服务器控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< script type =text / javascriptlanguage =javascript>

function add(){

var firstno,secondno,result;

firstno = document.getElementById(txtfnumber)。value;

secondno = document.getElementById(txtsnumber)。value;

result = firstno + secondno;

document.getElementById(lblResult)。innerHTML = result;



}

< / script>



< td class =style2>< asp:TextBox ID =txtfnumberrunat =ser ver>

         
  < asp:Label ID =Label1runat =serverText =first number>

   
         
  < asp:Label ID =Label2runat =serverText =second number>

< asp:TextBox ID =txtsnumberrunat =server>

   
  < asp:Button ID =btncalculaterunat =serverOnClientClick =add()

Text =Add/>

< asp:Label ID = lblResultrunat =发球r>

   




显示ERROR消息



JavaScript运行时错误:无法获取undefined或null引用的属性'value'

<script type="text/javascript" language="javascript">
function add() {
var firstno, secondno, result;
firstno = document.getElementById("txtfnumber").value;
secondno = document.getElementById("txtsnumber").value;
result = firstno + secondno;
document.getElementById("lblResult").innerHTML = result;

}
</script>

         
  <asp:Label ID="Label1" runat="server" Text="first number">
<asp:TextBox ID="txtfnumber" runat="server">
   
         
  <asp:Label ID="Label2" runat="server" Text="second number">
<asp:TextBox ID="txtsnumber" runat="server">
   
  <asp:Button ID="btncalculate" runat="server" OnClientClick="add()"
Text="Add" />
<asp:Label ID="lblResult" runat="server">
   


ERROR message is shown

JavaScript runtime error: Unable to get property 'value' of undefined or null reference

推荐答案

查看此示例:

在自定义控件上添加以下代码。

See this example:
Add the below code on your custom control.
<p>Click the button to calculate x.</p>
    <button >Try it</button>
    <br />
    <br />Enter first number:
    <input type="text" id="txt1" name="text1">Enter second number:
    <input type="text" id="txt2" name="text2">
<p id="demo"></p>//replace this p tag with your label</input></input>




<script type="text/javascript">
  function myFunction() {
    var y = document.getElementById("txt1").value;
    var z = document.getElementById("txt2").value;
    var x = +y + +z;
    document.getElementById("demo").innerHTML = x;
  }
</script>


JavaScript在客户端上运行,您的aspx文件在服务器上。此代码



JavaScript runs on the client, your aspx files are on the server. This code

stno = document.getElementById("txtfnumber").value;





正在寻找id为txtfnumber的元素。您的aspx页面有一个带有该ID的服务器标签,但是javascript使用发送到浏览器的内容,因此查看页面的来源并查看源中是否有一个带有该ID的元素。 asp.net在各种情况下更改控件的ID,因此您需要使用.net在您的javascript中生成正确的ID。





is looking for an element with an id of "txtfnumber". Your aspx page has a server tag with that ID, but javascript uses what is sent to the browser, so view the source of the page and see if there is an element in the source with that ID. asp.net changes the IDs of your controls in various situations so you need to use .net to generate the right ID in your javascript.

stno = document.getElementById("<%=txtfnumber.ClientID%>").value;


如果您正在使用母版页概念,那么任何控制值使用

stno = document.getElementById(<%= txtfnumber.ClientID%>)。value;



否则你可以直接使用

stno = document.getElementById(txtfnumber)。value;
If you are using master pages concept, for getting any control value use
stno = document.getElementById("<%=txtfnumber.ClientID%>").value;

Otherwise you can get directly by using
stno = document.getElementById("txtfnumber").value;


这篇关于如何在自定义控件上添加javascript(服务器控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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