0

I am creating a validation using javascript to validate the pagesize between 1 to 100.. if errorCtr = 0 then call GotoPage() in codebenhind.. Thank you in advance.

<script type="text/javascript" language="javascript">
    function validatePageSize() {
        var pageSize = document.getElementById('MainContent_txtPageSize').value;
        var errorCtr = 0;
        if (pageSize == "") {
            alert('Records per page should be a valid number');
            errorCtr++;
        }
        if (pageSize < 0 || pageSize > 100) {
            alert('Records per page should be between 1 to 100');
            errorCtr++;
        }
        if (errorCtr == 0) {
            //missing code
        }
    }
</script>

codebehind:

void GotoPage()
{
    if (txtPageSize.Text.Trim() != "0" && txtPageSize.Text.Trim().Length > 0)
    {
        GridView1.PageSize = Convert.ToInt16(txtPageSize.Text.Trim());
        GetPOHistoryByParameterOrderByPONumber();
        btnShowAll.Visible = false;
    }
    else
    {
        GridView1.PageSize = 100;
        GetPOHistoryByParameterOrderByPONumber();
        btnShowAll.Visible = false;
        txtPageSize.Text = "100";
    } 
}
0

4 Answers 4

2

You can't do this with just a javascript call, you need to postback if you want run code on the server. Javascript code runs on the client (in other words, in the browser). Your C# code get's run on the server. You should duplicate what the C# method is doing in client side javascript code.

Sign up to request clarification or add additional context in comments.

1 Comment

@devkiat, if Bill's answer helped you, consider to mark it as accepted.
1

You can call a server side function in javascript. But if you are trying to manipulate soem server side control in that function that may not work out. But if you are trying to do some calculation and get the return value from the function then definitely you can achieve that.

Sample code Java script

function callServer() {
alert('<%=LoadInvoiceForJob() %>');
}

Code Behind

public string LoadInvoiceForJob()
{

    LBTest.Text = "Something";
    return "hello";

}

Comments

0

Try this way,

WebForm2.aspx page:-

<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" />
<script type="text/javascript">
    function validatePageSize() {
        var pageSize = document.getElementById('MainContent_txtPageSize').value;
        var errorCtr = 0;
        if (pageSize == "") {
            alert('Records per page should be a valid number');
            errorCtr++;
        }
        if (pageSize < 0 || pageSize > 100) {
            alert('Records per page should be between 1 to 100');
            errorCtr++;
        }
        if (errorCtr == 0) {
            PageMethods.Goto();
        }
    }
</script>
<div>
    <asp:TextBox ID="MainContent_txtPageSize" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="validatePageSize();" />
</div>
</form>

WebForm2.aspx.cs page:-

Add using System.Web.Services;

 [WebMethod]
    public static void Goto()
    {
        if (txtPageSize.Text.Trim() != "0" && txtPageSize.Text.Trim().Length > 0)
        {
            GridView1.PageSize = Convert.ToInt16(txtPageSize.Text.Trim());
            GetPOHistoryByParameterOrderByPONumber();
            btnShowAll.Visible = false;
        }
        else
        {
            GridView1.PageSize = 100;
            GetPOHistoryByParameterOrderByPONumber();
            btnShowAll.Visible = false;
            txtPageSize.Text = "100";
        } 
    }

Comments

0

you can use webMethode && script methode. on the following code in the code behind i search to username availability and on the aspx file i get result from this method :
1)code behind(c# code):

   [System.Web.Services.WebMethod(EnableSession = true)]
    [System.Web.Script.Services.ScriptMethod()]
    public static string CheckUserName(string userName)
    {
        var sql = new SqlHelper();
        string returnValue = string.Empty;
        try
        {
            if (Regex.IsMatch(userName, @"^[A-Za-z0-9._]{1,20}$"))
            {
                returnValue = sql.GetUser(userName) != null & userName != null ? "false" : "true";
            }
            else
            {
                returnValue = "error";
            }
        }
        catch
        {
            returnValue = "false";
        }
        return returnValue;
    }

2)aspx(web page):

 <script type="text/javascript">
    function ShowAvailability() {
        window.PageMethods.CheckUserName(document.getElementById("<%=txtUserName.ClientID%>").value, OnSuccess);
    }
    function OnSuccess(response) {
        var mesg = document.getElementById("<%=lblUsername.ClientID%>");
        mesg.innerHTML = "";
        switch (response) {
            case "true":
                mesg.style.color = "green";
                mesg.innerHTML = "نام کاربری قابل استفاده می باشد";
                break;
            case "false":
                mesg.style.color = "#ec6730";
                mesg.innerHTML = "نام کاربری استفاده شده است ";
                break;
            case "error":
                mesg.style.color = "#ec6730";
                mesg.innerHTML = "نام کاربری صحیح نمی باشد";
                break;
        }
    }
    function sleep(milliSeconds) {
        var startTime = new Date().getTime(); // get the current time
        while (new Date().getTime() < startTime + milliSeconds); // hog cpu

    }
    function OnChange(txt) {
        //sleep(100);
        document.getElementById("mesg").innerHTML = "";
    }
</script>

and you should use script manager with the following structure in the form with runnat server attr :

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" EnablePartialRendering="true"></asp:ScriptManager>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.