[英]Using Jquery and Ajax in ASP.NET
I am using ajax and jquery to load contents into a div. 我正在使用ajax和jquery将内容加载到div中。
My jquery looks like this 我的jQuery看起来像这样
$("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 在GetStuff.aspx中,我想编写一些asp.net html控件,例如
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 我试图像这样将asp:Label放在我的aspx文件中
<%@ 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? 我怎样才能显示asp.net html控件?
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
You are trying to write an ASP.NET Server Control as the output? 您正在尝试将ASP.NET服务器控件编写为输出吗? You're actually overcomplicating things =D 您实际上使事情复杂化= 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. 写入响应流时,无论如何都需要编写有效的HTML /文本。 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. 仅当将ASP.NET标签的呈现函数作为ASP.NET生命周期的一部分调用时,它才会转换为<span>。
Just treat GetStuff.aspx as a regular page. 只需将GetStuff.aspx视为常规页面即可。 Put your HTML in the .aspx, and any business logic in the Page_Load. 将您的HTML放在.aspx中,并将任何业务逻辑放在Page_Load中。 It will then output HTML that your ajax call can use. 然后它将输出您的ajax调用可以使用的HTML。
updated: 更新:
Your GetStuff.aspx page would be something like: 您的GetStuff.aspx页面将类似于:
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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: 然后,您在GetStuff.aspx.cs后面的代码将包含:
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. 当然,您的Page_Load可以执行数据库查询或其他任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.