System.NullReferenceException:对象引用未设置为对象iframe src的实例

时间:2013-04-28 05:38:48

标签: asp.net iframe

我通过ASP.NET C#传递iframe的src值时收到此错误。这里isrc是一个字符串变量:

Line 26:         HtmlControl   ifm = (HtmlControl)this.FindControl("ifm");
Line 27:         if (cover_id != null)
Line 28:         ifm.Attributes["src"] = isrc;
Line 29:         //Response.Redirect("Default2.aspx")
Line 30:     }

    \Default2.aspx.cs    Line: 28
    Source File: c:\Users\arsha\Documents\Visual Studio 2008\WebSites\hajcover\Default2.aspx.cs    Line: 28

堆栈追踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   Default2.btn1_Click(Object sender, EventArgs e) in c:\Users\arsha\Documents\Visual Studio 2008\WebSites\hajcover\Default2.aspx.cs:28
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


Version Information: Microsoft .NET Framework Version:2.0.50727.4984; ASP.NET Version:2.0.50727.4971

1 个答案:

答案 0 :(得分:0)

要访问服务器端的控件,需要标记runat="server"

将您的iframe更改为;

<iframe name="ifm" id='ifm' width="100%" height="680" runat="server" 
        style="background-color:#FFFFFF; border:solid #999999 1px"> </iframe>

然后将代码更改为只是立即访问控件;

// Next line is not needed, control is automatically available with runat="server"
// HtmlControl ifm = (HtmlControl)this.FindControl("ifm");
if (cover_id != null)
ifm.Attributes["src"] = isrc;

......你应该被设置。