当前页面: JAVA 编程资料牛鼻论坛 → AJAX & JavaScript 技术→javascript脚本问题 |
在ASP。NET里面怎么调用JAVASCRIPT脚本?我没有弄清楚搂住的意思。一般的ASP.NET的后台程序是运行在服务器上的,JS是运行在客户端的IE的,所以是无法直接调用的。
可以使用页面输出的方式使ASP.NET调用JS,就是在ASP.Net里面加上直接输出调用JS的脚本。
还可以使用XMLHTTP的方式用JS请求ASP.NET,然后由ASP.NET输出有意义的XML文本,再由JS解析这些文本来执行相应的JS函数。AJAX就是这个思路
<%@ Page language="c#" Codebehind="user_login.aspx.cs" AutoEventWireup="false" Inherits="haogg.user_login" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="css/user.css" type="text/css" rel="stylesheet">
<script language="javascript">
function checkEmpty()
{
if(Form1.all.Textbox1.value=="")
{
alert("请输入用户名!");
return false;
}
else if(Form1.all.Textbox2.value=="")
{
alert("请输入密码!");
return false;
}
else
{
return true;
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table style="BORDER-COLLAPSE: collapse" cellSpacing="0" cellPadding="0" width="80%" align="center"
border="1" background="images/1.gif">
<tr>
<td vAlign="top" colSpan="2" height="25"> <span style="FONT-WEIGHT: bold; FONT-SIZE: 18px">用户登录</span>
</td>
</tr>
<tr>
<td width="15%" height="30"> 用户名:</td>
<td><asp:TextBox id="Textbox1" runat="server" width="198px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="HEIGHT: 27px" width="15%" height="27"> 密码:</td>
<td style="HEIGHT: 27px">
<asp:TextBox id="Textbox2" runat="server" width="199px" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr height="20">
<td borderColor="#ffffff" colSpan="2"></td>
</tr>
<tr>
<td></td>
<td><input id="Submit1" type="submit" size="20" value=" 登陆 " name="B1" runat="server">
</td>
</tr>
</table>
</form>
</body>
</HTML>
还是用document.getElementById(“Country”).value吧,这个能拿到属性值。
以下摘自MSDN
getElementById Method
--------------------------------------------------------------------------------
Returns a reference to the first object with the specified value of the ID attribute.
Syntax
oElement = document.getElementById(sIDValue)
Parameters
sIDValue Required. String that specifies the value of an ID attribute.
Return Value
Returns the first object with the same ID attribute as the specified value.
Remarks
If the ID value belongs to a collection, the getElementById method returns the first object in the collection.
Example
This example uses the getElementById method to return the first occurrence of the ID attribute value, oDiv.
<SCRIPT>
function fnGetId(){
// Returns the first DIV element in the collection.
var oVDiv=document.getElementById("oDiv1");
}
</SCRIPT>
<DIV ID="oDiv1">Div #1</DIV>
<DIV ID="oDiv2">Div #2</DIV>
<DIV ID="oDiv3">Div #3</DIV>
<INPUT TYPE="button" VALUE="Get Names" onclick="fnGetId()">