2011-08-04 10 views
1

如果我在客户端项目添加到选择元素,然后提交表单,我得到以下错误: -如何在服务器端注册客户端添加的数据以进行回发验证?

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

我不想禁用EventValidation。我如何将在客户端添加的数据注册到特定的元素以在服务器端进行回发验证?

我知道RegisterForEventValidation方法在那里,但我从来没有使用它,甚至我不明白从msdn。

如果有人曾经这样做过,请简单介绍一下示例代码吗?

回答

0

在我的情况,我知道所有事先可以被添加到在客户端列表框的值,所以幸运的是,有一个解决方案: -

protected override void Render(HtmlTextWriter writer) 
{ 
Page.ClientScript.RegisterForEventValidation(lstBox.UniqueID, "ListItemValue 
that can be added at client side"); 
base.Render(writer); 
} 
1

说实话,我从来没有过这个好运气,我发现为了避免ASP.NET尝试验证它,在这种特殊情况下子类化必要的控制就容易多了。这比翻页验证更好,因为除了那个控件之外的所有东西都会一如既往地被验证。我假设你在使用服务器端的HtmlSelect(同样可以使用DropDownList)。这里有一个选项:

public class NoEventValdationHtmlSelect : HtmlSelect 
{ 
} 

你必须通过检索的Request.Form [UniqueNameOfControl]自的SelectedIndex /价值/项目将在服务器端空选定的值。

+0

+1为您回复。但是我发现我的问题的答案是它不是完全可能的,但是如果我知道将要在客户端预先添加的值,那么我可以将这些值注册为该控件的有效后数据,使用* * Page.ClientScript.RegisterForEventValidation **。谢谢! – teenup

相关问题