-1

我在通过 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
4

1 回答 1

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;

...你应该被设置。

于 2013-04-28T05:59:17.953 回答