Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改输入会产生错误_C#_Asp.net - Fatal编程技术网

C# 更改输入会产生错误

C# 更改输入会产生错误,c#,asp.net,C#,Asp.net,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

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 />&nbsp;
        <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>
错误是对象引用未设置为对象的实例。我试过破解其他类似的问题,但还没有弄明白

以下是我的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

错误是什么…对象引用未设置为对象的实例您正在尝试访问未实例化的对象。检查堆栈跟踪中的行号,并逐步检查代码以查找问题。很可能-
pnlCard
null
[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