Thakns对此感到困惑,对此感到困惑。当我的用户控件试图引用占位符时,它会抛出null。
我收到了错误:
((ContentPlaceHolder)Page.Master.FindControl("JavascriptIncludes")).Controls.Add(JSIncludes);
System.ArgumentNullException: Value cannot be null.
这是我的网络用户控制。这里的空值是我试图添加的JSIncludes
控件。它存在于Web用户控件中!如果我把它分解为:
Literal NewLit = new Literal();
NewLit.Text = "TESTING";
JSIncludes.Controls.Add(NewLit);
我明白了:
System.NullReferenceException: Object reference not set to an instance of an object.
JSIncludes.Controls.Add(NewLit);
它位于我的web.config控件下:
<add tagPrefix="Scirra" src="~/Controls/Article/ArticleSubmitForm.ascx" tagName="ArticleSubmitForm"/>
snip
<script src="<%=ResolveClientUrl("~/js/scrollTo-min.js")%>"></script>
<asp:ContentPlaceHolder id="JavascriptIncludes" runat="server"></asp:ContentPlaceHolder>
</form>
</body>
</html>
我的教程页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Tutorials.aspx.cs" Inherits="Tutorials" MasterPageFile="MasterPages/Main.master" %>
<%@ MasterType VirtualPath="~/MasterPages/Main.master" %>
<%@ Reference Control="~/Controls/Article/ArticleSubmitForm.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Placeholder runat="server" ID="DefaultView" Visible="false">
<div class="center">
<h1>Tutorials</h1><br />
</div>
</asp:Placeholder>
<asp:Placeholder runat="server" ID="SubmitView" Visible="false">
<div class="center">
<h1>Submit a Tutorial</h1><br />
</div>
<asp:PlaceHolder runat="server" ID="ArticleSubbmitter" />
</asp:Placeholder>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Tutorials : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string Section = Request.QueryString["section"];
if (Section == "submit")
{
// Menu selection
Master.SetTopMenuSelected("tut");
Master.SetSubMenuSelected("submit");
// Page meta data
Page.Title = Meta.TUTORIAL_SUBMIT_TITLE + Meta.TITLE_SUFFIX; ;
Master.AddMetaDescription(Meta.TUTOTIAL_SUBMIT_DESCRIPTION);
// Canonicial meta link
string ThisPageURL = Settings.MasterDomainRoot + "/tutorials/submit";
Master.AddCanonical(ThisPageURL);
SubmitView.Visible = true;
// Forced tags for input
Controls_ArticleSubmitForm SubmitForm = new Controls_ArticleSubmitForm();
SubmitForm.ForcedTags = new string[] { "Tutorials" };
SubmitForm.Section = "Tutorials";
SubmitForm.ThisUser = Master.ThisUser;
ArticleSubbmitter.Controls.Add(SubmitForm);
}
else
{
// Menu selection
Master.SetTopMenuSelected("tut");
Master.SetSubMenuSelected("home");
// Page meta data
Page.Title = Meta.TUTORIAL_TITLE + Meta.TITLE_SUFFIX; ;
Master.AddMetaDescription(Meta.TUTORIAL_DESCRIPTION);
// Canonicial meta link
string ThisPageURL = Settings.MasterDomainRoot + "/tutorials";
Master.AddCanonical(ThisPageURL);
DefaultView.Visible = true;
}
}
}
控制本身:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ArticleSubmitForm.ascx.cs" Inherits="Controls_ArticleSubmitForm" %>
<div class="article-edit-wrapper">
<h3>Title</h3>
<asp:TextBox runat="server" ID="ArticleTitle" CssClass="nice-textbox wide" MaxLength="65" /><br /><br />
<h3>Body</h3>
<asp:TextBox runat="server" ID="ArticleInput" CssClass="article-edit-area" TextMode="MultiLine" />
<div id="DraftNotice" style="float:left"></div>
<div style="float:right">Auto Preview <input type="checkbox" checked="checked" id="AutoPreview" onchange="UpdatePV(this)" /></div>
<div class="clear"></div><br />
<h3>Preview</h3>
<div id="PreviewArea" class="article-preview"></div>
<br />
<h3>Tags</h3>
<Scirra:TagEntry runat="server" ID="TagInput" />
<br /><asp:Button runat="server" ID="SubmitButton" CssClass="nice-button" Text="Submit" />
</div>
<asp:PlaceHolder runat="server" ID="CSSIncludes">
<link rel="stylesheet" href="<%=ResolveClientUrl("~/css/article.css")%>" />
</asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="JSIncludes">
<script src="<%=ResolveClientUrl("~/js/FieldSelect.js")%>"></script>
<script src="<%=ResolveClientUrl("~/js/KBShortcuts.js")%>"></script>
<script src="<%=ResolveClientUrl("~/js/ArticleCommon.js")%>"></script>
<script>
var ArticleAJAXURL = "<%=ResolveClientUrl("~/Handlers/ArticlesAjaxHandler.ashx")%>";
var Section = "<%=Section%>";
</script>
<script>
// Called when edit area is modified
function UpdatePreview() {
if (AutoPreview) {
// Get relevent items
var PreviewArea = $('#PreviewArea');
var InputTxt = $('textarea#ArticleInput').val();
// Update preview
PreviewArea.html(FormatInput(InputTxt));
}
}
var R = 1;
shortcut.add("Ctrl+H", function () {
PerformShortcut("[", "][" + (R++) + "]", "Enter link description");
});
// Function called to save draft
var LastSavedTitle = "";
var LastSavedBody = "";
function SaveDraft() {
var Body = $.trim($('textarea#ArticleInput').val());
var Title = $.trim($('#ArticleTitle').val());
if ((Body.length > 0 || Title.length > 0) && (LastSavedTitle != Title || LastSavedBody != Body)) {
// Build data
var DataString = "§ion=" + Section +
"&body=" + Body +
"&title=" + Title;
LastSavedTitle = Title;
LastSavedBody = Body;
// Post it
$.ajax({
type: 'POST',
url: ArticleAJAXURL + "?action=savedraft",
data: DataString,
success: (function (data) {
var SplitD = data.split("|");
$('#DraftNotice').html(SplitD[1]);
$('#DraftNotice').fadeOut('slow', function () {});
})
});
}
}
setInterval(SaveDraft, 2000);
// Formats input into HTML
function FormatInput(Text) {
// Newlines to breaks
Text = Text.replace(new RegExp("\n", "g"), '<br />')
// **bold**
Text = Text.replace(new RegExp("\\*\\*(.+?)\\*\\*", "g"), '<strong>$1</strong>');
// *italic*
Text = Text.replace(new RegExp("\\*(.+?)\\*", "g"), '<em>$1</em>');
// ##header 1##
Text = Text.replace(new RegExp("##(.+?)##", "g"), '<h2>$1</h2>');
// #header 2##
Text = Text.replace(new RegExp("#(.+?)#", "g"), '<h3>$1</h3>');
// Relpace trailing breaks after header close tags
Text = Text.replace(new RegExp("</h2>(\<br \/\>)*", "g"), "</h2><br />");
Text = Text.replace(new RegExp("</h3>(\<br \/\>)*", "g"), "</h2><br />");
// Youtube
Text = Text.replace(new RegExp("\\[tube:(.+?)\\]", "gi"), "<object type=\"application/x-shockwave-flash\" class=\"youtube-embed\" data=\"http://www.youtube.com/v/$1\"><param name=\"movie\" value=\"http://www.youtube.com/v/$1\" /></object>");
Text = Text.replace(new RegExp("</object>(\<br \/\>)*", "g"), "</object><br /><br />");
// Insert title
var Title = $.trim($('#ArticleTitle').val());
if(Title.length > 0)
Text = "<h1>" + Title + "</h1><br />" + Text;
return Text;
}
</script>
</asp:PlaceHolder>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Controls_ArticleSubmitForm : System.Web.UI.UserControl
{
public string[] ForcedTags { get; set; }
public string Section { get; set; }
public LoggedInUser ThisUser { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
// Include the CSS/JS
((ContentPlaceHolder)Page.Master.FindControl("JavascriptIncludes")).Controls.Add(JSIncludes);
((ContentPlaceHolder)Page.Master.FindControl("HeadContent")).Controls.Add(CSSIncludes);
// Initialise the edit area
ArticleInput.Attributes.Add("onkeyup", "UpdatePreview()");
ArticleInput.Attributes.Add("onchange", "UpdatePreview()");
ArticleTitle.Attributes.Add("onkeyup", "UpdatePreview()");
ArticleTitle.Attributes.Add("onchange", "UpdatePreview()");
// Get forced tags
TagInput.DefaultTagList = this.ForcedTags;
// Load draft if one exists
using (DataClassesDataContext db = new DataClassesDataContext())
{
var q = (from d in db.tblDrafts where d.Section == this.Section && d.UserID == ThisUser.UserID select new { d.Title, d.Draft }).SingleOrDefault();
if (q != null)
{
ArticleTitle.Text = q.Title;
ArticleInput.Text = q.Draft;
}
}
}
}
答案 0 :(得分:1)
这让我感到不安,最后修好了。
Controls_ArticleSubmitForm SubmitForm = LoadControl("~/Controls/Article/ArticleSubmitForm.ascx") as Controls_ArticleSubmitForm;
您无法以编程方式执行新的MyControl()。