C# 如何从javascript调用服务器端函数
我想在ASP中继器上设置排序。请参阅我的中继器代码:C# 如何从javascript调用服务器端函数,c#,javascript,asp.net,C#,Javascript,Asp.net,我想在ASP中继器上设置排序。请参阅我的中继器代码: <asp:Repeater runat="server" ID="RptClientDetails"> <HeaderTemplate> <table id="example" class="dynamicTable table table-striped table-bordered table-primary">
<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>
你知道我怎么称呼它吗?或者直接从repeater我可以调用这个服务器端函数。谢谢要调用服务器端代码而不重新加载页面,请使用jquery ajax函数:您不能简单地从JavaScript调用
ASP.NET
-方法。这是因为ASP.NET纯粹是服务器端,JavaScript是客户端。ASP.NET从.NET代码生成HTML和JavaScript,这仅在回发或初始加载期间发生。
ASP.NET渲染并提供一个面,从这一刻起,它将退出游戏,直到回发
请参阅并发布。您可以直接从中继器控件调用服务器端代码,只需使用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>
我有一个类似的问题通过以下代码得到修复…希望有人能从中受益:- 我需要从客户端调用服务器端函数(按钮单击事件功能),只需打开一个文本框: asp:TextBox ID=“txtNum”runat=“server”CssClass=“TextBox”Width=“170px”MaxLength=“8”onblur=“javascript:return check2();”/>”
我没有关于加载页面的咨询…我只想从javascript调用服务器端函数…这就是您如何实现它的方法如果您在客户端和服务器之间实现ajax通信时遇到困难,您可以问一些具体的问题
public void Sorting(string SortExpression)
{
string s = SortExpression;
}
<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 - onblur calls button click serverside function
function check2() {
document.getElementById("<%=btncheck.ClientID%>").click();
}
//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
}