ASP.NET(C#)中:其它类如何调用.ASPX.CS内的成员?

Jave.Lin 2009-02-07 02:53:41
如题

详细提问:
假如我自己新建了一些类文件(.CS)。

我想在该类文件.CS中写一些方法。
而该方法操作的涉及到某个页面文件.aspx内的对象。
如:我新建了:
这是ShowMsg.CS类文件的内容
using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// ShowMSG 的摘要说明
/// </summary>
public class ShowMSG
{
public ShowMSG()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public static void LetTheTextBoxShowMSG()
{
//这里要写处理代码
//处理Show.aspx页面中的TextBox显示:“Hello,World!”
//问题一:就是在这里不会,请问各位大哥大姐们帮帮忙。谢谢。
//问题二:或者是调用Show.aspx.cs中的var变量。
//在该方法内,将Show.aspx.cs中的var变量的值改为:"Hello,ASP.NET",如何代码实现呢?
}
}


这是Show.aspx页面类内容:
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 Show : System.Web.UI.Page
{
public static string var = "Hello,World";
protected void Page_Load(object sender, EventArgs e)
{
//调用ShowMSG类中的LetTheTextBoxShowMSG()函数
ShowMSG.LetTheTextBoxShowMSG();
}
}


这是Show.aspx的“源”视图内容
%@ Page Language="C#" AutoEventWireup="true" CodeFile="Show.aspx.cs" Inherits="Show" %>

<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="256px" Width="507px"></asp:TextBox></div>
</form>
</body>
</html>

就一个ASP.NET:textBox控件
...全文
535 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jave.Lin 2009-02-09
  • 打赏
  • 举报
回复
我用了机其它的机子试了可以。

但就是不知道为啥我自己的机子不可以。
Jave.Lin 2009-02-07
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ojlovecd 的回复:]
引用 12 楼 linjf520 的回复:
引用 11 楼 ojlovecd 的回复:
重新编译,重新生成网站,运行以后ctrl+F5刷新页面试试


我“重新生成解决方案”数次。

运行后Ctrl+F5数次,还是不行啊。

你把我的代码弄到你那里试试看啊。

我把你的代码原封不动拷过来,运行OK
[/Quote]

奇怪啊。

我的是什么都没有啊。

我用Response.Write()都没东西。

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 Show : System.Web.UI.Page
{
public static string var = "Hello,World";

protected void Page_Load(object sender, EventArgs e)
{
//调用ShowMSG类中的LetTheTextBoxShowMSG()函数
//ShowMSG.LetTheTextBoxShowMSG(ref TextBox1);
ShowMSG.LetTheTextBoxShowMSG(ref TextBox1, ref var);
Response.Write(TextBox1.Text+" TextBox1.Text的内容");
Response.Write(var);
}
}
我姓区不姓区 2009-02-07
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 linjf520 的回复:]
引用 11 楼 ojlovecd 的回复:
重新编译,重新生成网站,运行以后ctrl+F5刷新页面试试


我“重新生成解决方案”数次。

运行后Ctrl+F5数次,还是不行啊。

你把我的代码弄到你那里试试看啊。
[/Quote]
我把你的代码原封不动拷过来,运行OK
Jave.Lin 2009-02-07
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ojlovecd 的回复:]
重新编译,重新生成网站,运行以后ctrl+F5刷新页面试试
[/Quote]

我“重新生成解决方案”数次。

运行后Ctrl+F5数次,还是不行啊。

你把我的代码弄到你那里试试看啊。
我姓区不姓区 2009-02-07
  • 打赏
  • 举报
回复
重新编译,重新生成网站,运行以后ctrl+F5刷新页面试试
Jave.Lin 2009-02-07
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// ShowMSG 的摘要说明
/// </summary>
public class ShowMSG
{
public ShowMSG()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public static void LetTheTextBoxShowMSG(ref TextBox tb)
{
tb.Text = "Hello,World";
}

public static void LetTheTextBoxShowMSG(ref TextBox tb,ref string content)
{
tb.Text = content;
}
}


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 Show : System.Web.UI.Page
{
public static string var = "Hello,World";

protected void Page_Load(object sender, EventArgs e)
{
//调用ShowMSG类中的LetTheTextBoxShowMSG()函数
ShowMSG.LetTheTextBoxShowMSG(ref TextBox1);
ShowMSG.LetTheTextBoxShowMSG(ref TextBox1,ref var);//两个方法我都试过,还是不行。
}
}
我姓区不姓区 2009-02-07
  • 打赏
  • 举报
回复
不知道你是怎么写的,我这没有问题
Jave.Lin 2009-02-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ojlovecd 的回复:]
把out改为ref再试试
[/Quote]

改成ref引用后。

生成成功了。

但是没有效果。

运行后,TextBox1还是空空的。
我姓区不姓区 2009-02-07
  • 打赏
  • 举报
回复
把out改为ref再试试
Jave.Lin 2009-02-07
  • 打赏
  • 举报
回复
ShowMSG类:
using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// ShowMSG 的摘要说明
/// </summary>
public class ShowMSG
{
public ShowMSG()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public static void LetTheTextBoxShowMSG(out TextBox tb)
{
tb.Text = "Hello,World";
}

public static void LetTheTextBoxShowMSG(out TextBox tb, out string content)
{
tb.Text = content;
}
}

Show.aspx.cs中:
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 Show : System.Web.UI.Page
{
public static string var = "Hello,World";
protected void Page_Load(object sender, EventArgs e)
{
//调用ShowMSG类中的LetTheTextBoxShowMSG()函数
ShowMSG.LetTheTextBoxShowMSG(out TextBox1);
ShowMSG.LetTheTextBoxShowMSG(out TextBox1, "Hello,ASP.NET");
}
}


运行后有错误:

用out的问题。
我对out不了解,现在还是第一次用。
错误:

1 使用了未赋值的 out 参数“tb”

2 控制离开当前方法之前必须对 out 参数“tb”赋值

怎么解决呢?
Jave.Lin 2009-02-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ojlovecd 的回复:]
C# code
using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// ShowMSG 的摘要说明
/// </summary>
public class ShowMSG
{
public ShowMSG()
{
//
// TODO: 在此…
[/Quote]

大哥,麻烦一下。

还有一点问题:

用out的问题。
我对out不了解,现在还是第一次用。
错误:

1 使用了未赋值的 out 参数“tb”

2 控制离开当前方法之前必须对 out 参数“tb”赋值

不知道咋能
Jave.Lin 2009-02-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 net_lover 的回复:]
ShowMSG.LetTheTextBoxShowMSG(param);
传参数进去啊
[/Quote]

问题就是传过去后。怎么在LetTheTextBoxShowMSG()函数给Show.aspx中的TextBox1.Text赋值。

因为在ShowMSG中感觉调用不Show.aspx中的TextBox1.Text因为他的控件定义是在HTML CODE的,而不是C# CODE。

我现在就是不知道怎么在LetTheTextBoxShowMSG()处理。

调用该函数我会啊。

麻烦大哥弄一下LetTextBoxShowMSG()的源码看看。
我姓区不姓区 2009-02-07
  • 打赏
  • 举报
回复

using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// ShowMSG 的摘要说明
/// </summary>
public class ShowMSG
{
public ShowMSG()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public static void LetTheTextBoxShowMSG(out TextBox tb)
{
tb.Text = "Hello World";
}
public static void LetTheTextBoxShowMSG(out string str)
{
str = "Hello World Again";
}
}



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 Show : System.Web.UI.Page
{
public static string var = "Hello,World";
protected void Page_Load(object sender, EventArgs e)
{
//调用ShowMSG类中的LetTheTextBoxShowMSG()函数
ShowMSG.LetTheTextBoxShowMSG(out TextBox1);
ShowMSG.LetTheTextBoxShowMSG(out var);
}
}
孟子E章 2009-02-07
  • 打赏
  • 举报
回复
ShowMSG.LetTheTextBoxShowMSG(param);
传参数进去啊
Jave.Lin 2009-02-07
  • 打赏
  • 举报
回复
VS2005下弄的。

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧