简体   繁体   English

如何从javascript调用服务器端功能

[英]how to call server side function from javascript

i am looking to set sorting on ASP Repeater. 我想在ASP Repeater上设置排序。 see my repeater code: 看到我的中继器代码:

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

here, i am calling javascript function. 在这里,我正在调用javascript函数。 see my javascript function code: 看到我的JavaScript函数代码:

function ClientSort(SortExpress) {

           <%= Sorting(SortExpress) %>
         }

from here i want to call my .net server side function. 从这里我想调用我的.net服务器端函数。

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

}

so, have you idea how can i call it? 所以,你知道我怎么称呼它吗? or directly from repeater i can call this server side function..Thanks 或直接从转发器中,我可以调用此服务器端功能。

You cannot simply call an ASP.NET -method from your JavaScript. 您不能简单地从JavaScript调用ASP.NET方法。 This is because ASP.NET is purely server-side and JavaScript is client-side. 这是因为ASP.NET纯粹是服务器端,而JavaScript是客户端。
ASP.NET generates HTML and JavaScript from your .NET code and this happens ONLY during postbacks or Initial loads. ASP.NET从您的.NET代码生成HTML和JavaScript,仅在回发或初始加载期间发生。
ASP.NET renders a side, provides it and from this Moment on is out of the game until postback. ASP.NET会提供一面,并提供该面,从此刻起,直到回发之前,该面都将保留。

See ASP.NET-page lifecycle and this post. 请参阅ASP.NET页生命周期文章。

Directly you can call server side code from repeater control, just use Linkbutton instead of href. 您可以直接从中继器控件调用服务器端代码,只需使用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>

And the Code Behind : 和背后的代码:

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

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

I had a similar issue got fixed by following code...hope someone will benefit:- I need to call a server side function (button click event functionality) from client side just by Onblur of a Textbox: 我有一个类似的问题可以通过以下代码得到解决...希望有人会从中受益:-我只需要通过文本框的Onblur从客户端调用服务器端函数(按钮单击事件功能):

asp:TextBox ID="txtNum" runat="server" CssClass="Textbox" Width="170px" MaxLength="8" onblur="javascript:return check2();"/>" 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" /> asp:按钮ID =“ btncheck” runat =“服务器” 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    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM