Why would a label control, or any other static control that never
changes and doesn't engage with http request data, need viewstate?
First of all viewstate have the Literal, the Label and other controls and use it by default, unless you turn it off for that controls using the EnableViewState="false"
.
Some reason that I can think of using the viewstate is the caching/saving/keeping the same data for the next post back. Eg. This code will keep the text inside the literal after the post back without setting it again.
<asp:Literal runat="server" EnableViewState="true" ID="txtLiterar">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtLiterar.Text = "Time of start editing:" + DateTime.Now.ToString();
}
}
After the post back the control will keep the fist init value.
Why the EnableViewState is on by default, well, you can see the controls as variables that you set your data on code behind and you need to have it again after the postback. Or else why to use controls ? type it directly to the page.
On the other hand, if you understand what you do, and how they work you can turn the viewstate off on most of them - and set them again on the post back.
In this answer I have write some more about the viewstate and where is needed most: How I can deactivate ViewState without Control problems
Now lets go back to the real issue, the security.
If you turn off the validation of the viewstate, now the text that is used for the Literal, is also saved on the viewstate.
Any one can read it and change it (if you do not have secured it) and lets say that change the text
"Time of start editing"
, to "<script>alert("hello there");</script>"
, now on the first post back the Literal will render the script and here is the issue that is warning you about.