C# WebBrowser-Control: how to replace Document-Parts dynamically - c#

I want to use a WebBrowser-Control within a WebForm-App. It should load a local Webpage which includes several JS and Images. Is it possible to replace placeholderstrings within the JS before the page gets loaded?
So something like that: get HTML->replace strings via string.replace(..,..)->load the new HTML into the control.
Thanks a lot!

Related

Inserting a c# string in a webbrowser component, how can i do this?

I'm creating a program that will generate a HTML page with information pulled from a database, some HTML is loaded into a webbrowser component and this webbrowser gets printed.
So in c#:
- There's a webbrowser component
- With some HTML code added into it
I'm trying to insert c# strings, textpulled from a database on specific places in the HTML that gets displayed in the browser component, but i have no clue how. I know in PHP for example, i am able to do between HTML text for example. Does something similar exist for C#? I'm working in winforms.
I'm looking for something like:
<html>
...
...
<h1>$String1</h1>
...
...
</html>

c# check if contentplaceholder has a userdefined control

I have a situation where in I am adding .js file to my aspx page using content place holder.
I need to add all the js to end of the page(the said content place holder is at bottom of the master page)
Here's the code for the same
ContentPlaceHolder content = (ContentPlaceHolder)Page.Master.FindControl("additionalJavaScript");
content.Controls.Add(new DeferedMinifiedScript("~/Scripts/Controls/ListBoxControl.js"));
Now the issue is I need to add the ListBoxControl.js file only once on the page.
But this is adding the file everytime a new listbox control is rendered on the page.
I need to put a check in place to see if the file is already available as control on page then skip adding it again.
I tried the following
var abc = new DeferedMinifiedScript("~/Scripts/Controls/ListBoxControl.js");
if(content.Controls.Contains(abc))
{
//Don't add the file again.
}
But this is still adding the file again and again.
Is there a way out?
Also, I can't use RegisterClientScriptInclude or similar functions
You should be able to use this:
https://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.content(v=vs.110).aspx
and Set the contents to a string and then use string to compare to see if it contains your .js string/path to file, then if it does do not do.

parsing html, source code, javascript problem

http://booking.travel24.com/index.php?KID=610000&&id=lmpergebnis&showresult=1&detail=zielgebiet&region=-1&ziel=-1&termin=20.02.2011&ruecktermin=17.03.2011&dauer=-1&abflughafen=46&personen=25;25&kategorie=-1&verpflegung=-1&zimmer=-1
I am trying to parse some HTML parts of this page, but when I check the source code I can not find this: "Tunesien, Marokko".
If I check with xdeveloper I can see this as html:
<a class="reglreg" href="javascript:s_hliste(20009);">Tunesien, Marokko</a>
but if i check source code of the page I can't find this. Why?
If you view the source and search for "Marokko" you will see there are several places where it occurs (loaded as data in several JavaScript arrays).
It appears as if some of the content is produced dynamically through the JavaScript loaded onto the page. That JavaScript builds HTML and changes the page to include the content you are looking for.
To answer your first real question
Why?
Because when you check the source code inside a browser, you'll get the original html code. Then javascript comes along and modify the DOM which you can follow in any modern browser's console.
can i get somehow whole source code
then? if i can not see it in browser
how can i see it?
To make it simple, it depends how you're trying to parse it. With what language?
maybe the data is coming via AJAX call, so it's not there on the html at the start, but dynamically added to it.
if you need to parse this, you can try to "emulate" the ajax call yourself.

Methods for dynamically building JavaScript within an ASPX page?

I have a page that is referenced via a <script> tag from a page on another site. In the script src, I pass in the form I want my script to build (from a db table), and the div where the dynamically built form should go. The calling page looks something like this:
<div id="FormContainer"></div>
<script type="text/JavaScript" src="http://www.example.com/GenerateForm.aspx?FormId=1&div=FormContainer"></script>
GenerateForm.aspx contains the code that reads the QueryString parameters for the FormId, and the Div Id, and outputs JavaScript that will build the form.
My question is this. What are the different methods for "outputting" the JavaScript? Some of the JavaScript is static, and can be packaged into an external .js file and I have jQuery too. But should I add that on the GenerateForm.aspx markup page? Or should I use a ScriptManager?
And what about the dynamically built JavaScript? Currently I'm just using Response.Write() for a proof of concept, but instead, should I be doing something else? Use a Literal control on the page and set its value? Use a ScriptManager? Something else?
I know this is a verbose question, so thanks in advance!
If you want to use a seperate, referenced Javascript file, you probably want to do is use an ashx file. Basically this is just a generic handler that you'll use to write directly to the output stream without having to deal with the ASP.NET page lifecycle. If you add a basic Generic Handler (.ashx) to your site from the Add New Item dialog, the template should be enough direction, using context.Response.Write() to output your Javascript dynamically.
The ScriptManager is more useful if you want to output individual lines of Javascript to be ran at certain times, like after an event has fired. Then you can do ScriptManager.RegisterClientBlock(this, this.GetType(), "CodeBlock", "alert('Button clicked');", true); to show a client alert box after a button has been clicked, for example.
Static files should be handled just that way - statically. The server can handle the caching, and does not cause unnecessary processing if you reference the static script file directly from the script tag. However, if you need to load a static script dynamically, you could, for example, create a literal that had the <script> tag inside it. This way it uses the browser's cached version of the static file.

Render a .NET Web Control from a string?

Is it possible in ASP.NET to dynamically load up a WebControl from a string with some tag contents in it (without writing a bunch of custom code)?
For instance, I have a string like the following:
string controlTag = "<asp:Label ID=\"lblLabel\" runat=\"server\" />";
I then want to do something like the following to load up the control from that string:
WebControl webControl = LoadControlFromTagString(controlTag);
I can simply parse the string myself and dynamically load up a control in LoadControlFromTagString, but I was wondering if there is anything built in to .NET I can take advantage of. Any suggestions?
There are several choices, depending on what you want to do your control instance (and how much control you want over stuff like rendering, databinding, etcetera).
The easiest is probably TemplateControl.ParseControl(String) which you have access to via your current Page instance.

Categories

Resources