Default.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Greeting Card Maker</title>
</head>
<body>
<form runat="server">
<div>
<!-- Here are the controls -->
Choose a background color: <br />
<asp:DropDownList ID="backColor" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ControlChanged" Width="194px" Height="22px" /> <br /><br />
Choose a font:<br />
<asp:DropDownList ID="fontName" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ControlChanged" Width="194px" Height="22px" /><br /><br />
Specify a numeric font size:<br />
<asp:TextBox ID="txtFontSize" AutoPostBack="true" runat="server" OnTextChanged="ControlChanged"/><br /><br />
Choose a border style:<br />
<asp:RadioButtonList ID="border" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ControlChanged" Width="177px" Height="59px" /><br /><br />
<asp:CheckBox ID="chkPicture" AutoPostBack ="true" runat="server" OnCheckedChanged="ControlChanged" Text="Add the Default Picture"></asp:CheckBox><br /><br />
Enter the greeting text below:<br />
<asp:TextBox ID="txtGreeting" AutoPostBack="true" runat="server" OnTextChanged="ControlChanged" Width="240px" Height="85px" TextMode="MultiLine" /><br /><br />
<asp:Button ID="cmdUpdate" OnClick="cmdUpdate_Click" runat="server" Width="71px" Height="24px" Text="Update" />
</div>
<!-- Here is the card -->
<asp:Panel ID="pnlCard" runat="server" Width="339px" Height="481px" HorizontalAlign="Center" Style="position: absolute;
top: 16px; left:313px;">
<br />
<asp:Label ID="lblGreeting" runat="server" Width="212px" Height="160px" />
<asp:Image ID="imgDefault" runat="server" Width="212px" Height="160px" />
</asp:Panel>
</form>
</body>
</html>
当输入选项发生变化时,页面应该动态变化。当页面首次加载并且我选择背景颜色中的一个选项时,我会在这行代码中得到错误。
//Update the border style using the value from the converter
pnlCard.BorderStyle = (BorderStyle)converter.ConvertFromString(border.SelectedItem.Text);
错误是对象引用未设置为对象的实例。我试过破译其他类似的问题并且没有弄明白。
这是我的Default.aspx.cs代码的其余部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Text;
using System.ComponentModel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
//Set color options
string[] colorArray = Enum.GetNames(typeof(System.Drawing.KnownColor));
backColor.DataSource = colorArray;
backColor.DataBind();
//Set font options
InstalledFontCollection fonts = new InstalledFontCollection();
foreach (System.Drawing.FontFamily family in fonts.Families)
{
fontName.Items.Add(family.Name);
}
//Set border style options
string[] borderStyleArray = Enum.GetNames(typeof(BorderStyle));
border.DataSource = borderStyleArray;
border.DataBind();
imgDefault.ImageUrl = "defaultPic.png";
imgDefault.Visible = false;
}
}
protected void ControlChanged(object Sender, System.EventArgs e)
{
UpdateCard();
}
protected void cmdUpdate_Click(object sender, EventArgs e)
{
UpdateCard();
}
private void UpdateCard()
{
//Find the appropriate TypeConverter for the BorderSyle enumeration
TypeConverter converter = TypeDescriptor.GetConverter(typeof(BorderStyle));
//Update the border style using the value from the converter
pnlCard.BorderStyle = (BorderStyle)converter.ConvertFromString(border.SelectedItem.Text);
//Update the font
lblGreeting.Font.Name = fontName.SelectedItem.Text;
if(Int32.Parse(txtFontSize.Text) > 0)
{
lblGreeting.Font.Size = FontUnit.Point(Int32.Parse(txtFontSize.Text));
}
if(chkPicture.Checked)
{
imgDefault.Visible = true;
}
else
{
imgDefault.Visible = false;
}
lblGreeting.Text = txtGreeting.Text;
}
}
这是堆栈跟踪
[NullReferenceException: Object reference not set to an instance of an object.]
_Default.UpdateCard() in c:\Users\WES\Documents\Semester 6\DistributedAppDevelopment\Module3AssignmentWesMincic\Default.aspx.cs:61
_Default.ControlChanged(Object Sender, EventArgs e) in c:\Users\WES\Documents\Semester 6\DistributedAppDevelopment\Module3AssignmentWesMincic\Default.aspx.cs:47
System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +116
System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent() +138
System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +15
System.Web.UI.Page.RaiseChangedEvents() +132
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1559