问题描述
所以,我有一个看起来像这样的 aspx 页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><title></title>头部><身体><form id="form1" runat="server"><div></表单>
我想知道,如何编写从我的 C# 代码中调用函数的 JavaScript(例如 jQuery)代码.假设这是我的 C# 方法:
protected void XXX(object sender, EventArgs e){Response.Redirect("pagewho?");}
再次感谢阿隆.:)
这是我现在使用的完整代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><title></title>头部><身体><script type="text/javascript">validateStuff = 函数 () {var isValid = true;var txt = document.getElementById("TextBox1");如果(txt){如果(txt.value.toLower()!=詹姆斯"){isValid = false;}}//执行验证并返回真/假返回是有效的;}<form id="form1" runat="server"><div><asp:Button ID="Button1" runat="server" OnClientClick="return validateStuff();"OnClick="Button1_Click"/><br/><br/><asp:TextBox ID="TextBox1" runat="server" Height="25px" Width="135px"></asp:TextBox><br/><br/><br/>
</表单>
但是,无论我在文本框中连接什么,它都会返回 true.有什么帮助吗?
您可以使用脚本中的 __doPostBack
,并在后面的代码中使用 RaisePostBackEvent 方法来执行您的服务器端逻辑.如果您想进行重定向,这可能是最好的方法.
编辑
如果您希望在单击按钮时在 JavaScript 中进行一些验证,请使用 OnClientClick
执行 JavaScript 验证,如果验证成功则返回 true.
<asp:Button ID="Button1" runat="server" OnClientClick="return validateStuff();"OnClick="Button1_Click"/>
您的 JavaScript 验证:
validateStuff = function(){var isValid = true;var txt = document.getElementById("<%=TextBox1.ClientID%>");如果(txt.value.toLower()!=詹姆斯"){isValid = false;}返回是有效的;}
如果 validateStuff
函数返回 true,则会发生回发,您可以在按钮单击事件中处理保存/更新逻辑:
protected void Button1_Click(object sender, EventArgs e){//保存一些东西到数据库字符串 txtValue = TextBox1.Text.Trim();}
<小时>
在 JavaScript 中:
redirectToAnotherPage = function(){__doPostBack("<%=SomeServerControl.ClientID%>", "someArgument");}
在代码隐藏中:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument){//调用RaisePostBack事件base.RaisePostBackEvent(source, eventArgument);Response.Redirect("anotherpage.aspx");}
如果您只想将用户带到另一个页面,您也可以这样做:
redirectToAnotherPage = function(){window.location.href = "somepage.aspx";}
So, i have an aspx page which looks like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
I would like to know, how I can write JavaScript (e.g. jQuery) code that will call a function from my C# Code. Lets say this is my C# method:
protected void XXX(object sender, EventArgs e)
{
Response.Redirect("pagewho?");
}
Thanks again, Alon. :)
EDIT:
This is the full code i am using the moment:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<script type="text/javascript">
validateStuff = function () {
var isValid = true;
var txt = document.getElementById("TextBox1");
if (txt) {
if (txt.value.toLower() != "james") {
isValid = false;
}
}
//perform validation and return true/false
return isValid;
}
</script>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClientClick="return validateStuff();" OnClick="Button1_Click" />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" Height="25px" Width="135px"></asp:TextBox>
<br />
<br />
<br />
</div>
</form>
</body>
</html>
but, no matter what i am wireting in the textbox, its return true. any help?
You can use __doPostBack
from the script, and use the RaisePostBackEvent method in the code behind to perform your server-side logic. If you're looking to do a redirect, this would probably be the best way.
EDIT
If you're looking to do some validation in JavaScript when a button is clicked, use OnClientClick
to perform your JavaScript validation, and return true if the validation succeeds.
<asp:Button ID="Button1" runat="server" OnClientClick="return validateStuff();" OnClick="Button1_Click" />
Your JavaScript validation:
validateStuff = function(){
var isValid = true;
var txt = document.getElementById("<%=TextBox1.ClientID%>");
if (txt.value.toLower() != "james"){
isValid = false;
}
return isValid;
}
If the validateStuff
function returns true, a postback will occur and you can handle your save/update logic in the button click event:
protected void Button1_Click(object sender, EventArgs e)
{
//save some stuff to the database
string txtValue = TextBox1.Text.Trim();
}
In JavaScript:
redirectToAnotherPage = function(){
__doPostBack("<%=SomeServerControl.ClientID%>", "someArgument");
}
In the code-behind:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
//call the RaisePostBack event
base.RaisePostBackEvent(source, eventArgument);
Response.Redirect("anotherpage.aspx");
}
If all you're looking to do is bring the user to another page, you can also do this:
redirectToAnotherPage = function(){
window.location.href = "somepage.aspx";
}
这篇关于在 Asp.net webforms 中从 JavaScript/JQuery 调用 C# 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!