views:

102

answers:

3

I am using ajax and jquery to load contents into a div.

My jquery looks like this

  $("a.trigger").click(function() {

   $.ajax({

    type: "POST",

    url: "GetStuff.aspx",

    data: "id=0",

    success: function(response){

     $("#contentDiv").html(response);

    }

   });

     });

In GetStuff.aspx I would like to write some asp.net html controls like

  private void Page_Load(object sender, System.EventArgs e)

  {

      Response.Expires = -1;

      Response.ContentType = "text/plain";

      Response.Write("<asp:Label id=\"label1\" runat=\"server\" text=\"helloworld\"/>");

      Response.End();

  }

However the label does not appear on the page.

I tried to put the asp:Label in my aspx file like this

<%@ Page Language="C#" Inherits="Untitled.GetStuff" %>

<asp:Label id="label12" runat="server" text="helloworld2"/>

It also does not work. How can I get asp.net html controls to show up?

+4  A: 

You can't. You're trying to add a server side control to a client side page. Try returning this instead:

Response.Write("<span id=\"label1\">helloworld</span>);

However, when you postback the page you won't have the luxury of being able to say

string text = label1.Text; //DOES NOT WORK
Corey Sunwold
If I wanted the luxury of doing string text = label1.Text; //DOES NOT WORKWhere would I put my asp:label control? In the aspx file? But after the ajax call it will not appear in my div.
xkcd
The way you are trying to load this html through ajax you are not going to get it to work. When the page gets postedback that server side control will have never been registered with the page. This typically happens at compile time when you build an ASPX page the controls on it are properties of the Page. This article might be worth reading for you: http://www.4guysfromrolla.com/articles/092904-1.aspx
Corey Sunwold
+1  A: 

You are trying to write an ASP.NET Server Control as the output? You're actually overcomplicating things =D

If you wrote out

<span>HelloWorld</span>

Instead of the

<asp:Label Id="label1" runat="server" text="HelloWorld" />

You would get what you want. When you write to the response stream, you need to write valid HTML / Text, whatever. An ASP.NET Label is only transformed into a <span> when it's render function is called as part of the ASP.NET Life Cycle.

Tejs
I used asp:Label control as an example. How would I use asp.net controls as opposed to html?
xkcd
You would need to follow a path similar to what @Matt Sherman wrote, where you return the response of the web call as the HTML and avoid calling Response.Write / Response.End yourself. This poses different problems though, such as how to get rid of the additional HTML form and associated viewstate cruft that will be returned with the response.
Tejs
A: 

Just treat GetStuff.aspx as a regular page. Put your HTML in the .aspx, and any business logic in the Page_Load. It will then output HTML that your ajax call can use.

updated:

Your GetStuff.aspx page would be something like:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetStuff.aspx.cs" Inherits="GetStuff" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label Id="label1" runat="server" text="Hello World" />
    <asp:Label Id="idToDisplay" runat="server" /> 
    </div>
    </form>
</body>
</html>

Then your codebehind GetStuff.aspx.cs would contain:

protected void Page_Load(object sender, EventArgs e)
{
    var id = Request["id"].ToString();
    this.idToDisplay.Text = id;
}

Of course your Page_Load can do a database query or whatever.

Matt Sherman
I have tried this. Can you show me an example?
xkcd
updated above...
Matt Sherman