当前位置: 代码迷 >> JavaScript >> 高手莫笑,小弟我对JavaScript不太了解:请教ASP.NET中如何获取TextBox
  详细解决方案

高手莫笑,小弟我对JavaScript不太了解:请教ASP.NET中如何获取TextBox

热度:204   发布时间:2012-02-19 19:43:37.0
高手莫笑,我对JavaScript不太了解:请问ASP.NET中怎么获取TextBox
高手莫笑,我对JavaScript不太了解:

------解决方案--------------------
怎么获取TextBox?
1、在ASP.NET中
C#:this.TextBoxID
VB:me.TextBoxID
2、JavaScript
document.getElementById( "TextBox ID Or Name ");
------解决方案--------------------
ASP.NET 应该是从POST中的数据筛选的...那跟JS没关系,
比如你用ASP 表单POST提交时, 就可以知道XX控件的内容...
------解决方案--------------------
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "textbox.aspx.cs " Inherits= "textbox " %>

<!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> 无标题页 </title>
<script language= "javascript ">
function foo()
{
var t1=document.getElementById( "txtOutPort ");
var t2=document.getElementById( "txtDigital ");
var t3=document.getElementById( "txtSimulate ");
if(Number(t1.value)> 512||Number(t2.value)> 512||Number(t3.value)> 512)
{
alert( "请不要大于512 ");
}
if((Number(t1.value)+Number(t2.value)+Number(t3.value))> 512)
{
alert( "三个数和不能大于512 ");
}
//alert(Number(t1.value));
}

</script>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:TextBox ID= "txtOutPort " runat= "server "> </asp:TextBox>
<asp:TextBox ID= "txtDigital " runat= "server "> </asp:TextBox>
<asp:TextBox ID= "txtSimulate " runat= "server "> </asp:TextBox>
<input type= "button " value= "提交 " id= "button1 " onclick= "foo() "/>
</div>
</form>
</body>
</html>
------解决方案--------------------
<asp:TextBox ID= "txtOutPort " runat= "server "> </asp:TextBox>
<asp:TextBox ID= "txtDigital " runat= "server "> </asp:TextBox>
<asp:TextBox ID= "txtSimulate " runat= "server "> </asp:TextBox>
<input type= "button " value= "提交 " id= "button1 " onclick= "foo() "/>

<script language= "javascript ">
function foo(v) //服务器端控件的ID与客户端页面里的ID是不一样的
{
var t1=document.getElementById( " <%= txtOutPort.ClientID %> ");
var t2=document.getElementById( " <%= txtDigital.ClientID %> ");
var t3=document.getElementById( " <%= txtSimulate.ClientID %> ");
alert(t1.value);
}
</script>
------解决方案--------------------
在ASP.NET写JS来验证表单,主要是表单的ID要写好
不是我们正常的TextBox1
而是类似aspnetForm.CtrlWantJob1_TextBox1
aspnetForm:是当前表单名,
CtrlWantJob1,是我的所属的用户自定义控件的表单名
TextBox1才是我们正常的文本框名字.

这个可以具体的在最后浏览的IE里查看源文件, 看表单的最终具体名称。

举例:
if (aspnetForm.CtrlWantJob1_TBName.value== " ") {alert( "请输入姓名! "); aspnetForm.CtrlWantJob1_TBName.focus();return false;alert( "dsfdsafsfsf ");}
  相关解决方案