C#语法[2] [英] C# grammer[2]

查看:78
本文介绍了C#语法[2]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-Default.aspx--

--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">
    <asp:Button ID="Button1" runat="server" Height="30px"

        Text="Button" Width="69px" onclick="Button1_Click"  />
    <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
    <div>

    </div>
    </form>
</body>
</html>



--Default.aspx.cs--



--Default.aspx.cs--

using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public delegate void Y();

public partial class _Default : System.Web.UI.Page 
{ 
    public event Y X;
    protected void Page_Load(object sender, EventArgs e)
    {
        A a = new A();
        X += a.f;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        X();
    }
}

public class A:_Default
{
    public void f()
    {
        TextBox1.Text = "success";
    }
}



1)为什么当运行Default.aspx并单击浏览器上的Button时,TextBox1.Text ="success"将在函数f()中生成NullReferenceException.



1)why TextBox1.Text="success" will generate a NullReferenceException in function f() when run Default.aspx and click Button on browser.

推荐答案

创建一个_Default类的实例,但其中的控件未实例化.这是当您请求页面时ASP.NET引擎的作用.

该页面可能会帮助您: http://www.west-wind.com/weblog/posts/120530. aspx [^ ]
Because you just create an instance of class _Default but the controls in it don''t get instantiated. This is what the ASP.NET engine does when you request a page.

This page may help you: http://www.west-wind.com/weblog/posts/120530.aspx[^]


这篇关于C#语法[2]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆