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?
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? 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.
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">
<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:
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.
Related
I am using the OnClick of the asp:Button tag to compute a report which I would like to display as an innerHTML of a div tag. I would like to use the Response.WriteLine method to return this html file. Does ASP.NET webforms provide a way of capturing this response?
Am I forced to use JavaScript instead? I am using C# on the Server side.
The Web Forms way of doing this is to use a control and set its InnerHTML from the code behind. Here's a self contained example:
<%#Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void GenerateReport(object sender, EventArgs e)
{
var reportHTML = "<p>This is a <span style='font-weight: bold;'>report!</span></p>";
ReportDiv.InnerHtml = reportHTML;
}
</script>
<!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>
<title>Test</title>
</head>
<body>
<form runat="server">
<asp:Button runat="server" OnClick="GenerateReport" Text="Generate Report" />
<div runat="server" id="ReportDiv"></div>
</form>
</body>
</html>
I like to to ask that is there a way to load an ascx control from string? To be more precise: I like to make/store a new .ascx in sql then load it to the website.
Yes, you should be able to use the ParseControl function to do what you want:
http://msdn.microsoft.com/en-us/library/kz3ffe28.aspx
Example from MSDN:
<%# Page language="c#" Debug="true" %>
<!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>
<title>ASP.NET Example</title>
<script runat="server">
// System.Web.UI.TemplateControl.ParserControl;
// The following example demonstrates the method 'ParseControl' of class TemplateControl.
// Since TemplateControl is abstract, this sample has been written using 'Page' class which derives from
// 'TemplateControl' class.
// A button object is created by passing a string to contstruct a button using ASP syntax, to the
// 'ParseControl' method. This button is added as one of the child controls of the page and displayed.
void Page_Load(object sender, System.EventArgs e)
{
Control c = ParseControl("<asp:button text='Click here!' runat='server' />");
myPlaceholder.Controls.Add(c);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:placeholder id ="myPlaceholder" runat="server" />
</form>
</body>
</html>
Easy question perhaps.
Okay, I have a post to my page and need to respond with one string.
in php, you could simply do something like this:
<?php
die ("test");
then you can place this page on a webserver and access it like this:
localhost/test.php
so, I need to do exact same thing in c#.
When I try to respond with:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("test");
Response.End();
}
I'm getting: "<html><head><style type="text/css"></style></head><body>test</body></html>"
as a response.
How can I make asp.net to just return exact response, without html?
I know that I probably missing some basic knowledge, but cannot find anything online.
You can clear the previous response buffer and write your new output.
Response.Clear(); // clear response buffer
Response.Write("test"); // write your new text
Response.End(); // end the response so it is sent to the client
Make sure in your *.aspx file, at the top you have AutoEventWireup="true", if it's false (or not there?) your Page_Load event handler will not be called.
Also, make sure you compiled your page.
Another suggestion is to use a Generic Handler (ie *.ashx), these do not use the typical webforms lifecycle and might be better suited to what you're doing.
I think you're looking for :
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/plain";
Response.Write("test");
Response.End();
}
For me it only Generates actual text in response.write(); statement. I am uploading the complete code for clarity.
Visual Studio: 2010
Code Behind:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("I CAN ONLY SEE THIS NO OTHER HTML TAG IS INCLUDED");
Response.End();
}
}
HTML CODE
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
</div>
</form>
</body>
</html>
OUTPUT & HTML Source:
I CAN ONLY SEE THIS NO OTHER HTML TAG IS INCLUDED
I am getting the desired result. I have tried this code with Master-Page also i get the same result.
Please make sure your AutoEventWireup="true" if i turn this false then HTML SOURCE changes to this
<!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><title>
</title></head>
<body>
<form method="post" action="Default2.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZGivF0fgbeE6VebNR51MYSu3yJdsZ9DwEtIPDBVRf4Vy" />
</div>
<div>
</div>
</form>
</body>
</html>
As the previous answer suggests above, you need AutoEventWireup="true" as well as Response.End() in code behind.
How can I display an html textbox with value obtained from code behinh?
As long as your string value is not private, you should be able to do something like this
<input type="text" value="<%= YourStringValue %>" />
You could use an actual <asp:Textbox /> and set it's "text" value directly from the code behind. If you want to directly inject text into a "normal" html textbox (or anywhere else for that matter), you can use <%= SomeValue %>. Yet another way is to include the "runat=server" attribute on standard html elements, allowing you to manipulate them from the codebehind.
Normally I'd just go for the built-in ASP textbox control so I don't have to worry about persisting values/wiring up viewstate/etc. Injecting dynamic content into plain html elements tends to be an edge case requirement...
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//assign text value here
txt1.Value = "hello world!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="txt1" runat="server" type="text" />
</div>
</form>
</body>
</html>
I am just trying to use UrlEncode in a Hyperlink which is in GridView and found it was not working. Then I tried to take the HyperLink as a separate control and tried with that. It is not giving Hyperlink to me, I mean it is not even clickable.
While when I tried with simple Anchor tag, it is working. This is what I am using
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl ='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>' > wc
</asp:HyperLink>
// While following is working
<a title="asxd" href='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>'>wc
</a>
Still looking for the answer
You don't need to use a Hyperlink control or make an anchor tag runat="server" unless you're doing something to it in your code behind.
wc
You are missing the text property of the hyperlink control.
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl ='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>' Text="wc" />
You also can set NavigateUrl property in code-behind file, in Page_Load event handler, for instance. It will work.
In the code-behind class:
protected void Page_Load(object sender, EventArgs e)
{
HyperLink2.NavigateUrl = "~/Default.aspx?customer=&CompanyName=" + Server.UrlEncode("abc#");
}
And in markup:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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:HyperLink ID="HyperLink2" runat="server" Target="_new">wc
</asp:HyperLink>
</div>
</form>
</body>
</html>
<%= ... %>
doesn't work inside ASP.NET controls.
Alternatives:
<%# ... > works in some cases in data-bound controls
code-behind