问题描述
当我执行此代码(在.js内部)时:
When I execute this code (inside .js) :
jQuery.fn.stickyNotes.createNote = function () {
var pos_x = 0;
var pos_y = 0;
var note_id = jQuery.fn.stickyNotes.notes.length + 1;
PageMethods.saveNote(note_id); // remote method
我需要在MSSQL数据库中保存note_id值.
我将其放在default.aspx上(在< body>内部,我不确定我是否正确),以启用AJAX:
I need to save note_id value inside MSSQL database.
I put this on default.aspx (inside <body> I''m not sure if I''m right), to enable AJAX:
<pre lang="xml"><form id="form2" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
</form>
以及default.aspx.cs
中的方法
and for a method in default.aspx.cs
[System.Web.Services.WebMethod]
public static string saveNote(int note_id)
{
if (note_id == null || text.Length == 0)
return String.Empty;
SqlConnection conn = null;
try
{
string connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
conn = new SqlConnection(connection);
string sql = "INSERT INTO .....";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("ID_Note", note_id);
// ...
conn.Open();
string contNm = Convert.ToString(cmd.ExecuteScalar());
return contNm;
我在< head>
上定义了这个< script type ="text/javascript" src ="script/jquery.stickynotes.js"></script>
到目前为止,我有两个重要的问题:
-我仍然没有编译代码,因为我得到了以下错误:runat = server有两种形式(我又一个两个登录名).我怎么解决这个问题?我可以将脚本管理器放在该表单中吗?
-如果我这样做,我的AJAX无效.当我单击添加注释"时,什么也没有发生……因此它可以正常工作(它会创建一个新的注释/元素).如果我对此行发表评论PageMethods.saveNote(note_id);它又可以工作了...看来我的JS停止工作了.你能帮我吗?
预先感谢.
I have this defined on <head>
<script type="text/javascript" src="script/jquery.stickynotes.js"></script>
By now I have two importante questions:
- I still didnt compiled the code because I get the error that I have two forms with runat=server (I another one two login). How can I solve this problem? Can I put the script manager inside that form?
- If I do that my AJAX dont work. When I click add note nothing happens... previosly it works (it creates a new note/element). If I comment this line PageMethods.saveNote(note_id); it works again... It looks like that my JS stops working. Can you help me please ?
Thanks in advance.
推荐答案
<%@ Page Language="C#" %>
<script runat="server">
[System.Web.Services.WebMethod]
public static string GetHello()
{
return "Hello";
}
</script>
<html>
<head runat="server">
<title>Test PageMethods</title>
</head>
<body>
<script type="text/javascript">
function DoIt() {
PageMethods.GetHello(function(result) {
alert(result);
});
}
</script>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" EnablePageMethods="true" />
<input type="button" onclick="DoIt();" value="Do Stuff" />
</form>
<form id="form2"></form>
</body>
</html>
这篇关于使用ASP.NET AJAX中的PageMethods从JS调用ServerSide函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!