【问题标题】:Dynamically created controls causing Null reference动态创建的控件导致空引用
【发布时间】:2011-09-05 19:45:58
【问题描述】:

我正在尝试动态创建控件并在运行时为其赋予属性。

我已将代码放入 Page_Init 事件中,当我运行我的网站时,我可以看到我的控件,但是当我单击提交按钮时,出现错误,提示“对象引用未设置为对象的实例”。

这是我使用的代码:

    //Creates instances of the Control    
    Label FeedbackLabel = new Label();
    TextBox InputTextBox = new TextBox();
    Button SubmitButton = new Button();
    // Assign the control properties

    FeedbackLabel.ID = "FeedbackLabel";
    FeedbackLabel.Text = "Please type your name: ";
    SubmitButton.ID = "SubmitButton";
    SubmitButton.Text = "Submit";
    InputTextBox.ID = "InputTextBox";
    // Create event handlers
    SubmitButton.Click += new System.EventHandler(SubmitButton_Click);

    // Add the controls to a Panel
    Panel1.Controls.Add(FeedbackLabel);
    Panel1.Controls.Add(InputTextBox);
    Panel1.Controls.Add(SubmitButton);
}

protected void SubmitButton_Click(object sender, EventArgs e)
{
    // Create an instance of Button for the existing control
    Button SubmitButton = (Button)sender;
    // Update the text on the Button
    SubmitButton.Text = "Submit again!";

    // Create the Label and TextBox controls
    Label FeedbackLabel = (Label)FindControl("FeedbackLabel");
    TextBox InputTextBox = (TextBox)FindControl("InputTextBox");
    // Update the controls
    FeedbackLabel.Text = string.Format("Hi, {0}", InputTextBox.Text);

我该如何解决这个错误?

这是堆栈跟踪

[NullReferenceException:对象引用未设置为对象的实例。] _Default.Page_PreInit(Object sender, EventArgs e) 在 c:\Users\bilalq\Documents\Visual Studio 2010\WebSites\WebSite3\Default.aspx.cs:31 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,对象 o,对象 t,EventArgs e)+14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者,EventArgs e)+35 System.Web.UI.Page.OnPreInit(EventArgs e) +8876158 System.Web.UI.Page.PerformPreInit() +31 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +328

【问题讨论】:

  • 错误出现在哪一行?
  • 您在哪一行得到异常?
  • @Neil,他说点击提交时出现错误。我认为它找不到控件(因为它们被添加到面板,而不是直接添加到页面)。
  • 今天的反对票似乎有点苛刻
  • @qablan89,你能给我们一些来自堆栈跟踪的信息吗?异常是从哪一行抛出的?

标签: c# asp.net dynamic-controls


【解决方案1】:

由于 FindControl 不是递归的,你必须替换这段代码:

Label FeedbackLabel = (Label)FindControl("FeedbackLabel");
TextBox InputTextBox = (TextBox)FindControl("InputTextBox");

通过此代码:

Label FeedbackLabel = (Label)Panel1.FindControl("FeedbackLabel");
TextBox InputTextBox = (TextBox)Panel1.FindControl("InputTextBox");

但是,根据其他答案,您应该将声明(而不是实例化)移到方法之外(在类级别),以便轻松获取控件的条目。

【讨论】:

  • To DoctJones 我试图替换它们,但它不起作用 这里发生的错误 Panel1.Controls.Add(FeedbackLabel);这是堆栈跟踪
  • 此时是 Panel1 != null 吗?
【解决方案2】:

尝试将您的代码放在 Page_Load 而不是 Page_Init 中,并且在使用 FindControl 返回的对象之前检查 null。

我怀疑对象 InputTextBox 为 null,当您尝试打印其 Text 时它会崩溃。

作为一般规则,在将 FindControl 的结果转换为其他内容时,只需检查 null 和类型。

【讨论】:

    【解决方案3】:

    FindControl 失败,因为它找不到控件并导致空引用。

    只需使用FeedbackLabel 直接引用它,因为您已经在课堂上使用了它。只需将范围移到“Init”方法之外。

    private Label feedbackLabel = new Label();
    private TextBox inputTextBox = new TextBox();
    private Button submitButton = new Button();
    
    public void Page_Init(EventArgs e)
    {
        feedbackLabel.ID = "FeedbackLabel";
    }
    
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        feedbackLabel.Text =...;
    }
    

    【讨论】:

      【解决方案4】:

      我建议您在 page_int 之外声明您的控件并在 init 中进行初始化,然后使用它们的名称而不是查找控件。

      【讨论】:

        【解决方案5】:
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                //-- Create your controls here
            }
        

        【讨论】:

        • 谢谢,当我评论这条语句时,一切正常 FeedbackLabel.Text = string.Format("Hi, {0}", InputTextBox.Text);
        猜你喜欢
        • 2012-07-12
        • 1970-01-01
        • 2021-12-15
        • 1970-01-01
        • 2014-02-24
        • 2011-06-10
        • 2011-06-27
        • 1970-01-01
        • 2017-07-22
        相关资源
        最近更新 更多