How to correct run WCF services from javascript?
This is my page code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProductType.aspx.cs" Inherits="Application.ProductType" MasterPageFile="~/Main.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<script type="text/javascript">
function getMainTable() { return document.getElementById("<%= ProductGridMain.ClientID %>"); }
</script>
<script type="text/javascript" src="JavaScripts/ProductType.js"></script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Services/ProductTypeService.svc" />
</Services>
</asp:ScriptManager>
<asp:Label ID="ProductGridMain" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="AddNewItemBtn" CssClass="Btn" runat="server" Text="New" OnClientClick="NewAddBtn(); return false;" />
</asp:Content>
This is the ProductType.js file:
$(document).ready(function() {
var counter = "fast";
ProductTypeService.GetMainGridProductTypeHtml(counter, ResultLoadTableMainGridHtml, ErrorLoadTableMainGridHtml);
});
function ResultLoadTableMainGridHtml(html) {
//debugger;
var tblMainGrid = getMa开发者_高级运维inTable();
if (tblMainGrid != null && html != null && html != "null" && html != "" && html != " ") {
tblMainGrid.innerHTML = html;
}
}
function ErrorLoadTableMainGridHtml(html) {
alert("Error");
}
function NewAddBtn() {
//debugger;
var counter = "test";
ProductTypeService.GetMainGridProductTypeHtml(counter, ResultLoadTableMainGridHtml, ErrorLoadTableMainGridHtml);
}
The service method returns html code of main grid, then in javascript I insert it into label. When I click on button everything works correctly like I suppose, but page is reloading. How do I call the service method on button click without page reloading?
Why not use HTML button instead of ASP.NET button - that way you can indicate that you don't need submit behavior. For example,
<input type="button" id="AddNewItemBtn" class="Btn" runat="server" value="New" onclick="NewAddBtn()" />
You may eliminate runat="server"
if you don't need to refer to the button in the server code.
精彩评论