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

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - fisherman

学习写第一个SQL server触发器 一些DIV+CSS 命名规范 asp.net 2.0中生成RSS 避免重复提交 - fisherman - 博客园 button 删除确认 - fisherman - 博客园 用相对定位和负向移动完成图片象框阴影 负边距居中法(水平居中、垂直居中) - fisherman - 博客园 CSS 代码格式化工具 - fisherman - 博客园 JS代码的格式化和压缩 - fisherman - 博客园 把SQL SERVER里表里的数据导出成为insert into 脚本 对FckEditor编辑器在MAXTHON浏览器下选择服务器文件对话框显示不正常的改进 下载网页中远程图片的方法 读取marc数据 关于ISO2709数据的格式说明 C#写的读取ISO2709格式数据的DLL 数据库打开 使用 Engine 对象创建 SQL Server Compact Edition 数据库 用SharpZipLib压缩和解压缩文件 Access数据库压缩和修复
操纵自如--页面内的配合与通信
fisherman · 2007-12-20 · via 博客园 - fisherman

.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中调用就行了。

//这是在ascx中的方法,该方法将参数_value的值赋给Label。
    public void setSelect(string _value)
    {
        lblMessage.Text = _value;
    }

    protected void btnSet_Click(object sender, EventArgs e)
    {
        WebUserControl3_1.setSelect(txtValue.Text);// WebUserControl3_1是用户控件的ID
    }


可能大家看得不是很明白,不过总之,在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。

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

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

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

来源:

http://99love.blueidea.com/archives/2007/5312.shtml