I'm using the following method to render a user control to a string:
Page pageHolder = new Page();
pageHolder.EnableViewState = false;
UserControl viewControl = (UserContro开发者_JS百科l)pageHolder.LoadControl(path);
viewControl.EnableViewState = false;
System.Type viewControlType = viewControl.GetType();
HtmlForm tempForm = new HtmlForm();
tempForm.EnableViewState = false;
tempForm.Controls.Add(viewControl);
pageHolder.Controls.Add(tempForm);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
string content = output.ToString();
However, in the output, there are sometimes hidden fields with __EVENTVALIDTION
and __VIEWSTATE
info as well as JavaScript to enable PostBack.
Is there a way to completely disable all WebForms/ViewState related code from being generated for the control I'm trying to render? Setting EnableViewState
to false and setting the EnabledViewState="false"
in the .ascx
file doesn't seem to work.
Did you try
<pages enableViewState="false" enableEventValidation="false">
...
</pages>
On your web.config?
I am not sure it's a good idea in general, but I assume you know what you are doing.
You can set EnableViewState="false"
in the directive of the user control. As for disabling event validation, I believe you can only do that through the web.config or page directive.
精彩评论