Register ViewState for “visible=false” UserControl

2019-09-04 06:17发布

We have a web app(-ish) webform, and we use many custom UserControls. We load a bunch of them in each view, but we don't want them to render, or even pre-render, until we don't have to use them. Then we set Visible = False in each control and then we use Update Panels to set it to True whenever the control is needed. The control then shows up correctly.

But in the next postback we have a very nasty MAC validation error, and we think it is related to the ASP not updating the viewstate (but I don't know if that's entirely true yet).

The question: Is it possible to know in exactly wich argument the MAC validation is throwing an error? If we're right, and the ViewState for the user controls is not being updated in the already sent page, what would be the correct way to do it / workaround for it?

Many thanks

1条回答
仙女界的扛把子
2楼-- · 2019-09-04 07:04

I've since learnt a lot of ASP, so I will clarify my failure points:

and we think it is related to the ASP not updating the viewstate (but I don't know if that's entirely true yet)

Of course, it's absolutelly false. ASP updates the entire ViewState on every post back, so there's no way to not update it.

The MAC validation issue occured when we turned on the UserControl (Visible=true) because we didn't specify the validation of the events that control performed (performing custom __doPostBack()s)

So, the assumption is entirelly incorrect, and the error goes away by simply registering with RegisterForEventValidation() (see here)

查看更多
登录 后发表回答