Skip to main content
 首页 » 编程设计

c#之在 ASP.NET 中使用 Jquery 和 Ajax

2024年02月03日29duanxz

我正在使用 ajax 和 jquery 将内容加载到一个 div 中。

我的 jquery 看起来像这样

  $("a.trigger").click(function() { 
 
   $.ajax({ 
 
    type: "POST", 
 
    url: "GetStuff.aspx", 
 
    data: "id=0", 
 
    success: function(response){ 
 
     $("#contentDiv").html(response); 
 
    } 
 
   }); 
 
     }); 

我想在 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(); 
 
  } 

但是标签没有出现在页面上。

我试图像这样将 asp:Label 放入我的 aspx 文件中

<%@ Page Language="C#" Inherits="Untitled.GetStuff" %> 
 
<asp:Label id="label12" runat="server" text="helloworld2"/> 

它也不起作用。 如何让 asp.net html 控件显示出来?

请您参考如下方法:

你不能。您正在尝试将服务器端控件添加到客户端页面。尝试返回这个:

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

但是,当您回发页面时,您将无法说出

string text = label1.Text; //DOES NOT WORK