我試圖調用函數後面的代碼:如何使用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