C# - How To Dynamically Render Controls From String
Feb 28, 2010
Let's say I have a string that I retrieve from a DB like: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et {{Hyperlink|navigateurl='/foo.aspx'}} dolore magna aliquyam."
This string may now get assigned to the Text-property of a label. What I want is to parse {{Hyperlink|navigateurl='/foo.aspx'}} and replace it with
and assign the whole text including the HyperLink-Control to the Label.
Is that even possible? I think I could use reflection to create the control and set the properties. (the HyperLink-Control was just an example) But can I manage to insert the asp.net control back into the string to ensure the hyperlinks rendering as server contorl?
I hope you understand what I want. If not, feel free to post comments.
Edit1:
what do you mean by "assign the whole text including the HyperLink-Control to the Label."? can you xplain a bit, the reason for doing so ?
I think it won't work to assign the control into the string, because a asp.net control cannot be fit into a string.
After some thinking I found a way to achieve my goal. That would be to create a placeholder (I name it A). Therein some Literal control will be added. In addition I would create a placeholder (I name it B), add my Hyperlink into B, and add A into B. But I think is way to overkill.
The reason why I started thinking about this, was to gain access to Server.MapPath without replacing occurences in the string. I want to be able to use relative paths in my CMS, that get rendered like the NavigateUrl property from a hyperlink. Nevertheless I think my question with the dynamically creation is worth thinking about
Iam getting the below string in a variable"result" after xslt tranformation. if i do Page.ParseControl(result) iam able to render controls to the page. But the requirement is different. I have 4 place holders in that aspx page.I need to bind div id="Section1" to placeholder1, div id="Section2" to placeholder2 and similar the other two sections also to the corresponding place holder. how can i loop and add controls to corresponding place holder.
Iam getting the below string in a variable"result" after xslt tranformation. if i do Page.ParseControl(result) iam able to render controls to the page. But the requirement is different. I have 4 place holders in that aspx page.I need to bind div id="Section1" to placeholder1, div id="Section2" to placeholder2 and similar the other two sections also to the corresponding place holder. how can i loop and add controls to corresponding place holder.
The thing is that the HtmlContent string is getting "auto decoded" so the paragraph-elemnts gets renderd as <p>(sry might mix up decoding and encodeing..Im always getting confused by those)
anyway..I want the string to render as proper Html...how can this be achieved?
way to read xml from a database and then dynamically render out the node name in text and the node value in a checkbox/textboxI want to be able to edit the values and then save it back to the database.I know that you can use serialize the xml to a class object but that only works when you know how the xml looks like.. if im not mistaken?
I would like to get the html code a view would generate in a string, modify it in my controller, then add it to my JsonResult.
I found code that would do what i'm talking about from a partial. I would like to do it from an aspx View though.
Extra explanation:
Let's say I have a page Frame.aspx that /Controller/Frame will return I would like to get my hand on the response before it out so I can to wrap it with jsonp. I do not wish to edit the return result in code every time, this is why I want to load the view programmatically.
/Controller/Frame currently returns Frame.aspx's content: <html><body>hello</body></html>
Let's say there's a function that renders a view in a string builder
StringBuilder sb = new StringBuilder(); RenderView(sb, "Frame");
I have come across a requirement to dynamically show or hide asp.net web controls , html server controls. I want to do this through a configuration XML (as it would be easier to config). So each page /aspx will have a configuration xml.
Would it be possible to create a control (may be a custom control , to which i can attach the xml file). The control (lets says - call it sequencer) should decide which control should be ordered in which order and if the control has to be shown or not. Ideally this sequencer control should use the page events and take decision on rendering the child controls.
We have an application where the controls are dynamically added to the form based on the logged in user,
This user controls related data is stored in the database. There is lot of logic in the code behind in the asp.net webforms which is so messy to maintain it, we are planning to re-write it using the MVC framework.
I would say there would be rougly 50 different forms based on the user logged in, is it good idea to create so many views and redirect to the appropriate view based on the logged in user, in some of the forms there might be minor difference like the order of the Control might change.
I need to dynamically modify the contents of a column in a GridView1 before it is displayed. Basically, I need to convert every 'Environment.NewLine' in a field to a so it displays as a new line on an ASP.NET page. How do I do this?
When the page loads it works fine. (and lots 10 links) on the update panel it hits the same function and tries to get another 10 links. I set the navigationURL to something like
"../Folder/Mypage.aspx?498592ghjgfosdfgo"
It is set identically both times(load and updatepanel postback) but when i try to render it to html the second time (on the update panel) it adds "../" to the front so i end up with
"../../Folder/Mypage.aspx?498592ghjgfosdfgo"
The function where it changes here
Public Shared Function RenderControl(ByVal c As Control) As String Dim sw As New IO.StringWriter Dim htmlw As New HtmlTextWriter(sw) c.RenderControl(htmlw) Return sw.ToString End Function
c is the hyperlink which has the propertry navigationurl (this never gets changed) but the sw which ends up looking like this on load
Is there a way that I can create a page and master page and have some of the values dynamicly loaded thru selecting items in the querystring parameters.....
then is there a way that I can render that page only to a text string? by calling it with some querystring parameters and have it render to a string?
I guess my question is only the second part as I already know how to create a page with masterpage etc...
I also know how to create an email and snd it. I just dont know how to render the page to a string
I guess what I mean is that I want to render the page on the server in code behind and then have a text string which would be the rendered HTML in a string variable of some sort... I dont want to render the page to a users browser... in fact there would be no user at all connected (there is no need to be)What I want to do is (in a code module) load up a page created and stored in my wed app directory. I want to be able to load it from a code module and to bhe able to load it with some parametsrs and just get the rendered HTML.
I am trying to write a module which will send a HTML email to registered users and I want that HTML to be dynamicly created depending on some values in the recipient user,s profile.
I've got a page that I'm using as an e-mail template. I'd like to spin off a new thread, take the rendered page, and put it into an e-mail. Is this possible? If so, how? I haven't had luck so far.
I have some code that is used to replace certain page output with other text. The way I accomplish this is by setting the Response.Filter to a Stream, Flushing the Response, and then reading that Stream back into a string. From there I can manipulate the string and output the resulting code. You can see the basic code for this over at [URL] However, I noticed that Page Caching no longer works after the first Response.Flush call.
I put together a simple ASP.NET WebApp as an example. I have a Default.aspx with an @OutputCache set for 30 seconds. All this does is output DateTime.Now.ToLongTimeString(). I override Render. If I do a Response.Flush (even after the base.Render) the page does not get cached. This is regardless of any programmatic cacheability that I set. So it seems that Response.Flush completely undermines any page caching in use. Why is this?
extra credit: is there a way to accomplish what I want (render output to a string) that will not result in Page Cache getting bypassed?
I am trying to use aspx pages as an email templates. There will likely be a bunch of objects on the page which will be used as replacements in the html. Because it's an aspx page I'll be able to use databinding, repeaters, etc. At run time, I want to be able to instantiate the aspx page from its path, pass in a bunch of properties, and then get the rendered result of the page and email it. This seems pretty straightforward from a asp.net website (maybe using BuildManager or Server.Execute.) However, I want to be able to use the same templates via a console application by just loading up a page object from its filepath. Is this possible?
I have a gridview that is built using a objectdatasource and set to autogenerate the columns based on that source, on some other pages i have a gridview that is built within the page with template fields and so on..
in that gridview i have a field that displays the time value like so
I've been watching a video on Scott Hanselmnn teaching MVC 2 tricks/tips. He mentions how MVC 2 by default uses ASP.NET Web Forms view engine to render the output of the views; he mentions that the web forms view engine is a little slower than it could be for MVC 2 since it generates a control tree and then outputs the HTML to the page (I hope I said that right).
I was wondering what he meant by web forms generating a code tree before outputting the HTML to the page. Does anyone have insight on the view engine of Web forms and the steps of the rendering process works for ASP.NET and MVC2?
I need to dynamically construct a url by appending new parameter to the current url.
I know how to do this in the long way by getting the current url and check if there is any QueryString, if no add "?" if yes add "&". Also check is the parameter is already exist. If yes change the value.
But is there any build in .NET function to do this? Like Request.QueryString.Add
Is there some way that qury string variables can be added dynamically ? Suppose Page one.aspx?Id=3 redirect me to Two.aspx
Can I add some parameters in Two.aspx before page loads ? I know query string collection is read only and it looks not possible but just asking there may be some way to do this.
Up until now I only needed one connection string since I had only one database but this has changed since I want some users to connect to database A and others to database B What would be the correct way to go about it?
I'm trying to reference a public property from a string. How can this be done in vb.net? I have the text value of "FirstName" stored in strucParam(i).TxtPropertyName.
This is what I'm currently doing:
Dim tmpValue As String Dim ucAppName As UserControl_appName = CType(Parent.FindControl(strucParam(i).ParentFindControl), UserControl_appName) tmpValue = ucAppName.FirstName.Text
How can I use the value in strucParam(i).TxtPropertyName so that I can remove ".FirstName" from my code?