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

推荐订阅源

N
News | PayPal Newsroom
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
H
Hacker News: Front Page
Apple Machine Learning Research
Apple Machine Learning Research
TaoSecurity Blog
TaoSecurity Blog
Help Net Security
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
Security Latest
Security Latest
Cloudbric
Cloudbric
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Know Your Adversary
Know Your Adversary
A
Arctic Wolf
L
LangChain Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
小众软件
小众软件
NISL@THU
NISL@THU
云风的 BLOG
云风的 BLOG
P
Privacy & Cybersecurity Law Blog
S
Security @ Cisco Blogs
博客园 - 【当耐特】
I
InfoQ
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Proofpoint News Feed
O
OpenAI News
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
量子位
宝玉的分享
宝玉的分享

博客园 - goodbaby

辞职了 爱上无聊 读程序有感 把网通的网关接口程序重写了 深深陷入困境 我的技术和观点 我的AOP初步 membership and roleship 我的asp.net 2.0初体验 一点技术,一点生活 简单体验多层应用 基于服务的架构 变化,感触 又可以进自己的blog了 浅谈asp.net UI 浅谈验证码 浅谈基于角色的安全 浅谈数据库里的自引用 简单的URL重写
静态和动态控件回递数据的处理差别
goodbaby · 2005-06-01 · via 博客园 - goodbaby

关于asp.net回递数据和视图的讨论在去年的joycdoe上如火如涂,我也从中学习了很多知识,这是由动态控件所引发的讨论,对学习asp.net受益非浅。其实在两年前的joycode上从那篇动态用户控件就开始,之后在今年的msdn上一篇很热的,讨论ViewState和动态控件的文章(作者Scott(也是.Text的作者同时也是MVP))),几乎都是动态控件引发然后就分析asp.net的页面模型。我想讨论的也是这个,是我在学习时遇到的问题,同时在这些文章中我没遇到类似的讨论。我用asp.net 2.0时候学习时是发现一个有趣的现象。
private void RefreshAvailableRolesListBox()
    {
        lbxAvailableRoles.SelectedIndex = -1;
        lbxAvailableRoles.DataSource = Roles.GetAllRoles();
        lbxAvailableRoles.DataBind();

        if (lbxAvailableRoles.Items.Count == 0)
        {
            lblRoleInfoText.Text = "当前没有角色";
            lbxAvailableRoles.Visible = false;
            btnDeleteRole.Visible = false;
        }
        else
        {
            lblRoleInfoText.Text = "角色列表";
            lbxAvailableRoles.Visible = true;
            btnDeleteRole.Visible = true;
        }
    }
protected void Page_Init(object sender, EventArgs e)
    {
        RefreshAvailableRolesListBox();
    } 
protected void btnDeleteRole_Click(object sender, EventArgs e)
    {
        lblResults.Text = "fire";
        if (lbxAvailableRoles.SelectedIndex != -1)
        {
            try
            {
                Roles.DeleteRole(lbxAvailableRoles.SelectedValue);

                lblResults.Text = null;
                lblResults.Visible = true;
            }
            catch (Exception ex)
            {
                lblResults.Text = "不能删除角色 " + Server.HtmlEncode(ex.Message);
                lblResults.Visible = true;
            }
        }
程序可以正确执行,当删除一 个角色的时候lbxAvailableRoles.SelectedIndex != -1为false,说明在 Page_Init(object sender, EventArgs e)后lbxAvailableRoles.SelectedIndex 静态控件的postdata被处理了,这是没问题的。但我这样
protected void Page_Load(object sender, EventArgs e)
    {
        RefreshAvailableRolesListBox();
    } 
将RefreshAvailableRolesListBox();放在这里,执行结果是lbxAvailableRoles.SelectedIndex != -1为true,我以为是asp.net 2.0的原因,但我在2003上试也是同样的结果,我们知道在Page_Load后有一次PostData处理请看下面的.net源代码。
         this.Trace.Write("aspx.page", "Begin ProcessPostData");
                  }
                  this.ProcessPostData(this._requestValueCollection, true);
                  if (context1.TraceIsEnabled)
                  {
                        this.Trace.Write("aspx.page", "End ProcessPostData");
                  }
            }
            base.LoadRecursive();
            if (this.IsPostBack)
            {
                  if (context1.TraceIsEnabled)
                  {
                        this.Trace.Write("aspx.page", "Begin ProcessPostData Second Try");
                  }
                  this.ProcessPostData(this._leftoverPostData, false);
                  if (context1.TraceIsEnabled)
                  {
                        this.Trace.Write("aspx.page", "End ProcessPostData Second Try");
                        this.Trace.Write("aspx.page", "Begin Raise ChangedEvents");
                  }
去掉跟踪可以看出进行了两次PostData处理,但现在为什么不对呢。然后我把静态控件换成动态控件
private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   RefreshAvailableRolesListBox();
   Form1.Controls.Add(lbxAvailableRoles1);
   RefreshAvailableRolesListBoxDy();
  }
好,work了,在Page_Load后动态控件的PostData被正确设置,为什么动态控件被正确设置了而静态控件不对呢。看下面的.net源代码片段:

找了半天,原因我分析得出的就是,静态控件在第一次PostData处理的时候注意这句this._controlsRequiringPostBack.Remove(text1);text1是控件名称,这意思就是说PostData处理了你就该不再被下一次处理所以从controlsRequiringPostBack里删除,也就是说在之后的Page_Load后处理过的静态控件不再不处理,好了问题解决了,当然如果你的动态控件放在其他地方又是另一会事,这里真对这种情况。也许你有更好的解释,我们一起讨论。下次我想讨论可复用的对象模型,谈一点体会,学习心得。
6-1祝所有的小朋友节日快乐,也祝大家天天开心。