我想做个最简单的例子,实现以下功能。
在一个画面上画一个TextBox和一个Button,当点Button时,通过Javascript来调用Button的ButtonClick事件,在这个事件中将TextBox中的值设置为1。
整个过程不刷新。请问怎么实现?
希望给出简单代码,推荐一些简单的文章!谢谢
在一个画面上画一个TextBox和一个Button,当点Button时,通过Javascript来调用Button的ButtonClick事件,在这个事件中将TextBox中的值设置为1。
整个过程不刷新。请问怎么实现?
希望给出简单代码,推荐一些简单的文章!谢谢
解决方案 »
- aspnetpage分页后对内容分类
- 求一个关联表的sql
- 现在和master在同一级目录下的内容页能显示图片、css,和master不再同以及目录下的内容也就没有办法显示图片了,css也是用不了,怎么办?
- 使用WAS进行性能测试时,Socket Errors 有很多
- █ 新建了一个asp.net2.0的技术群,有兴趣的朋友请加 17307623 █
- 请问ViewState与Session的区别?
- 未能加载视图状态。是何原因?
- 寻找日历控件
- 求microsoft sql server 2005 软件下载地址
- 请问这里谁是斑竹?能留email地址吗?
- 我是用vs2003,微软的treeview控件,想显示tooltip,该如何实现
- 弹出对话框问题!
支持一下你的贴子
那就请大侠们赏个小例子吧,说查msdn的,谁都知道的啦,找到了就不用来问了.
TextBox有一个ClientID属性,就是客户端生成的ID,可以用JS控制他的VALUE值,
TextBox有一个ClientID属性,就是客户端生成的ID,可以用JS控制他的VALUE值,
-------------------------------
下面是我写的代码,但这样是刷新的, 怎么做到无刷新的呢?<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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">
<script language ="c#" runat ="server" >
void button1_Click(object sender, EventArgs e)
{
this.label1.Text = "这样写是刷新的";
}</script>
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID ="button1" Text ="请点我测试是不是刷新的" OnClick ="button1_Click" runat ="server" />
<asp:Label ID = "label1" runat ="server" ></asp:Label>
</div>
</form>
</body>
</html>
--------------------
<head runat="server">
<title>无标题页</title>
<script type="text/jscript">
function aj()
{
pay=document.getElementById("te").value;
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//创建XMLHTTPRequest对象,需MSXML4.0支持 ["MSXML2.XMLHTTP.4.0","MSXML2.DOMDocument.4.0"]
xmlhttp.open("GET","Default12.aspx",false,"",""); //使用HTTP GET初始化HTTP请求
xmlhttp.send(); //发送HTTP请求并获取HTTP响应
if(xmlhttp.responseText=='1') //获取返回结果字符串
{
alert("插入数据成功!");
}
else
{
alert("插入数据没有成功!");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="te" />
<input type="button" onclick="aj()" />
</div>
</form>
</body>
</html>public partial class Default12 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write( "1");
}
}
既没有ClientId这个属性 也没有value这个属性,
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function PagePostBack(){
var arg="";
var content=document.getElementById("tb");
//注册回调脚本
<%=ClientScript.GetCallbackEventReference(this,"arg","ShowClientContext","content")%>
}function ShowClientContext(result,context){
context.value=result;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="tb" />
<input type="button" onclick="PagePostBack()" value="postback" />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;//实现ICallbackEventHandler接口
public partial class Default3 : System.Web.UI.Page, ICallbackEventHandler
{
public string _Result; protected void Page_Load(object sender, EventArgs e)
{ }
//ICallbackEventHandler接口要求必须提供以下两种方法
public void RaiseCallbackEvent(string argu)
{
_Result = "1";
}
public string GetCallbackResult()
{
return _Result;
}}
怎么知道自己的环境支持不支持这个呢?要是没有, 怎么加入引用?
的例子我也做了一遍,恩,可以用,我看到效果了
Default12.aspx只留这,另外的可能与浏览器有关,网上看看
<head runat="server">
<title>Untitled Page</title>
<atlas:ScriptManager ID="ScriptManager1" runat="server">
</atlas:ScriptManager>
<script type="text/javascript">
function RequestWebMethod(){
PageMethods.Greeting(onCallback);
}
function onCallback(result){
//document.getElementById("divResult").innerText=result;
document.getElementById("<%=TextBox1.ClientID%>").value=result;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Hello:<asp:TextBox ID="TextBox1" runat="server" Text="World"></asp:TextBox>
<input id="Button1" type="button" value="button" onclick="RequestWebMethod();" />
<div id="divResult">
</div>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class PageMethod : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public string Greeting() {
return "1";
}
}
HelloWorld.asmx:
================using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for HelloWorld
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class HelloWorld : System.Web.Services.WebService { private static string[] autoCompleteWordList = null;
public HelloWorld () { //Uncomment the following line if using designed components
//InitializeComponent();
} [WebMethod]
public string Greeting(string username) {
return "Hello "+username;
}
}
========aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Hello.aspx.cs" Inherits="Hello" %>
<!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>Hello world</title>
<script type="text/javascript">
function NavigateWebService(){
var username=document.getElementById("tb_UserName").value;
HelloWorld.Greeting(username,OnRequestComplete);
}
function OnRequestComplete(result){
document.getElementById("Div_Result").innerText=result;
}
</script>
<!--首先这儿要放置一个脚本管理器,并引用webservice-->
<atlas:ScriptManager runat="Server" id="ScriptManager">
<Services>
<atlas:ServiceReference Path="HelloWorld.asmx" />
</Services>
<ErrorTemplate>
</ErrorTemplate>
<Scripts>
</Scripts>
</atlas:ScriptManager>
</head>
<body>
<form id="form1" runat="server">
hello <input type="text" id="tb_UserName" value="world" />
<input type="button" value="访问Web服务" onclick="NavigateWebService();"/><br />
这儿是从服务端返回的结果:<div id="Div_Result">
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default12.aspx.cs" Inherits="Default12" %>
----------------我用的是Default.aspx.cs
xmlhttp.open("GET","Default12.aspx",false,"",""); //使用HTTP GET初始化HTTP请求
这里也做了相应的修改, 还是没有成功,另外
public partial class Default12 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write( "1");
}
}
你在Page_Load里写1,结果页面一出来就出来一个1了,而且点按钮时还是插入数据不成功,是你程序问题还是我环境问题,要不你将程序发给我?
里面有我的一个例子,可以去看看
他的例子也基本可用
只是有两个地方要改一下
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/jscript">
function aj()
{
pay=document.getElementById("te").value;
//这个对象并不一定总能创建成功
//因为在firefox/netscape中是使用
//var xmlhttp=new XMLHttpRequest来创建xmlhttprequest对象的
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//创建XMLHTTPRequest对象,
需MSXML4.0支持 ["MSXML2.XMLHTTP.4.0","MSXML2.DOMDocument.4.0"] xmlhttp.open("GET","Default12.aspx",false,"",""); //使用HTTP GET初始化HTTP请求
xmlhttp.send(); //发送HTTP请求并获取HTTP响应
//send后不能马上判断responseText
//因为有网络延时,应该使用回叫,或者等xmlhttprequest对象的状态变更
//在回叫中判断xmlhttp.readyState==READY_STATE_COMPLETE 再来alert
if(xmlhttp.responseText=='1') //获取返回结果字符串
{
alert("插入数据成功!");
}
else
{
alert("插入数据没有成功!");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="te" />
<input type="button" onclick="aj()" />
</div>
</form>
</body>
</html>