У меня есть класс с именем Alerts
, который я наследую от BootstrapPage
. Я использую этот класс как MessageBox
для пользователей после выполнения некоторых операций. Вот код
public class BootstrapPage : Page
{
public enum WarningType
{
Success,
Info,
Warning,
Danger
}
public void ShowNotification(string message, WarningType type)
{
var mainContentHolder = Master.FindControl("MainContentPlaceHolder") as ContentPlaceHolder;
Panel notificationPanel = new Panel();
{
LiteralControl closeButton = new LiteralControl();
closeButton.Text = "<a href=\"#\" class=\"close\" data-dismiss=\"alert\" aria-label=\"close\">×</a>";
Label notificationMessage = new Label() { Text = message };
notificationPanel.Controls.Add(closeButton);
notificationPanel.Controls.Add(notificationMessage);
}
notificationPanel.CssClass = string.Format("alert alert-{0} alert-dismissable", type.ToString().ToLower());
notificationPanel.Attributes.Add("role", "alert");
mainContentHolder.Controls.AddAt(0, notificationPanel);
}
}
У меня есть MasterPage
с именем Site.Master
, который здесь (упрощенный код для лучшего понимания)
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="DeceasedSystem.Site" %>
а вот это HTML
<!DOCTYPE html>
<html>
<head runat="server">
<link href="css/bootstrap.min.css" rel="stylesheet" />
<link href="css/Style.css" rel="stylesheet" />
<link href="css/select2.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div class="col-md-9">
<asp:ContentPlaceHolder ID="MainContentPlaceHolder" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
а вот contentPage
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="UserProfile.aspx.cs" Inherits="DeceasedSystem.WebForm1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
</asp:Content>
Теперь, когда я вошел в систему, в этой строке выдается NullReferenceException
var mainContentHolder = Master.FindControl("MainContentPlaceHolder") as ContentPlaceHolder;
Вот картинка.

Я понятия не имею, почему он выдает NullReferenceException
, когда я предоставил MainContentPlacholder
, и он присутствует в моем MasterPage
. Пожалуйста, помогите мне, потому что я не понимаю Exception
, а также по какой причине он выбрасывает Exception
.
Примечание: Я Inheriting
веб-страниц от BootstrapPage
. Это как-то связано с Exception
? Помоги мне, ребята. ПОЖАЛУЙСТА