Dynamically created controls causing Null reference

后端 未结 5 1644
情深已故
情深已故 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 15:57

    As FindControl is not recursive, you have to replace this code :

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

    by this code :

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

    However, according other answers, you should move the declaration (not the instantiation) outside the method (at class level) in order to easily get an entry for your controls.

提交回复
热议问题