Windows Phone - Add CSS to WebBrowser displaying third-party webpage - css

I have an app that displays pages from a website in a WebBrowser control. I want to add a "position:fixed" to the <header>, either through an inline style or an external stylesheet saved in my app's resources. Is this possible?

It is possible, although I'm not sure if for online 3rd party pages as well - from your code you can invoke any JavaScript method using this method:
webBrowser.IsScriptEnabled = true; // make sure this property is set on your WebBrowser
webBrowser.InvokeScript(methodName, listOfParameters);
you can either have some target JavaScript method in web page code to be invoked, or you can directly invoke any code using the JavaScript "eval" method like this:
webBrowser.InvokeScript("eval", new[] { "document.body.style.zoom=\"80%\"" }
That's the basic idea, how to change your page programatically from C#, I won't go here into details here how to change CSS values using JavaScript.

Related

get ckeditor plain text server-side from ckeditor in web forms

I need to retrieve the plain text value of the ckeditor html server-side from ckeditor 3.x asp.net control from asp.net web forms. I can get the HTML value in the Text element client-side but I don't seem to have access to CKEDITOR.instances.editor1.document.getBody().getText() server-side like they can in the java sdk. I've looked in the asp.net sdk control for similar named elements and only seem to find mostly configuration related elements in the asp.net control (maybe the java control is more robust than the asp.net control)
I see you can do this in javascript client side but not sure how to translate that into a get server-side like radeditor has with it's text property. Does anyone know how to get to this with the ckeditor? Thanks much for any help
I have the same problem. Now this code works for me
$(document).ready(function () {
CKEDITOR.replace('YorFieldattachedCkEditer');
function setValue() {
var value = CKEDITOR.instances['YorFieldattachedCkEditer'].getData();
alert(value);
}
});

Is it possible to render a web control dynamically?

We have a web control that we want to render into a div on the already loaded page. This needs to happen when the user clicks on a hyperlink in the menu.
The web control has some javascript files that are added to it dynamically in the C# code.
We also arent using aspx files, and want to load the web control onto a normal html page.
Please let me know what would be the best way to do this, or direct me to an article as I'm struggling to find anything useful on the web. any help is appreciated.
You can render your control dynamically in a string, send the string back and place it inside your div. Using ajax you call the code behind to render the control and return you the results.
// load the control
var oCConrol = Page.LoadControl("CustomControl.ascx");
// here you need to run some initialization of your control
// because the page_load is not loading now.
// a string writer to write on it
using(TextWriter stringWriter = new StringWriter())
{
// a html writer
using(HtmlTextWriter renderOnMe = new HtmlTextWriter(stringWriter))
{
// now render the control inside the htm writer
oCConrol.RenderControl(renderOnMe);
// here is your control rendered output.
strBuild = stringWriter.ToString();
}
}
Alternative you can get the same results by just make an extra empty aspx page with only your control inside and call it using Ajax. The result will be again the control
The javascript how ever is dificult and can not go with the control. You can not send text back and then make it run as javascript inside the control, this must be done separated.
I have done something similar by creating a separate, standalone .aspx page and Jquery ajax. Call to the .aspx page using Jquery ajax and return all the rendered HTML from that page via the ajax response.
As far as I know, there is not any way to render a web control outside the context of a .aspx page.

how can i apply complete css on ajax response using jquery

We are building a single page application using jQuery Mobile framework. the scenario we have is that we dont want to use the default behavior of having href links being hijacked and rendered as ajax response because practically, the page change causes the screen to flicker a bit and we dont want that to happen. so, to avoid that, we shall write our own ajax calls to the server to get specific data and update the content on page using the response from the server. However, in doing so any javascript that needs to be bound on the response AND the Css styling on the response HTML is completely lost.
Can somebody please advise what would be the best way to rebind / refresh the JS & CSS for the html received via Ajax.
Read "Enhancing new markup" in the JQM documentation. The trigger('create') event should solve this:
$( ...new markup that contains widgets... ).appendTo( ".ui-page" ).trigger( "create" );

How to change html view of web user control

I am creating a web user control for a simple poll. I am currently registering it on the page and then referencing it via tagprefix.
The form for the poll is in basic html (no server controls) and is in the front-end of the web control. How can I change the look of the user control depending on the settings passed into it? Is this possible without using server controls?
Update
Can I change the html layout of a user control? If so could someone post some examples. Please note I do not use asp.net form controls, so none of that please :)
You might be able to also use jQuery to replace existing css setting in your code. Create properties on for your user control, and then pass settings in the classes. Then use jQuery to replace them. This however requires jQuery to be linked to your page (or within your control) and you'd have to write the CSS classes out to the jQuery code (using server controls, but you could use the literal control so there's no excess code).
Personally I'd go with the option of using server controls instead of straight up HTML, you'd get alot more flexibility, and then passing through the settings would be pretty straightforward, put something like this in your controls backend code:
Private _TextBoxCssClass As String
Public Property TextBoxCssClass() As String
Get
Return _TextBoxCssClass
End Get
Set(ByVal value As String)
_TextBoxCssClass = value
txtBox1.CssClass = value
txtBox2.CssClass = value
End Set
End Property
You most likely want to have a property or event in the control that changes the css. It may end up best to add some server controls or javascript / jquery to make it easier.
If its only the styles you want to change, then you can expose a property to set the style attribuites of the respective control inside your User Control. If you want to control the whole HTML layout of the control then Custom Control is the viable option.

removing duplicate script from page

I am trying to make use of the yahoo exceptional performance rule : avoiding duplicate script
To do so i would like to be able to know whether or not a script was already added to the page before injecting it in the page. It looks like i can't figure what has been added in asp.net code behind unless i have a scriptmanager added to the page. but i would like to avoid using asp.net AJAX. From the description of the rule, it looks like it is something possible in php though.
Assuming that i can't do the check in my code behind, i was considering using jQuery $.getString function but it doesn't check before fetching the script. If i was to choose the javascript file, would i have to parse the whole http response in order to figure out which script was loaded on the page?
If the page is registering the scripts with the ASP.NET Page.ClientScript Register APIs then you can use Page.ClientScript.IsClientScriptIncludeRegistered. On the other hand, if you are using those APIs you don't really need to call it, since it already ensures only one of each is registered.
http://msdn.microsoft.com/en/us/library/system.web.ui.clientscriptmanager.isclientscriptincluderegistered.aspx
If the page just has regular ole script elements statically in the markup, and you need to detect if a script is loaded on the client side, you will have to get all the script elements on the page and look at their .src values. The thing with that is that some browsers automatically resolve that url to a full path, not just the one you declared. So, you can account for that in various ways -- you can just search for the end of the string being the script you want, or you can cause the url you want to compare with to also be resolved by setting it onto a dynamically created script element (which you never add to the DOM but is still resolved for you).
This is just off the top of my head, sorry if I get something wrong:
var s = document.createElement("script");
s.src = "foo.js";
var loaded, scripts = document.getElementsByTagName("script");
for (var i = 0; i < scripts.length; i++) {
if (scripts[i].src === s.src) {
loaded = true;
break;
}
}
if (loaded) {
// this script is already loaded
// assuming you dont have multiple copies in different locations
}
You don't need to use any client-side scripting to do this... you can do this in your code behind using the ClientScriptManager without needing to make use of ASP.NET AJAX (I think you're confusing ClientScriptManager with ScriptManager*) in your control/page just use:
ClientScript.RegisterClientScriptInclude("some-script", "myScript.js");
or from your user controls:
Page.ClientScript.RegisterClientScriptInclude("some-script", "myScript.js");
This will use the key "some-script" & only register one copy of the script on the page.
*To be clear I think the confusion is arrising from the difference between these:
ClientScriptManager if a server-side helper class which is used to manage client side scripts (in other words its whole purpose is to do exactly what you are trying to do). It is accessed via the Page's ClientScript property.
ScriptManager is a Control used to aid client side Ajax scripting in ASP.NET AJAX
(hell I even confused myself & gave the wrong example code initially)
well that wouldn't actually work in a master detail scenario with multiple web user controls.
Then you wouldn't have control over who has to do the script initialization if the web user control is dynamic.
It's easier to link once, but a developer would have to weigh his options between ClientManager and using a script load.
yeah you have to parse the whole response...
why don't you create a javascript file and put all of your javascript there and then import that javascript file in your code??? in this way you can get rid of duplicate script insertion.

Resources