<%@PageLanguage="C#"AutoEventWireup="true"MasterPageFile="~/Default.master"CodeFile="Default.aspx.cs"Inherits="_Default"%>

黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

當PreviousPage.FindControl遇到MasterPage,如

系統 2201 0

本文轉自: http://www.sqlsky.com/asp-net/070721/585/

有以下兩個頁面Default.aspx和Result.aspx,代碼如下:
<!--?Default.aspx?-->
<%@?Page?Language="C#"?AutoEventWireup="true"?MasterPageFile="~/Default.master"??CodeFile="Default.aspx.cs"?Inherits="_Default"?%>?
<asp:Content?ID="Content1"?runat="server"?ContentPlaceHolderID="ContentPlaceHolder1">
?????<asp:Label?ID="Label1"?runat="server"?Text="Please?input?a?string?here"></asp:Label>
????<asp:TextBox?ID="TextBox1"?runat="server"></asp:TextBox>
????<asp:Button?ID="Button1"?runat="server"?Text="Button"?PostBackUrl="~/Result.aspx"?/></asp:Content>

//Default.aspx.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;

public?partial?class?_Default?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????}
}
?

<!--?Result.aspx?-->
<%@?Page?Language="C#"?MasterPageFile="~/Default.master"?AutoEventWireup="true"?CodeFile="Result.aspx.cs"?Inherits="Result"?Title="Untitled?Page"?%>
<asp:Content?ID="Content1"?ContentPlaceHolderID="ContentPlaceHolder1"?Runat="Server">
????<asp:Label?ID="Label1"?runat="server"?Text="The?string?you?input?in?the?previous?page?is"></asp:Label>
????<asp:TextBox?ID="TextBox1"?runat="server"></asp:TextBox>
</asp:Content>?
//Result.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?Result?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????if?(PreviousPage?!=?null)
????????{
????????????TextBox?tb?=?(TextBox)PreviousPage.FindControl("TextBox1");
????????????if?(tb?!=?null)
????????????????TextBox1.Text?=?tb.Text;
????????}
????}
}
?

這兩個頁面都指定了MasterPageFile屬性。因為該MasterPage中的內容無關緊要,就不列出來了。在Default.aspx上有兩個控件:TextBox1用于接受用戶的輸入,Button1用于提交頁面,其PostBackUrl指向Result.aspx。在Result.aspx.cs的Page_Load方法中嘗試在TextBox1中顯示用戶在前一頁面的TextBox1中輸入的字符串。當執行以下語句時:

TextBox?tb?=?(TextBox)PreviousPage.FindControl("TextBox1");

tb的值為null。將以上語句更改為如下代碼:

Content?con?=?(Content)PreviousPage.FindControl("Content1");
if?(con?==?null)
????return;

TextBox?tb?=?(TextBox)con.FindControl("TextBox1");

但con的值為null,這樣后續的語句也不可能執行了。問題出在哪里呢?

經過一番搜索,在forums.asp.net中找到了答案,以下引用的是bitmask的說法:
...becasue?the?Content?controls?themselves?dissapear?after?the?master?page?rearranges?the?page.?You?can?use?the?ContentPlaceHolders,?or?the?<form>?on?the?MasterPage?if?there?are?no?INamingContainers?between?the?form?and?the?control?you?need.?


所以以上的代碼應該改成:

TextBox?tb?=?(TextBox)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1");

bitmask還在他的博客上寫了一篇文章來闡述FindControl方法和INamingContainers接口:
http://www.odetocode.com/Articles/116.aspx

在他的另一篇文章里給出了一張圖片,顯示出了運行時母版頁與內容頁合并后的頁面上各個控件的關系,我冒昧地貼在這里了:
當PreviousPage.FindControl遇到MasterPage,如何查找控件
原文: http://odetocode.com/Blogs/scott/archive/2006/01/12/2726.aspx ?

當PreviousPage.FindControl遇到MasterPage,如何查找控件


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論