Facing problem regarding JQuery and ASP.Net 4.0
I have created a small asp.net 4.0 webforms project. I have one button and a <div>
in my default.aspx page.
I just attach a event with my button through OnClientClick
here is my code
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="MyWebApps._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script language="javascript" type="text/javascript">
function showMessage() {
alert("pp");
}
</script>
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<asp:Button ID="btn" runat="server" Text="Hello" OnClientClick="showMessage();return false;" /><div id="message"></div>
the above code works fine. when I click on button then a alert message come and no postback happen.
the problem start when I include jquery js file in page like
<script language="javascript" type="text/javascript" src="Scripts/jquery-1.4开发者_开发知识库.1.min.js"/>
when I include the above file then problem start. if I click on my button after adding jquery js file then nothing happen and postback happen. I am very much confused why the problem is occuring when I include jquery js file in my page.
please help me how solve this situation.
Try changing the script tag so that it's not self closing (i.e. change .../>
to ...>< /script>
).
This worked for me using IE8/XP3/VS2010/.NET4
Where in your page are you placing the jquery script reference? The problem is that the javascript is throwing an error which is why the showMessage() javascript function is not working.
As a result of this, the return false is not called and thus clicking on the button leads to the page being posted.
A good way to debug this would be to actually use a hyperlink which does not have a NavigateUrl instead of a button and then associate the client side onclick of the hyperlink with showMessage() and then try and use it in firebug and see what javascript error is coming up when adding the jquery reference.
try this one
<script language="javascript" type="text/javascript" src="http://wwww.domainname.com/Scripts/jquery-1.4.1.min.js"/>
Try changing the script tag so that it's not self closing (I.e. Change .../> to ...>< /script>)
精彩评论