当前位置:编程学习 > C#/ASP.NET >>

asp.net中一个A页面(没打开),一个B页面(打开),A页面后台如何给B页面中Label赋值???

B页面是打开的(其中有一个Label),A页面没打开,通过ajax请求A页面并传入一个参数,现在想使B页面中的一个Label的值显示为这个参数的值。请问如何实现??? --------------------编程问答--------------------   //打开B页面通过URL 传值到B页面 B页面page_load接收
  //ajax post?过去 然后request接收 显示 --------------------编程问答--------------------
引用 1 楼 wxr0323 的回复:
//打开B页面通过URL 传值到B页面 B页面page_load接收
  //ajax post?过去 然后request接收 显示

B页面已经打开,另外在A页面的后台你怎么通过url打开B页面(因为A页面没打开,没窗体,所以不能用js) --------------------编程问答-------------------- 我用了这个方法:添加一个C类,C类中有一个事件,A页面后台调用这个事件(传入一个参数),B页面后台注册这个事件,并绑定一个方法Method。现在能触发这个B页面后台这个Method方法。但是我不能把这个参数显示到B页面前台去,方法中为B页面中的Label赋值没有反映,因为B页面没重新从服务端获取数据。

卡在这个问题上了,有人帮忙解决不??? --------------------编程问答-------------------- A页面:
using System;
using System.Web;

public partial class A : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["ajaxType"] != null && Request["ajaxType"] == "updateLabel")
{
this.Controls.Clear();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(DateTime.Now.ToString());
}
}
}

B页面前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="B.aspx.cs" Inherits="B" %>

<!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 src="common.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<input type="button" value="更新" onclick="updateLabel('Label1')" />
</div>
</form>
</body>
</html>

A页面后台:
using System;
using System.Web;

public partial class A : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["ajaxType"] != null && Request["ajaxType"] == "updateLabel")
{
this.Controls.Clear();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(DateTime.Now.ToString());
}
}
}

common.js文件:
var ajaxRequest;
function ajaxUpdate(Xurl, Xdata, XajaxCode)
{
if (window.ActiveXObject)
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
if (window.XMLHttpRequest)
ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function()
{
if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200)
XajaxCode();
}
if (Xdata == null)
ajaxRequest.open("get", Xurl, true);
else
ajaxRequest.open("post", Xurl, true);
ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxRequest.send(Xdata);
}
function updateLabel(XelementId)
{
var ajaxCode = function()
{
document.getElementById("Label1").innerHTML = ajaxRequest.responseText;
}
ajaxUpdate("A.aspx?ajaxType=updateLabel", null, ajaxCode);
}
--------------------编程问答-------------------- A页面后台发重复了,按照这个做就行了。
--------------------编程问答--------------------
引用 4 楼 dalmeeme 的回复:
A页面:
C# code
using System;
using System.Web;

public partial class A : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["ajaxType"] != null &a……

--------------------编程问答-------------------- 楼上的js代码有点乱。要注意一点的是我不是在B页面通过ajax去请求的A页面,不然我不是发神经吗?直接在B页面前台去给后台的label赋值就是。。。。

请看清我的问题 再回答 谢谢!!! --------------------编程问答-------------------- 是当满足一定条件之后(收到客户端得数据)才ajax请求A页面,这时我再在B页面呈现我收到的数据。 --------------------编程问答-------------------- 顶上  高手给个解决方案 谢谢。。。 --------------------编程问答-------------------- var ajaxRequest;
function ajaxUpdate(Xurl, Xdata, XajaxCode)
{
    if (window.ActiveXObject)
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    if (window.XMLHttpRequest)
        ajaxRequest = new XMLHttpRequest();
    ajaxRequest.onreadystatechange = function()
    {
        if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200)
            XajaxCode();
    }
    if (Xdata == null)
        ajaxRequest.open("get", Xurl, true);
    else
        ajaxRequest.open("post", Xurl, true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajaxRequest.send(Xdata);
}
正解 +1 --------------------编程问答-------------------- 真不懂什么意思?  这样的代码正解。。。。。 --------------------编程问答--------------------
引用楼主 wzwyuyi 的回复:
B页面是打开的(其中有一个Label),A页面没打开,通过ajax请求A页面并传入一个参数,现在想使B页面中的一个Label的值显示为这个参数的值。请问如何实现???

ajax执行前就可以用javascript代码直接给B上的label赋值啊.

当然你用B+ajax发参数到A,A页收到参数,再response这个参数值,ajax的callback函数收到后再给B的label赋值也可以.不过这是乎绕了一大圈子 --------------------编程问答--------------------
引用 7 楼 wzwyuyi 的回复:
楼上的js代码有点乱。要注意一点的是我不是在B页面通过ajax去请求的A页面,不然我不是发神经吗?直接在B页面前台去给后台的label赋值就是。。。。

请看清我的问题 再回答 谢谢!!!

你的问题中根本没讲明不是在B页通过ajax去请求A,要人家怎么去看清?

回到你的问题:
假设你发出ajax请求的页面叫C页面.
如果C与B之间没有任何联系(譬如由B打开C页或由C页面打开B页面)
那么只能找一个中间人
方法1:用cookie, A存入COOKIE, B用js定时读cookie
方法2:用session(这个要求B页与C页之间仍存在着联系)
方法3:用网站服务器,譬如application或某个全局对象.A页将参数存入这个全局对象,B页另发一个ajax,定时轮循服务器的这个全局对象.得到参数后,赋给label
      




--------------------编程问答--------------------
引用 13 楼 newdigitime 的回复:
引用 7 楼 wzwyuyi 的回复:
楼上的js代码有点乱。要注意一点的是我不是在B页面通过ajax去请求的A页面,不然我不是发神经吗?直接在B页面前台去给后台的label赋值就是。。。。

请看清我的问题 再回答 谢谢!!!

你的问题中根本没讲明不是在B页通过ajax去请求A,要人家怎么去看清?

回到你的问题:
假设你发出ajax请求的页面叫C页面.
如果C与B之间没有任……


说的相当正确。为何不用全局变量。。。 --------------------编程问答-------------------- 不知道楼主这样干想干什么?不打开A页传送值给A页有什么意义? --------------------编程问答-------------------- mark!
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,