从TextBox类派生类

本文关键字:派生 TextBox | 更新日期: 2023-09-27 18:01:31

我正试图从TextBox类派生一个名为NumBox的自定义类,并为该类创建一个setter和getter,但当我运行程序时,我得到了一个运行时异常"用户代码未处理FormatException"。在我看来,这与此有关。文本不是整数,但我的程序的输入是int。错误发生在以下行:return Convert.ToInt16(this.Text);

谢谢你的帮助!

请在下面找到我的代码:default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <glm:numbox ID="NumBox1" runat="server" Style="position: relative" />
        <asp:Button ID="Button_SquareIt" runat="server" Style="position: relative" 
            Text="Square It" onclick="Button_SquareIt_Click" />
    </div>
    </form>
</body>
</html>

webconfig

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
        <pages>
            <controls>
                <add tagPrefix="glm" namespace="GLM"/>
            </controls>
        </pages>
    </system.web>
</configuration>

App_Code/NumberBox.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Class1
/// </summary>
/// 
namespace GLM
{
    public class NumBox : TextBox
    {
        public NumBox()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public int Num
        {
            set
            {
                this.Text = value.ToString();
            }
            get
            {
                return Convert.ToInt16(this.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;
public partial class _Default : System.Web.UI.Page
{
    GLM.NumBox n = new GLM.NumBox();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button_SquareIt_Click(object sender, EventArgs e)
    {
       n.Num = n.Num *  n.Num;
    }
}

从TextBox类派生类

好的,我已经对此进行了更详细的研究,并找到了问题的原因。

您创建一个NumBox的新实例,然后对其执行操作。您没有引用在页面上创建的实例。

如果你没有相关的designer.cs文件,那么这应该可以工作:

public partial class _Default : System.Web.UI.Page
{
    GLM.NumBox NumBox1;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button_SquareIt_Click(object sender, EventArgs e)
    {
       NumBox1.Num = NumBox1.Num *  NumBox1.Num;
    }
}

如果确实有关联的designer.cs文件,则删除NumBox1的声明。

尝试以下操作:

    public int Num {
        set {
            this.Text = value.ToString();
        }
        get {
            return Int16.Parse(this.Text);
        }
    }

我建议使用TryParse方法来确保您只尝试解析一个实际的数字。请注意,如果您的Text属性IsNullOrEmpty、Parse和TryParse将抛出异常。您应该测试一个空值,如果为空,则返回0或使属性返回Nullable<int>

此外,应该解析为要返回的类型。在解析Int16时,您的属性返回int