我试图调用函数后面的代码:如何使用javascript调用代码隐藏功能?
protected void Insert(object sender, EventArgs e)
{
blah blah blah code
this.BindGrid();
}
而在.aspx文件我有我的Java功能,看起来像这样:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:Button ID="Button2" runat="server" Text="Call Button Click" Style="display:none" OnClick="Insert" />
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClientClick="return Validate();" Width="100" />
<script type="text/javascript">
function Validate()
{
var id = document.getElementById("txtStudentID").value;
Number(id);
var fn = document.getElementById("txtFirstName").value;
String(fn);
var ln = document.getElementById("txtLastName").value;
String(ln);
var cls = document.getElementById("txtClass").value;
String(cls);
var rn = document.getElementById("txtRollNumber").value;
Number(rn);
My code blah blah blah
document.getElementById("Button2").click();
PageMethods.Insert(onSuccessMethod, onFailMethod);
};
</script>
我需要插入到当我按下按钮添加(可见的按钮)时工作。 我不确定我的逻辑是否正确,或者有更好的方法。 如果有人知道热门,我会非常感激,我可以从我的javascript调用插入函数。我尝试了谷歌的一些东西,但没有一个似乎工作。 由于事先
检查http://stackoverflow.com/a/4558607/2558060 – Damith
嗯,我想我将不得不通过ajax,因为我无法将我的受保护的方法更改为公共静态。 –
检查以下网址,它将帮助http://www.aspsnippets.com/Articles/Call-Code-Behind-Server-Side-function-from-Client-Side-code-in-ASPNet-using-C-and -VBNet.aspx http://www.codeproject.com/Articles/180355/Calling-a-code-behind-function-from-JavaScript – Ashokreddy