Set JavaScript events in WebBrowser control - c#

I have a WebBrowser control and try to set onclick and href attributes on all links.
foreach (HtmlElement link in webBrowser1.Document.Links)
{
link.SetAttribute("href", "http://www.google.com");
link.SetAttribute("onclick", "return false;");
}
It works well. When i out source code of outer html i see that attributes was exist.
But JavaScript code does not work. Why and how i can force WebBrowser control to execute javascript code?

onclick is a property of type IDispatch. You can assign a function with no parameter to the property or add an attribute to HTML and let the parser to do this for you but you can't assign a string to it via DOM.
You can either pass an object that implements IDispatch and the default method (dispid=0) to the property, or use IHTMLElement2::attachEvent to attach an event handler. If you are using Microsoft's class libraries, you can use Microsoft's wrappers like HtmlElement.AttachEventHandler in Windows Forms and HtmlElement.Attachevent in Silverlight.

Related

Change font-weight of an asp:RadioButton Text in client script

I have 2 asp:RadioButton that are unchecked when the page is loaded. When one is checked, I want to change the font-weight to bold in client script. I tried:
radio1.style.fontWeight = 'bold';
but it didn't work.
You can access the the css class using the attributes property. I am assuming radio1 is the id if so you could modify your c# code to
radio1.Attributes["class"] = "newCssClassName";
I personally have never modified a single property of css using c#. To do that I have always used something like jQuery.
Try to create a new css class such as
.rbSelected
{
font-weight:bold;
{
And then toggle them accordingly ( your css property should be font-weight not fontWeight)
Also a word of warning with controls such as radio buttons and checkboxes the browser will override your styling in most cases. Each browser will render the control differently regardless of what style(s) you apply. If you are looking for something custom you may need to create your own control/element (Such as an image/element that looks like a checkbox but has manages states handled in code)
For bold
$("#YourElementID").css("font-weight","bolder");
For regular
$("#YourElementID").css("font-weight","normal");
You can create a function with this lines to toggle the font-weight. To call the javascript function from the server side controls use:
radio.Attributes.Add("onclick","functionname()");
This will add the functionname to the onclick event when the radio button is rendered to the browser.

How to update DOM content inside WebBrowser Control in C#?

I've a windows .Net Form which contains a WebBrowser Control.
This WebBrowser displays a webpage based on its Url property.
Can I modify the DOM of the displayed page inside the WebBrowser control ?
If yes, how to ?
For those who are interested, here's the solution:
HtmlElement headElement = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptElement = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement domScriptElement = (IHTMLScriptElement)scriptElement.DomElement;
domScriptElement.text = "function applyChanges(){/*DO WHATEVER YOU WANT HERE*/}";
headElement.AppendChild(scriptElement);
// Call the nextline whenever you want to execute your code
webBrowser1.Document.InvokeScript("applyChanges");
From http://msdn.microsoft.com/pt-br/library/system.windows.forms.webbrowser.aspx:
You can also manipulate the contents of a Web page through the Document property, which contains an HtmlDocument object that provides managed access to the HTML document object model (DOM) for the current page. This property is useful, when used in combination with the ObjectForScripting property, to implement two-way communication between your application code and dynamic HTML (DHTML) code in a Web page, letting you combine Web-based controls and Windows Forms controls in a single user interface. You can use the Document property to call scripting code methods from your application. Your scripting code can access your application through the window.external object, which is a built-in DOM object provided for host access, and which maps to the object that you specify for the ObjectForScripting property.

How to update an HTML control from code within SilverLight

How can I update a textbox or label (specfically an asp.net control) text property from the code in the silverlight control?
Suggested solution:
I suppose that you could try to do it in two steps:
write a javascript function that updates a control based on a given parameter, let's name it updateControl:
<script type="text/javascript">
function updateControl(newValue)
{
//update your control here with newValue parameter with javascript
...
}
</script>
in your Silverlight application (in the place you want to invoke the control value change) you should write:
HtmlPage.Window.Invoke("updateControl", "this is a new value")
Another solution for page update only:
If you just need to refresh the page to get the value from other place, you can write in your Silverlight code:
HtmlPage.Document.Submit()
In the postback, you could get this data and show it in the control.
References and useful resources:
ScriptObject.Invoke Method : http://msdn.microsoft.com/en-us/library/system.windows.browser.scriptobject.invoke%28v=vs.95%29.aspx
Walkthrough: Calling JavaScript from Managed Code: http://msdn.microsoft.com/en-us/library/cc221359%28v=vs.95%29.aspx
Silverlight and JavaScript interop basics: http://pietschsoft.com/post/2008/06/Silverlight-and-JavaScript-Interop-Basics.aspx
How to set the value of a form element using Javascript: http://www.javascript-coder.com/javascript-form/javascript-form-value.phtml
You can do it calling javascript function from silverligt.
Shortly it looks like this:
HtmlPage.Window.Invoke("globalJSMethod", stringParam);
Note that javascript method must be accessable from window - window.globalJSMethod(...)
Check this walkthrough to see in details how to do this.

use a javascript from embeded resource of a custom control on a page

I have a custom server control which renders some HTML on the aspx page it is added.
protected override void RenderContents(HtmlTextWriter output)
{
Text = GetHTMLContent();
output.Write(Text);
}
the GetHTMLContent() retuns some HTML, say
<div id="panel" onMouseOver="hide"><table><tr><td>Something Here</td></tr></table></div>
And I have a javascript file which is an embeded resource in this server control. This javascript file contains a function, say
function hide(){
document.getElementById("panel").visible = false;
}
I add the custom control in an aspx page like this
<cc1:CControl ID="Div" runat="server"></cc1:CControl>
now when I open in browser, the HTML contents are rendered fine, but javascript needs to be working.
My question is how can we make the function, which is in javascript file embeded in custom control, work on a aspx page where the custom control will be loaded?
Thanks
There are several ways. First can be to have an OnClientHide="hide" property, where this property defines the name of a method to call as a callback. Your control can pass this to the onmouseover client event handler during rendering.
Or: have your control write javascript to the browser, like this: http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx
EDIT
Also check out this example: http://www.karpach.com/Custom-ASP-NET-server-control-with-embedded-resources.htm
Besides what Brian Mains said, you javascript to hide is incorrect. It should be:
function hide()
{
document.getElementById("panel").style.display = "none";
OR
document.getElementById("panel").style.visibility = "hidden";
}

stop setting onclick event for asp:LinkButton if no OnClick event was explicitly defined

How do I setup a default setting so that if I do not set an OnClick (i.e the asp.net OnClick attribute) explicitly for an asp:LinkButton tag, it will not render an onclick(html attribute for javascript) attribute client side? By default, asp.net adds an onclick='doPostBack....' for the LinkButton.
Case for use:
There is a LinkButton tag on the page. For this page, if the user has one friend, I only want to run client side code if the button is clicked and would not for any reason want to make a post back. If the user has more than one friend I would want a click to trigger a postback.
Solutions that include the following are not helpful:
Using any asp.net Ajaxtoolkit
Dynamically switching the control type (i.e. if friends == 1 use a asp:Hyperlink)
-I want to avoid this because it is not scalable. There might be many cases where I want an asp:Link tag to do a postback or to not do a postback depending on the user context or user attributes
Using OnClientClick (I am using jQuery would like to avoid this)
Solution that would be helpful if possible:
If I could see server side at runtime whether an OnClick event was explicitly set on an asp:LinkButton tag, this would solve my problem, too. any ideas?
How about rather than dynamically switching the controls (as you mentioned is a solution you don't want), you could always use an asp:HyperLink and set the NavigateUrl property to redirect your page back to itself with a query string of some sort indicating what was clicked.
If you don't want the post to happen at all, simply leave the NavigateUrl property blank.
Of course, this will be pretty worthless if the rest of the page is dependent on ViewState and such.
http://forums.asp.net/t/1129106.aspx
This link explains how to see server side at runtime whether an OnClick event was explicitly set using reflection

Categories

Resources