Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery AJAX and ASP.NET

I have this little problem... I have this asp.net website. I have a menu, all done with html and css. So when I click on home, the ajax loads the other content into the specified div element. Working 100%.

In the content that was loaded into the div element, I have a button. An ASP.NET button.

When I click on the button, it gives me a "The resource cannot be found." error.

There must be something I am missing. If you dont understand, heres the ajax:

//Load the Home page on click.
$(document).ready(function () {
    $('.home').click(function () {
        $("#content").load("html/home/home.aspx");
    });
});

Now the aspx page that was loaded into the content div, displays a button, btnAdd:

<asp:Panel ID="pnlAddNewBlog" runat="server">
    <asp:TextBox ID="txtAddNewBlog" runat="server" TextMode="MultiLine"></asp:TextBox>
    <br />
    <asp:Button ID="btnAdd" runat="server" Text="Add" />
</asp:Panel>

When I click on that button, the error appears.

What I want to achieve is to: when the user clicks on the button, the text in txtAddNewBlog gets added into a database. Now this I can achieve using C#... but not if this error is in my way. Any ideas?

like image 701
Fred Avatar asked Apr 18 '13 18:04

Fred


People also ask

Can Ajax work with ASP NET?

AJAX is used to create dynamic web pages that do not require page reloading when any part of the whole web page content or the whole web page content is changed. The server data exchange is asynchronous in nature and AJAX in ASP.net uses multiple technologies like XSLT, XHTML, CSS, JavaScript, etc.

Can we use jQuery in ASP NET?

Including jQuery in ASP.NET html, or index. php file is). Microsoft Visual studio 2010 and 2012 include jQuery by default and provide intellisense to use jQuery. Tip: using Google AJAX libraries to add jQuery has several advantages: decreased latency, increased parallelism, and better caching.

Can jQuery and Ajax be used together?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

Is Ajax better than jQuery?

While JQuery is a library for better client-side web page development, AJAX is a technique of doing XMLHttpRequest to the server from the web page and sending/retrieving data used on a web page. AJAX can change data without reloading the web page. In other words, it implements partial server requests.


1 Answers

The problem is going to be the form that gets created. When you make that request, ASP.NET will build a form whose target is home.aspx, not the full path to it. But you're dumping that form onto an HTML page that's at a different level.

When you do a form post, it attempts to post to home.aspx, relative to where the browser thinks it is, which is two levels up, and it doesn't find it.

I don't know if there's a good way to do this - ASP.NET is not meant to handle this kind of thing. You can do an IFRAME. You can wrap the home.aspx content into a user control, and leave the ASP.NET form on the outer page. Or you may be able to manipulate the form's target so you post to the right place. But I don't think any of those would be a lot of fun.

like image 163
Joe Enos Avatar answered Oct 04 '22 21:10

Joe Enos