惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
Forbes - Security
Forbes - Security
MongoDB | Blog
MongoDB | Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
About on SuperTechFans
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
腾讯CDC
P
Proofpoint News Feed
S
Schneier on Security
S
Secure Thoughts
V
Visual Studio Blog
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy International News Feed
SecWiki News
SecWiki News
S
SegmentFault 最新的问题
T
Threatpost
小众软件
小众软件
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
T
Tailwind CSS Blog
I
Intezer
C
CERT Recently Published Vulnerability Notes
U
Unit 42
V
V2EX
Cyberwarzone
Cyberwarzone
Recorded Future
Recorded Future
O
OpenAI News
Project Zero
Project Zero
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
Know Your Adversary
Know Your Adversary
C
Cybersecurity and Infrastructure Security Agency CISA
Scott Helme
Scott Helme
V2EX - 技术
V2EX - 技术
博客园 - 叶小钗
S
Securelist
A
Arctic Wolf
The Cloudflare Blog
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
博客园 - Franky

博客园 - nect

【转】ie9 rc版软件兼容问题 [z]前端设计IE6/IE7/IE8/IE9/FF问题汇总:IE和FirFox兼容问题 - nect - 博客园 [z]ie和FF 在insertRow和insertCell的区别 [z]JS在IE和FF下attachEvent,addEventListener学习笔记 - nect - 博客园 WinForm使用webclient通过http的POST方式上传文件 转:另类解决:“ScriptManager”不是已知元素。原因可能是网站中存在编译错误。 - nect - 博客园 转:css妙用1-用图片替换文字 .net中的正则表达式(一)——转义字符 使用Cascadingdropdown控件遇到的一个问题 IP地址匹配算法 服务器控件的客户端验证脚本 从一个字符串中Remove另一个字符串 gridview 中模板列无法响应row_command事件 - nect - 博客园 在asp.net Atlas中调用 Web Service时无法找到自定义的Web Service对象的可能原因 【转】Prototype.js开发笔记//mark一下 在GridView中使用邮件链接 【转】ASP.net2。0中解决无法获取 GridView 隐藏列值问题 GridView 中格式化整理 [导入]Java调用.net的WebService
[转]操纵自如--页面内的配合与通信
nect · 2007-10-08 · via 博客园 - nect

操纵自如--页面内的配合与通信

.NET的页面看似一个整体,却可能是由很多不同的区域组合而来的,常常用到的母版页、用户控件就是最鲜明的例子。
然而在一个页面内的元素要形成一个整体,就少不了控件之间的通信与传值,本文是个人在不断的使用过程中总结的一些东西,有谬误或有更好的解决方案,还请提出来。
在写这篇文章的过程中我做了一些一示例,以作佐证。这些示例的目的都是 在“A”中 把 “B”中 的一个Label的值改变。
这样的做法有什么意义?
比如说你在masterpage中含有一个GridView,在aspx改变了一些数据,而这些数据正是影响到masterpage中的GridView的呈现内容,那么你就有必要在aspx中通知masterpage更新了。本文的目的就是要说,如何去通知它的更新。
本文包括以下几个部分:
1、aspx与ascx的通信
2、master与aspx的之间的通信
3、master中的ascx 与 master中的aspx通信
4、ascx与ascx之间的通信

1、aspx与ascx的通信

一个简单的示例,在这个例子中,一共有两个文件:UserControl-Page.aspx和WebUserControl3.ascx,两个文件之中均含有一个TextBox、Label和一个Button
在这里,我们使用A和B来简称前者和后者。
要求的效果是:点击A中的Button,能将A中TextBox中的值赋给B中的Label;反之,点击B中的Butoon,要将B中TextBox中的值赋给A中的Label。
也就是说,它们两个能改变对方的控件内容。
首先说A->B
这是很简单的,在Ascx中写一个公共的方法,在aspx中调用就行了。


可能大家看得不是很明白,不过总之,在aspx中可以调用ascx中的公共方法,传入想传的参数,就OK了。

如果倒过来,由B->A。

在ascx中改变aspx中的一个Label,也只换种方式

 Label lblMessage= (Label)Page.FindControl("lblMessage");
 lblMessage.Text 
= txtValue.Text;

也就是说,可以在ascx中查找当前aspx页的控件ID,找到了,直接赋个值就行了。反正,只要找到这个控件,用起来就好像自己的一样。

2、master与aspx的通信

在master中访问aspx中的东西也是查找控件,和ascx中查找aspx中差不多

Label lblMessage= (Label)ContentPlaceHolder1.FindControl("lblMessage");
 lblMessage.Text 
= txtValue.Text;


倒过来,aspx可以调用masterpage的公共方法

 MasterPage master = (MasterPage)Page.Master;//转换为masterpage的类型
    master.setValue(txtValue.Text);//调用masterpage的方法


3、master中的ascx 与 master中的aspx通信

从master中的ascx到master中的aspx,需要通过master,查找ContentPlaceHolder,再查找Label

 MasterPage master = (MasterPage)Page.Master;
 Label _lblMessage 
= (Label)master.FindControl("ContentPlaceHolder1").FindControl("lblMessage");
 _lblMessage.Text 
= txtValue.Text;

aspx要想与masterpage的ascx联系,要先得到master,再查找ascx,再查找Label。

这个双方互通是一样的原理哦。

4、ascx与ascx之间的通信

这应该是最常见的情况,前段时间做的项目,分两个区域,用户在A区域从事活动,B区域记录下他活动的信息,这两个区域都是用户控件。
一种方法是从a.ascx中查找Aspx,再查找B.ascx,再查找Label。
这种方向理论上的行得通的,不过我没试,因为这种方法必须考虑B控件在A中的ID,我却不想与ID发生任何关系。
所以我用接口。
假设现有WebUserControl2.ascx想操纵WebUserControl1.ascx中的控件。
在app_code中新建一个IUserControl1.cs
内容:

public interface IUserControl1
{
    
void setSelect(string value);
}


在WebUserControl1.ascx继承该接口,并实现其方法。




public partial class UserControl_WebUserControl1 : System.Web.UI.UserControl, IUserControl1
{
    
public void setSelect(string _value)
    
{
        lblMessage.Text 
= _value;
    }

}

然后直接在webUserControl2.ascx调用webUserControl1.ascx中的方法

 IUserControl1 userControl1 = (IUserControl1)Page.FindControl("WebUserControl1_1");//转化到接口去
 userControl1.setSelect(txtValue.Text);//setSelect是webUserControl1.ascx中的方法,调用它。


这实际上是很好的一种方法,这样来做,其它类型的控件间通信应该都能实现,在那几天,我还一直沾沾自喜着呢。通过这一样一个例子,也发现接口确实不简单,真的是一个“接口”。

.net的partial类的引入使用我的可以在各个文件中实现一个,最后合并在一起,而通过这些小技巧,又可以把各部分联系起来,成为一个真正的“整体”。