I have a method in Serverside validaNIF and want to call it through javascript (Clientside) to validate an Asp:textbox when it does Leave

Asked

Viewed 57 times

0

I tried to do through the event ontextchange and call the backend function, only as this is to make a record, whenever it ran the event wipes the data from my other textboxs (it seems to refresh the page). Just getting the data from the textbox I’ve been validating(nif).

public void validaNif(object sender, EventArgs e){

    .....

}
  • you need to write an Ajax method and call

  • @Pedrocardoso, what kind of validation and other actions should this method do?

  • 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

  • @Ricardopunctual I’ll see if I can manage with Ajax (little use)

  • @Leandroangelo validates whether the nif is validated through an algorithm already designed by the tax authority. (number positions , what kind of numbers , length etc)

  • @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

Show 1 more comment

1 answer

1


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)

inserir a descrição da imagem aqui

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);
    }
}
  • Leandro but for me to use the Viewstate I don’t have to do Submit ? i did in my Leave Event ("valida nif" which runs ontextchange) Viewstate["name"] = Request.Form["name"]; only that it does the assignment but after leaving the event it does not enter me in the page load, will I have to add to my Leave Event a refresh page , or a Response.redirect to the same page ?

  • For the solution with the ViewState It takes a Postback, but then it would be another question within the same scenario, "How to maintain the state of my controls after postback a component?"

  • Yes, I guess I don’t need to call functions with javascript since what I really want is to save the data of the textboxs after the execution of validationNif

  • So... is that actually my post is the answer to your question... but not the solution to your problem :P

  • Exactly xD Yet I solved my problem with your tip. Viewstate already figured out how to use is already functional , if anyone needs say I put the solution thank you Leandro

  • I leave the link with the solution for those who need it https://asp.net-tutorials.com/state/viewstate/

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.