开发者

Accessing the property inside Dynamic control gives Null reference exception

I have an app with 24 different Forms. They have some security options based on which the 5 different buttons Submit, Approve, 2nd Approve, 3rd Approve, Reject etc get enabled disabled.

Now I designed a MainForm that has all the buttons and the security code for them. I have created eachform as a usercontrol and load them dynamically based on which form the user wants. My loading works perfectly fine. I load the control and Add it to the place holder in the Page load event of the main page.

Now when the user selects the Submit button I want to call the Save method inside the usercontrol of the Form as each form will have a separate Save. So when I try this code snippet I get the Null reference error. Do let me know how to resolve it.

Private UCDynamic As UserControl

Then on Page Load event I use this code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If not Me.IsPostBack
            UCDynamic = LoadControl("Controls/BkCode.ascx")
            Me.PlaceHolderForm.Controls.Add(UCDynamic)
End if

Then on the ButtonClick Event for Submit I do this

 Protected Sub Save_OnClick(ByVal sender As Object, ByVal e As EventArgs)
    Save()
End Sub

Sub Save()
    CType(Me开发者_StackOverflow社区.UCDynamic, controls_BkCode).Save()
End Sub

Thats where I get the cast Null reference error. So is it that after post back the control no longer exist on the page. Any ideas will be greatly appreciated.

Thanks.


Http protocol is stateless, so all server variables are nothing after postback.

Have you tried to find it with FindControl, it should be stored in the Viewstate of your Placeholder?

Directcast(Me.PlaceHolderForm.FindControl("the_id_of_your_usercontrol"), controls_BkCode).Save()


Hi Tim Thanks for your help. But Findcontrol does not work for the placeholder. Though I have the viewstate enabled for the placeholder yet it does not hold the control after post backs.

Finally resolved this issue by moving my LoadControl code out of If not is postback. So I load the control every time and the code inside the control takes care of the logic of post backs and the viewsataes for the control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜