What's the best way for print html?
I can do this:
container.InnerHtml = "<input type=\"text\" ...runat=..>";
where container is an ref to an <div> HTML
but I have impression that this it's not the correct mode.
can someone point me for right direction?
Thanks in advance.
As Paolo has mentioned, using the <asp:PlaceHolder> is a good way to inject HTML at runtime. Not only does it allow for you to output plain html <div><p>Hello World!</p></div> but it also allows you to dynamically add other asp.net controls.
For your example above you could do
.aspx
<asp:PlaceHolder id="PlaceHolder1" runat="server" />
c#
TextBox textBox1 = new TextBox();
textBox1.Id = "textBox1";
textBox1.TextMode = TextBoxMode.SingleLine;
PlaceHolder1.Controls.Add(textBox1);
This then allows you to access the value which you enter into textBox1 dynamically.
if by "printing HTML" you mean injecting it at runtime in a ASP.NET webform, you may try using a PlaceHolder
Related
I'm not sure if this is even possible,
but I wonder if I can generate a full control that its tags are stored in a text property to HTML page.
let's say that I have an <asp:Image ....... /> or <div><i href=""/></div> as a Text, and it is stored in a property named X in .cs file, and I want this X to be placed in a specific line in the Html page.
is that possible?
in .cs file let's say I have something like this
X = #"<asp:Image ID = ""Image111"" runat = ""server"" ImageUrl = ""img.jpg"" />";
and in the HTML file I want this:
<body>
<form id="form1" runat="server">
.
<!-- X comes here but as a real control -->
.
.
.
</form>
I'm not sure if it is possible but I wish to find a way to do this, any way.
Thanks in advance, best regards.
I would change the way youre thinking about this. Sounds like what you want to do is create a server control then pass some sort of parameter into that control which will then change the output accordingly. If you were using razor you can also have helpers which would handle this sort of thing.
This might be of interest.
Pass data from a ASP.NET page to ASCX user controls loaded dynamically
Hope that helps.
I need some help figuring out how to do something.
I got this gallery (galleriffic) and some images that are store in Flicker.com so I used the flicker api to get the images but still add them manually to test the gallery.
Now I'm looking for a good way to insert the images into the html after I get them with the flicker api.
I found this htmltextwriter and used the function
Response.Write(GetDivElements());
but this is adding the div's on the top of the html and not inside the body tag.
my qustions is:
is HtmlTextWriter writer = new HtmlTextWriter(stringWriter) a good way to build html tags on the server side?
Is there a better way to add elements to the html other then Response.Write(""); ?
Here is what I do when I need to add mark-up.
in my page
<asp:PlaceHolder ID="MyPlaceholder" runat="server"></asp:PlaceHolder>
in my code behind
MyPlaceholder.Controls.Add(new Literal() { Text="<div>some markup</div>"});
I do it this way because:
1) you can put the PlaceHolder where you need it in the structure of your page
2) by adding a Literal at runtime to the Controls collection prevents ViewState getting bloated with it's contents.
If you are using the older style of asp.net, and not asp.net MVC, then you can just create a div with an id and runat="server". Then you can just write directly to the html.
aspx page
<div id = "DivINeedToAddStuffTo" runat="server" />
aspx.cs
DivINeedToAddStuffTo.InnerHtml = GetDivElements();
Also, I do not see anything wrong with using HtmlTextWriter to create your Html markup
You might try looking into Placeholders. That way you can create an instance of an image control and then add it your your placeholder.
Image myImg = new Image();
myImg.ImageUrl = "MyPicture.jpg";
myPlaceholder.Controls.Add(myImg);
You should be able to use the ASP literal control:
foreach (var item in items)
{
Literal literal = new Literal();
literal.text = item.html; //Assuming the item contains the html.
MyPlaceholder.Controls.Add(literal);
}
You could have that code before the page has rendered.
Hope that helps
Paul
EDIT
Sorry, I think I was mistaken, I thought you had the html with the link to the image(s) and not the actual image itself, Justin's answer would suit you if that's the case.
var ctrl = new WebControl(HtmlTextWriterTag.Div) { CssClass = "SomeClass" };
ctrl.Attributes["style"] = "float:left;display:inline-block;margin:3px;";
ctrl.Controls.Add(new Image
{
ImageUrl =
Page.ResolveUrl("image path here")
});
this.Controls.Add(ctrl);
I am trying to retrieve a html document to show in my web page, now the content comes from database as a string. Now I know there is a way to do this in win forms using Browser.DocumentText. But how do I do this in web form?
I tried the setting the innerHTML property for a div inside of the "OnRowCommand", the "OnRowCommand" happens to be inside an update panel. When I move the Div outside the panel, say to just below the body, it renders well.
Well there are many ways to do this, you can use a label, literal Controls.
Or maybe defining a public string within your page then use it directly in your html as:
<%= strSomeString %>
Add a literal control in aspx file and in codebehind set
Literal1.Text=data_from_DB;
Try this one:
html code:
<div id="webcontent" runat="server">
</div>
Code behind:
webcontent.InnerHtml = FromDBstring;
Write up for Mvc app Html.Raw('html tages') or MvcHtmlString.Create('html tages') and for web form HttpUtility.HtmlEncode('html tages')
I have made an application in javascript using HTML fields in asp.net, as asp text boxes were disturbing the ids, so i used html fields, which are working fine with javascript, now i want to fetch database table columns on page load, and want to assign to html fields, what is the best way to do so? Help me!!!!
You could go back to using the ASP TextBoxes and access the ids in JavaScript as follows:
<%= IDofTextBox.ClientID %>
It's probably the easiest as naturally they can then be accessed in the code behind very easily.
you can use asp text boxes fine if you grab a reference in your javascript to their asp.net generated ID via <%= textboxname.ClientId %>
This is not the right way to do it (I wouldn't recommending it), but if its what you need, then it will work.
Add method="post" action="<your path here>" to your form element and when the submit button posts, you will be able to access all the form variables like so:
string txtName = Request["TextBox1"] ?? string.Empty; //replace textbox 1 with text box name
Just be sure to replace the action in form to your page etc..
But really, going back to <asp:TextBox... will save you a lot more time and as Ian suggested, you can access them with javascript by the server tags <%= TextBox1.ClientId %>
ps: also, the ?? is a null coalesce character. its a short form of saying
if(Request["textbox1"] != null)
txtName = Request["textbox1"];
else
txtName = "";
If I understand you correctly. You just need to add runat="server" and id="someName" to the html fields and access them in the code behind by its given id.
I'm very new to ASP.net. I have a c# content page, in which I want to inset this code half way down within the HTML:
<%
HttpResponse r = Response;
r.Write(HttpContext.Current.Request.ServerVariables["SERVER_NAME"]);
%>
But when I view the page, this content comes out first, before even the tag.
Any ideas on how to get this code inline instead?
Thanks!
EDIT
I'd just like to add a note to all who answered this question to explain what you've done.
You spared your valuable time to help me, a stranger to you, solve a difficult problem at work, which allowed me to get out of the office on Friday night, just in time to catch the last bus to my home 50 miles away, and see my wife who was sick in bed. You didn't just answer my question, you made my day SO much better. THANK YOU so much!
Steven
Because you are doing a Response Write, that will push out before everything else. If you want to just imbed something at a specific point you can do:
<%= HttpContext.Current.Request.ServerVariables["SERVER_NAME"]) %>
This <%= %> will write any string to that exact location in the HTML.
You could also use a Literal control and assign its Text property in your codebehind or use a Label if you require formatting.
You can put a label on the page where you want the text to appear and then set the label's text instead of doing like this. Simply put an asp label on the page and frmo your code behind do
myLabel.Text = HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString();
where myLabel is the ID of your label in your HTML Markup.
Define a DIV on the page like this, where you want the outputted string to be displayed:
<div id="myDIV" runat="server" />
Then, instead of r.Write() you can simply set the inner text of the DIV to be what you want:
myDIV.innerText = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
when you use Response.Write("..."); its shows up before page header , instead of using Response.Write you can put a label on the form wherever you want to see the message and set label's Text property.
Or if you just want the text, with no html markup.
Use a literal.
In aspx file
<asp:Literal ID="MyLiteral" runat="server" />
in code-behind
MyLiteral.Text = HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString();