Dynamically created controls causing Null reference

后端 未结 5 1629
情深已故
情深已故 2021-01-07 15:26

I am trying to dynamically create controls and give them properties during run time.

I have put my code inside the Page_Init event, when I run my website I can see m

5条回答
  •  温柔的废话
    2021-01-07 16:16

    The FindControl is failing because it can't find the control and causing a null reference.

    Just reference it directly using FeedbackLabel as you already have it in your class. Just move the scope outside of your 'Init' method.

    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 =...;
    }
    

提交回复
热议问题