How to create aspx page on runtime and place literal control? - c#

I have an requirement where i have to display some content on iframe dynamically. Let me explain my scenario. I have a form where i have to display some html text. Earlier what i did is , i placed a literal on the form and fill it with HTML text. All worked fine but , the text alignments of the displayed html gets overriden by the page css.
So what i want now is that, instead of literal, i place an iframe. Now at page load event , i will generate a aspx dynamically and place a literal control and fill that literal control with the html text. And then will display that aspx page in the iframe.
So i would like to know the following things:
Is this a good idea to hold heavy
text , more than 20K of chars in
memory, dynamically generate aspx
page at runtime.
If any workaround to not to inherit
the page css inside the literal
displayed.
Any other approach better than my approach.
Way to implement my approach(iframe and dynamic aspx).
--- I am using ASP.NET / C# 4.0
Loads of questions...
Thanks in advance

AmRan, if this actually is a css problem, I would first try to create separate css styles for that content and use with the first solution you did. For example, wrap your dynamic content widh
<div id="dynamicContent">
tag and place
#dynamic {}
style settings inside your css file.
I don't think the Literal control has a CssClass property so you would have to use Label instead, or some other technique.
I would try to stay away from IFrame solutions if possible.
Hope this helps.

Related

C# simple html editor - how to search and replace content

Today i'm working on simple html editor in visual c#.
My goal is to open pure html file from local drive (Opendialog load into string or load into webbrowser completed) and allow to edit key fragments.
application should find specific divs and return the full content of that div to textbox or better to combobox (compare to combobox item and show it).
if i change the textbox (or pick up another item from combobox) application should present changes on webbrowser control.
Then i need to print this html as is seen on webbrowser control.
Last thing is to save modified hmtl overwrting original and adding comment with changes at bottom of the html file.
I want to know how to perform search&replace in this project? How to "adress" content of a div?
Better is string searching, indexof, string.replace etc. Or drown into DOM-thing (i don't know both at the moment).
How to present changes on html preview on webcontrol component? And finally overwrite a file?
Code's examples appreciated :)
Thanks in advance
You should use HtmlAgilityPack for that, it makes working with DOM a lot more easier, than parsing it yourself.
http://htmlagilitypack.codeplex.com/
For example, here is how to get all divs
var elements = hdoc.DocumentNode.Descendants("div") /.Where(.your conditions..))/;

Display web form in tooltip

I have used calendar control of ASP.NET. When I move on any date than it will be display tooltip , in which I can show my another aspx page. And in that aspx page there is gridview.
How can I display tooltip which contains aspx page.
Try this
Track one
Use a div to position your element as a tooltip.
In that div use an iframe in which you can load your aspx page.
Track two
you can use div without iframe and use jquery to pull data from server against hovered date and render that data into that div at runtime using $("#divID").html() function.
Personally I would recommend track two because here page life cycle is eliminated and data would load much faster which is required for a tooltip,
only demerit with track two is that its a little complicated because you will have to create your design at runtime using document.createElement() and other functions like that.
You could get some help from Here maybe.

what problems can occur styling if div tags, are replaced by asp.panels (in c# code)

what problems can occur styling if div tags, are replaced by asp.panels (in c# code)
i have a sample code which uses pure html,
the css styles are mapped using the ids of the divs.
i would like to replace these divs by panels using c#, yes i will keep the ids same as that in css file.
question
should i expect any problems in styling because of hidden problems, or hidden styles, or default style of asp panels?
If you are developing on .NET framework 4.0 you can set ClientIdMode property to static, so asp.net don't change your Ids.
Otherwise div ids will be affected by the container's id.
Answer
There won't be any problems if you set ClientIdMode to static.
An asp.panel is rendered as a div. Once you convert it, what's rendered should be exactly the raw HTML you have now.
So if you keep the ids the same you shouldn't have any problems.

Dynamically Add iFrame to a Page?

I need to generate a composite page made up of other pages in our system.
Is it possible for me to dynamically add iFrames to a page, each with its own src pointing to different URLs which are determined on the fly? If so, is there a preferred method to this?
Otherwise I need to refactor the other pages into user controls so I can add them as needed.
Although I'd seriously consider revising your pages to usercontrols if you'd like the "pages" to interact with each other. But for the question itself: you could add LiteralControls to your page (containing the iFrames) or to a placeholder.
PlaceHolder1.Controls.Add(new LiteralControl("<iframe src='mypage.aspx'></iframe>"));
You can do this the same way you dynamically add any .net control to a page, but you need to add an HtmlGenericControl
You could use the Document Object Model (DOM) / Javascript [1,2] to add iframes to your page. If you'd better like c# you'll have to postback before modifying the document I think (I'm not an expert in c# / asp).
[1] http://www.howtocreate.co.uk/tutorials/javascript/dombasics
[2] http://www.w3schools.com/jsref/default.asp

How do I dynamically create new Hyperlinks in ASP.NET?

I'll explain what I'm trying to achieve:
I want to have a situation where I can create as many controls as I want by creating them in a loop in the code behind. I can do this while using PHP, by mixing the PHP code and the HTML code. This allows me to generate actual HTML tags dynamically.
In ASP.NET, I haven't found a way to replicate this functionality.
I've thought about using a loop in the code behind on something like the Init() function to create an array of new() objects, setting their attributes and hoping it is passed into the aspx file, but it didn't work.
How do I do this?
If you want to creat Dynamically ASP.Net Hyperlink control
You can simply do this:
HyperLink hyp = new HyperLink();
hyp.ID = "hypABD";
hyp.NavigateUrl = "";
Page.Controls.Add(hyp);
Well, keep in mind there's a difference between Hyperlinks and LinkButtons in ASP.Net
If you just want Hyperlinks, you can create as many as you want by creating a HyperLink object, creating new objects and then adding them to some control's Controls collection like panel or span or something.
LinkButtons are a little different. You have to use a Repeater control if you want to create a link that posts back to the server. (Well, you don't have to but the alternative is a hack job.)
Hyperlinks can be created by using a Repeater control as well and, if possible is the method I would recommend.
There are lots of ways to do this in asp.net:
If you really wanted to, you could put a loop using <% %> tags directly into the aspx markup, just like with php or classic asp. This is not recommended.
You can put a placeholder or panel control on your form, create as many hyperlink controls or anchor tags as you want in your code behind and add them to the placeholder's controls collection. This is a little better, but still not optimal.
You can put a reasonable number of hyperlink controls on the page, set their visible property to false by default, and "enable" the ones you need. This is preferred to #2 because of some oddities with asp.net page lifecycle.
These links come from somewhere, and that somewhere is usually a database or other reasonable datasource. So make it data driven — bind that datasource to a control like a repeater, gridview, details view, or similar. This is probably your best option.
If you want to generate controls dynamically & don't need to have those PostBack to the server (i.e. when a control is clicked/changed, it will come back to the same page) - you could use controls in System.Web.UI.HtmlControls namespace.
e.g.HtmlAnchor link = new HtmlAnchor();
link.href = "www.stackoverflow.com";
Page.Controls.Add(link);
Hope this gives you enough background.
EDIT: You can use the above code in a loop & replace the fixed value with what comes from a database/object.

Categories

Resources