Here you have two points with different solutions, if your problem is the imperviousness of the data after the postback of a control, this can be solved with the ViewState
.
Now, objectively on the question of how to execute a code-Behind method via Javascript, there is a cake recipe for this, but with a more restricted application, it would be necessary to create a static method defined as [WebMethod]
, that besides having a peculiar configuration for its operation, you will not be able to handle other ASP components of your page on the server side.
First, check that on your.Master Site, there is already a component <asp:ScriptManager>
declared, if the answer is yes, just add the attribute EnablePageMethods="true"
<form runat="server">
<asp:ScriptManager runat="server" EnablePageMethods="true">
<Scripts>
//...
</Scripts>
</asp:ScriptManager>
<!-- resto do conteúdo -->
</form>
Done that, you can add the [WebMethod]
static for validation on your aspx. Since this method does not have a connection with the rendered components on the screen, instead of a void
ValidarNif()
will get a return bool
and will receive an input parameter of type string
.
[System.Web.Services.WebMethod]
public static bool ValidaNif(string nif)
{
//no lugar dessa linha você aplicará as suas regras
return string.IsNullOrWhiteSpace(nif);
}
If everything worked so far, when rendering the page and accessing the browser console, you will notice that there is an object PegeMethods
initialized, as well as a representation of your method with the following signature ValidaNif(nif, onSuccess, onFailure, userContext)
You can invoke this method through the `Pagemethods', remembering to indicate the base path.
<script type="text/javascript">
PageMethods.set_path('/SuaPagina.aspx'); //Precisa ser definido apenas uma vez
PageMethods.ValidaNif('teste', //Valor que você vai capturar do TextBox
onSuccess = function(res){alert(res);},
onFailure = function(res){alert('error:' + res);}
);
</script>
If you receive an error message with status 401 (Unauthorized)
, in your project browse App_start and edit the file RouteConfig.cs
altering the AutoRedirectMode
for Off
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
//settings.AutoRedirectMode = RedirectMode.Permanent;
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
}
}
you need to write an Ajax method and call
– Ricardo Pontual
@Pedrocardoso, what kind of validation and other actions should this method do?
– Leandro Angelo
Have you thought about doing it with Ajax? If you are using Submit with Post it will refresh the page even, The best way to do this would be using Ajax I believe
– Edenilson Bila
@Ricardopunctual I’ll see if I can manage with Ajax (little use)
– Pedro Cardoso
@Leandroangelo validates whether the nif is validated through an algorithm already designed by the tax authority. (number positions , what kind of numbers , length etc)
– Pedro Cardoso
@Edenilsonbila The idea is not to use Submit itself, but to use the text box (lostfocus) to execute my function to validate the textbox. I’ll try with ajax
– Pedro Cardoso