从javascript调用csharp方法 - c#

            
        

我正在使用JavaScript创建验证,以验证1到100之间的页面大小。
如果errorCtr = 0,则在代码隐藏中调用GotoPage()。

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

代码背后:

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";
    } 
}

参考方案

您不能仅使用javascript调用来执行此操作,如果要在服务器上运行代码,则需要回发。 Javascript代码在客户端上运行(换句话说,在浏览器中)。您的C#代码在服务器上运行。您应该在客户端javascript代码中复制C#方法的作用。

ddl在服务器中未更新-asp.net - javascript

我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……

ASP.NET-如何更改JSON序列化的方式? - javascript

我正在使用ASP.NET通过以下查询返回Json文件:public ActionResult getTransactionTotals(int itemID) { DBEntities db = new DBEntities(); var query = from trans in db.Transactions // Linq query removed …

ASP.NET MVC中应为DataTable返回哪种数据? - javascript

我想为DataTable中的每个页面创建动态加载信息。我正在尝试遵循以下示例:https://www.datatables.net/manual/server-sidehttps://www.datatables.net/manual/data来自示例的代码:$('#example').DataTable( { serverSide: t…

javascript popupwindow之后的行如何工作? - javascript

好的,我有一个来自后面代码的方法,可以创建一个popupwindow。然后有一行代码要在那之后执行,我想知道那行代码何时执行,是在使用popupwindow之后执行还是在创建popupwindow之后执行?例如:void exPopupWindowMethod() { string scr = "window.open('exampleP…

asp.net mvc中的对象数组数据始终为null - javascript

我需要通过json将对象数组发送到asp.net mvc 2,但是我在mvc控制器中没有得到null对象是这样的entries[1].date = "12/22/2014" entries[1].Ref = "0002" entries[1].Credit = "100" entries[2].da…