Frage

I'm using

__doPostBack(clientIdOfSomeButton, someData);

to create a PostBack in JavaScript. The problem is that someData is not known in advance, and event validation kicks in because I cannot ClientScript.RegisterForEventValidation every possible value of someData.

So far, I can see only two possibilities to solve this problem:

  1. Turn off event validation for the page, which is not recommended for security reasons.
  2. Instead of passing the data via event validation, put the data in some hidden text field via JavaScript and then call __doPostBack. That's ugly.

Is there some third option that I've missed? Ideally, I'd like something like ClientScript.RegisterForEventValidationIgnoreArguments(myButton), but something like this does not exist...

War es hilfreich?

Lösung

If someData is not known in advance then how does the server know the meaning of the postback? The second argument is to indicate the type of event or specific information concerning the event, not a user inputted value.

I would register for a custom argument and pass the someData another way.

ClientScript.RegisterForEventValidation(clientIdOfSomeButton, "CustomEvent");

And on the client

Html

<input name="customArgument" type="hidden" value="" />

Javascript

document.forms[0].customArgument = someData;
__doPostBack(clientIdOfSomeButton, '');

then retrieve your value

if(Request["__EVENTARGUMENT"] == "customArgument")
{
   var customArgument = Request["customArgument"]
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top