如何从javascript调用服务器端函数

how to call server side function from javascript

本文关键字:服务器端 函数 调用 javascript      更新时间:2023-09-26

我希望在ASP中继器上设置排序。查看我的中继器代码:

 <asp:Repeater runat="server" ID="RptClientDetails">
                    <HeaderTemplate>
                        <table id="example" class="dynamicTable table table-striped table-bordered table-primary">
                            <thead>
                                <tr>
                                    <th>
                                 <a href="#" onclick="ClientSort('ClientID')">Client ID</a>

                                    </th>
                                    <th>
                                     <a href="#" onclick="ClientSort('Name')">Name</a>

                                    </th>
                                    <th>
                                     <a href="#" onclick="ClientSort('TotalBalanceDue')">Total Balance Due</a>

                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                    </HeaderTemplate>
                    <ItemTemplate>

在这里,我调用javascript函数。查看我的javascript函数代码:

function ClientSort(SortExpress) {
           <%= Sorting(SortExpress) %>
         }

从这里我想调用我的。net服务器端函数。

public void Sorting(string SortExpression)
{
    string s = SortExpression;
}

那么,你知道我该怎么称呼它吗?或者直接从中继器调用这个服务器端函数。谢谢

您可以直接从中继器控件调用服务器端代码,只需使用Linkbutton而不是href.

<asp:Repeater runat="server" ID="RptClientDetails">
   <HeaderTemplate>
            <table id="example" class="dynamicTable table table-striped table-bordered table-primary">
                <thead>
                    <tr>
                        <th>
                            <%--<a href="#" onclick="ClientSort('ClientID')">Client ID</a>--%>
                            <asp:LinkButton ID="lbtn" Text="Client ID" ForeColor="Blue" OnCommand="lbtnSorting_Click" CommandArgument="ClientID" runat="server"></asp:LinkButton>
                        </th>
                        <th>
                            <asp:LinkButton ID="LinkButton1" Text="Name" ForeColor="Blue" OnCommand="lbtnSorting_Click" CommandArgument="Name" runat="server"></asp:LinkButton>
                            <%--<a href="#" onclick="ClientSort('Name')">Name</a>--%>
                        </th>
                        <th>
                            <asp:LinkButton ID="LinkButton2" Text="Total Balance Due" ForeColor="Blue" OnCommand="lbtnSorting_Click" CommandArgument="TotalBalanceDue" runat="server"></asp:LinkButton>
                            <%--<a href="#" onclick="ClientSort('TotalBalanceDue')">Total Balance Due</a>--%>
                        </th>
                    </tr>
                </thead>
                <tbody>
        </HeaderTemplate>
</asp:Repeater>

和背后的代码:

protected void lbtnSorting_Click(object sender, CommandEventArgs e)
    {
        string s = e.CommandArgument.ToString();
    }

您不能简单地从JavaScript中调用ASP.NET -方法。这是因为ASP。. NET是纯粹的服务器端,JavaScript是客户端。ASP。.NET从。NET代码生成HTML和JavaScript,这只发生在回发或初始加载期间。
ASP。NET渲染一个边,提供它,并从这一刻起,是出了游戏,直到回发。

看到ASP。

调用服务器端代码而不重新加载页面,使用jquery ajax函数:http://api.jquery.com/jQuery.ajax/

我有一个类似的问题得到修复以下代码…希望有人会受益:-我需要调用服务器端函数(按钮点击事件功能)从客户端只是通过文本框的Onblur:

asp:TextBox ID="txtNum" runat="server" CssClass=" TextBox " Width="170px" MaxLength="8" onblur="javascript:return check2();"/> "

 //JAVASCRIPT - onblur calls button click serverside function
 function check2() {
     document.getElementById("<%=btncheck.ClientID%>").click(); 
 }

asp:Button ID="btncheck" runat="server" Text="Verify" OnClick="check1"/>

//CODE BEHIND - On clicking the Button
protected void check1(object sender, EventArgs e)
{
    string str1 = txtBox.Text;
    CheckCCN2(str1);        
}
//Onblur of the Textbox txtNum   
public void CheckCCN2(string strCCNNum)
{
  //all your server side code    
}