从javascript调用csharp方法
本文关键字:方法 csharp 调用 javascript | 更新日期: 2023-09-27 18:22:04
我正在使用javascript创建一个验证,以验证页面大小在1到100之间。。如果errorCtr=0,则在codeenhind中调用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#代码在服务器上运行。您应该复制C#方法在客户端javascript代码中的作用。
您可以用javascript调用服务器端函数。但是,如果您试图在该函数中操作soem服务器端控件,则可能无法成功。但是,如果你试图进行一些计算并从函数中获得返回值,那么你肯定可以实现这一点。
示例代码Java脚本
function callServer() {
alert('<%=LoadInvoiceForJob() %>');
}
背后的代码
public string LoadInvoiceForJob()
{
LBTest.Text = "Something";
return "hello";
}
试试这种方法,
WebForm2.aspx页面:-
<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页面:-
使用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";
}
}
您可以使用webMethode&;脚本方法。在下面的代码中,我搜索用户名的可用性,在aspx文件中,我从这种方法中得到结果:
1) 代码隐藏(c#代码):
[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(网页):
<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>
并且您应该使用具有以下结构的脚本管理器,格式为runnat-server-attr:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" EnablePartialRendering="true"></asp:ScriptManager>