复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<script type="text/javascript">
function GetValue()
{
<SPAN><STRONG>var t=document.getElementById('<%= TextBox1.ClientID %>');</STRONG></SPAN>
t.innerText=2;
}
</script>
<body>
<form runat="server">
<div>
<asp:TextBox runat="server"></asp:TextBox></div>
<input type="button" runat="server" value="赋值" />
</form>
</body>
</html>
复制代码 代码如下:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function GetValue()
{
<SPAN><STRONG>document.write('<%= TextBox1.ClientID %>')</STRONG></SPAN>
}
</script>
<asp:TextBox runat="server"></asp:TextBox>
<input type="button" runat="server" value="赋值" />
</asp:Content>
页面显示了<SPAN><STRONG>ctl00_ContentPlaceHolder1_TextBox1</STRONG></SPAN>。即TextBox1.ClientID =ctl00_ContentPlaceHolder1_TextBox1。
复制代码 代码如下:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function GetValue()
{
<SPAN><STRONG><SPAN>var t=document.getElementById("TextBox1");</SPAN></STRONG></SPAN>
t.innerText=2;
}
</script>
<asp:TextBox runat="server"></asp:TextBox>
<input type="button" runat="server" value="赋值" />
</asp:Content>
出错了,t=null,也就是找不到TextBox1,所以需要改成<SPAN><STRONG>var t=document.getElementById('<%=TextBox1.ClientID%>');</STRONG></SPAN>
新闻热点
疑难解答
图片精选