I have a user control in which I have successfully set Page.EnableEventValidation = false
in the Page_Init
event in the codebehind (in order to render the page markup to a string):
我有一个用户控件,我在代码隐藏的Page_Init事件中成功设置了Page.EnableEventValidation = false(为了将页面标记呈现为字符串):
MyControl.ascx.cs
protected void Page_Init(object sender, EventArgs e)
{
if (Request.Form["__EVENTTARGET"] != null
&& Request.Form["__EVENTTARGET"] == btnPrint.ClientID.Replace("_", "$"))
{
Page.EnableEventValidation = false;
}
}
However, when I attempt to duplicate this functionality on a separate page (this time in runat=server
script tags)...
但是,当我尝试在单独的页面上复制此功能时(这次是在runat = server脚本标签中)...
MyPage.aspx
<script runat="server">
protected void Page_Init(object sender, EventArgs e)
{
if (Request.Form["__EVENTTARGET"] != null
&& Request.Form["__EVENTTARGET"] ==
btnDownloadPDF.ClientID.Replace("_", "$"))
{
Page.EnableEventValidation = false;
}
}
</script>
... I get the following error:
...我收到以下错误:
The 'EnableEventValidation' property can only be set in the page directive or in the configuration section.
'EnableEventValidation'属性只能在页面指令或配置部分中设置。
Now, in my first example, I was receiving this error when originally attempting to do this on Page_Load
; however, it seems that you can disable event validation programmatically as long as it's done during (or before) Page_Init
. Unfortunately the same does not work in my second example.
现在,在我的第一个例子中,我在最初尝试在Page_Load上执行此操作时收到此错误;但是,只要在Page_Init期间(或之前)完成,您似乎可以通过编程方式禁用事件验证。不幸的是,这在我的第二个例子中不起作用。
Why does this work in one scenario and not the other? Is it related to the fact that the code is not in a codebehind?
为什么这在一个场景中起作用而不在另一个场景中?它与代码不在代码隐藏中的事实有关吗?
1 个解决方案
#1
5
Generally using Page.EnableEventValidation = false
is a bad idea, for security reasons.
出于安全原因,通常使用Page.EnableEventValidation = false是个坏主意。
Instead you should look at Page.ClientScript.RegisterForEventValidation
相反,您应该查看Page.ClientScript.RegisterForEventValidation
For example:
protected override void Render(HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation(btnDownloadPDF.UniqueID);
base.Render(writer);
}
This will register the event reference and allow the event to fire. Then you can leave Page.EnableEventValidation = true
.
这将注册事件参考并允许事件触发。然后你可以让Page.EnableEventValidation = true。
Check this example from MSDN.
从MSDN检查此示例。
#1
5
Generally using Page.EnableEventValidation = false
is a bad idea, for security reasons.
出于安全原因,通常使用Page.EnableEventValidation = false是个坏主意。
Instead you should look at Page.ClientScript.RegisterForEventValidation
相反,您应该查看Page.ClientScript.RegisterForEventValidation
For example:
protected override void Render(HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation(btnDownloadPDF.UniqueID);
base.Render(writer);
}
This will register the event reference and allow the event to fire. Then you can leave Page.EnableEventValidation = true
.
这将注册事件参考并允许事件触发。然后你可以让Page.EnableEventValidation = true。
Check this example from MSDN.
从MSDN检查此示例。