梦想网络

Good good study Day day up

导航

【HELP】引用母版后提示 未将对象引用设置到对象的实例

Posted on 2007-05-08 19:24  张三  阅读(1222)  评论(0)    收藏  举报

我在页面中 引用母版后
进行操作的时候就提示 未将对象引用设置到对象的实例
去掉后又正常,错在什么地方了?修改方法?
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="Manager.Master" CodeBehind="AboutModify.aspx.cs" Inherits="WebSite.Manage.AboutModify" %>
<%@ Register TagPrefix="ftb" Namespace="FreeTextBoxControls" Assembly="FreeTextBox" %>
<asp:Content ContentPlaceHolderID="ContentRight" ID="content1" Runat="Server">
    <div>
        <a href="AboutManage.aspx">返回上一页</a><br />
        标 题 :<asp:TextBox ID="TextBoxFirstName" runat="server"></asp:TextBox><br />
        内&nbsp; 容 :<FTB:FREETEXTBOX id="FreeTextBoxContent" runat="server" AutoConfigure="Default" ButtonPath="../Images/office2003/"
        ImageGalleryPath="../Upload/" ButtonType="Image" HelperFilesPath="Helper/" Width="600px"></FTB:FREETEXTBOX><br />
        关键字:<asp:TextBox ID="TextBoxTag" runat="server"></asp:TextBox><br />
        <asp:Button ID="ButtonAdd" runat="server" Text="确定" OnClick="ButtonAdd_Click" />
        </div>
</asp: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;
using DB;

namespace WebSite.Manage
{
    public partial class AboutModify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Id"] != null)
                {
                    this.upbind();
                }

            }
        }

        void upbind()
        {
            MyControl Ctrl = new MyControl();
            Feng7_About About = new Feng7_About();
            DataView mydv = About.About_List(QueryString.GetId);
            TextBoxFirstName.Text = mydv[0]["FirstName"].ToString();
            FreeTextBoxContent.Text = mydv[0]["Content"].ToString();
            TextBoxTag.Text = mydv[0]["Tag"].ToString();
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
                Feng7_About About = new Feng7_About();
                string FirstName, Content, Tag;
                FirstName = ((TextBox)FindControl("TextBoxFirstName")).Text;
                Content = F7.FunStr(this.FreeTextBoxContent.Text);
                Tag = ((TextBox)FindControl("TextBoxTag")).Text;
            if (Request.QueryString["Id"] != null)
            {
                string ChangeTime;
                ChangeTime = DateTime.Now.ToString();
                About.About_UPDATE(QueryString.GetId, FirstName, Content, Tag, ChangeTime);
                Response.Write("<script>alert('修改成功')</script>");
            }
            else
            {
                string  UpdateTime;
                UpdateTime = DateTime.Now.ToString();
                About.About_INSERT(FirstName, Content, Tag, UpdateTime);
                Response.Write("<script>alert('添加成功');window.location.href='AboutManage.aspx'</script>");
            }
        }
    }
}

自己解决了

在.net 2.0 中如果使用了masterPage,则不能像以前那样使用 FindControl,则需要使用如下方法

先找到ContentPlaceHolder,然后再找在这个ContentPlaceHolder中的你要找的控件


1
2//MasterPage 中的ContentPlaceHolder ID
3string masterPageContentPlaceHolderID = "";
4//在 masterPageContentPlaceHolderID 中所要找到的控件的 ID
5string m_strSetDataToID = "";
6//例如找 TextBox
7TextBox textBoxFind = (TextBox)this.Page.Master.FindControl(masterPageContentPlaceHolderID).FindControl(m_strSetDataToID);

原文 http://www.cnblogs.com/forward/articles/masterpage.html