# Implementing Client-side Events Using the BrowserTextControl

> The BrowserTextControl runs as a .NET user control inside Internet Explorer, but its events are inaccessible to JavaScript by default. Defining a COM-visible interface via ComSourceInterfaces with delegate-backed events allows those events to be raised and caught by client-side script.

- **Author:** Bjoern Meyer
- **Published:** 2008-07-11
- **Modified:** 2026-03-05
- **Description:** The BrowserTextControl runs as a .NET user control inside Internet Explorer, but its events are inaccessible to JavaScript by default. Defining a COM-visible interface via ComSourceInterfaces with delegate-backed events allows those events to be raised and caught by client-side script.
- **3 min read** (592 words)
- **Tags:**
  - .NET Server
  - Sample
- **Web URL:** https://www.textcontrol.com/blog/2008/07/11/implementing-client-side-events-using-the-browsertextcontrol/
- **LLMs URL:** https://www.textcontrol.com/blog/2008/07/11/implementing-client-side-events-using-the-browsertextcontrol/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2008/07/11/implementing-client-side-events-using-the-browsertextcontrol/llms-full.txt

---

When building a browser-based word processing application using TX Text Control Server for ASP.NET (incl. Windows Forms), the BrowserTextControl is wrapped in a *Windows Control Library*. This provides many advantages including the fact that most of the functionality can be wrapped in the user control and must not be implemented from outside using Javascript or any other client-side scripting language.

However, we are building web applications! Today's web applications are built using client-side scripting for different purposes like an AJAX implementation or the client-side communication between different integrated controls.

By implementing the events in the wrapper control, you can trap them inside of the control that is isolated in the web page. The browser is not able to access these events, though.

But trapping such an event using client-side Javascript could be really helpful. Consider the following example:

You implemented a function to save the content of the BrowserTextControl to post it to the server using HTTP POST like described in our [documentation](https://docs.textcontrol.com/textcontrol/windows-forms/index.htm). This client-side call should be triggered by the wrapper control when the user clicks a specific button or menu entry. On this action, the wrapper control should fire an event that can be trapped client-side using Javascript to call the implemented save function.

Sounds good! But how to implement that?

Due to an [article in the asp.netPRO magazine](http://www.aspnetpro.com/features/2005/09/asp200509so_f/asp200509so_f.asp) from [Steve C. Orr](http://steveorr.net/about/), an expert I hold in great respect, it is not possible to raise events back to the page:

*BTW: I honestly recommend to read this article completely, it perfectly describes the methodology behind this technology.*

*"\[...\] Another issue is that communication between client-side script and Windows controls is currently one-way only. That is, JavaScript functions within the page can call methods of the hosted Windows control, but the Windows control cannot raise events back to the page. \[...\]"*

From what we have learned above, this statement is true. Anyway, there is a way to get around this catch-22 situation.

The Internet Explorer accesses the Windows Control Library properties and methods using the implemented COM interface that enables Internet Explorer to execute client-side controls like ActiveX controls, Flash players or our .NET assembly. This doesn't imply that this approach uses COM or ActiveX technology. The Internet Explorer simply uses the existing interface to handle everything that is between <OBJECT> and </OBJECT>. This is the reason why the 'Make assembly COM-Visible' checkbox must be checked in the assembly properties.

To implement a public COM-visible event, it is required to create a COM-interface in the wrapper control:

```
[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface BrowserApplicationEvents
{
    [DispId(0)]
    void SaveDocument();
}
```

This visible interface enables the client-side Javascript to trap our events. Then you need to identify a list of interfaces that are exposed as COM event sources in your wrapper class:

```
[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDispatch), ComSourceInterfaces(typeof(BrowserApplicationEvents))]
public partial class BrowserAppControl : UserControl
{ ... }
```

The event itself can be implemented to any event in .NET:

```
public delegate void SaveDocumentHandler();
SaveDocumentHandler saveDocument;

public event SaveDocumentHandler SaveDocument
{
    add { saveDocument += value; }
    remove { saveDocument -= value; }
}

protected virtual void OnSaveDocument()
{
    SaveDocumentHandler handler;
    handler = saveDocument;

    if (handler != null)
        saveDocument();
}
```

This event is now publicly accessible for client-side scripting languages:

```
<script for="BrowserApp" event="SaveDocument" language="javascript">
    alert("In Javascript client-side event, you can call the client-side save method.");
</script>
```

Feel free to contact me, if you would like to learn more about this approach.

[Download the sample code here](https://s1-www.textcontrol.com/assets/dist/blog/2008/07/11/a/assets/tx_browser_events.zip)

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [ASP.NET DocumentViewer Sample Online](https://www.textcontrol.com/blog/2009/05/15/aspnet-documentviewer-sample-online/llms.txt)
- [Create a Table of Contents in Windows Forms using C#](https://www.textcontrol.com/blog/2023/01/23/create-toc-in-windows-forms/llms.txt)
- [Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub](https://www.textcontrol.com/blog/2023/01/08/official-tx-text-control-net-sample-applications-are-now-hosted-on-github/llms.txt)
- [Detect Toggle Button Changes Using a MutationObserver](https://www.textcontrol.com/blog/2021/11/11/detect-toggle-button-changes-using-a-mutationobserver/llms.txt)
- [Two Ways to Restart Numbered Lists in TX Text Control](https://www.textcontrol.com/blog/2021/11/03/two-ways-to-restart-numbered-lists/llms.txt)
- [Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More](https://www.textcontrol.com/blog/2020/12/09/zoom-tricks-disabling-ctrl-mouse-wheel-and-more/llms.txt)
- [AutoCorrect Using TX Text Control and TX Spell .NET](https://www.textcontrol.com/blog/2017/08/17/autocorrect-using-tx-text-control-and-tx-spell-net/llms.txt)
- [Creating Conference Badges with Text Control Reporting](https://www.textcontrol.com/blog/2017/02/14/creating-conference-badges-with-text-control-reporting/llms.txt)
- [Using Custom Document Properties to Store Additional Document Information](https://www.textcontrol.com/blog/2017/02/09/using-custom-document-properties-to-store-additional-document-information/llms.txt)
- [Use SubTextParts to Protect Document Parts](https://www.textcontrol.com/blog/2015/12/29/use-subtextparts-to-protect-document-parts/llms.txt)
- [Reporting: Styling the DocumentViewer for ASP.NET](https://www.textcontrol.com/blog/2015/06/14/reporting-styling-the-documentviewer-for-aspnet/llms.txt)
- [Reporting: Merging MS Word Documents with DocVariables](https://www.textcontrol.com/blog/2015/06/10/reporting-merging-ms-word-documents-with-docvariables/llms.txt)
- [TextControl.Web: Determine when a Document Has Been Completely Loaded](https://www.textcontrol.com/blog/2015/06/09/textcontrolweb-determine-when-a-document-has-been-completely-loaded/llms.txt)
- [TextControl.Web: Adding Custom Ribbon Tabs](https://www.textcontrol.com/blog/2015/05/29/textcontrolweb-adding-custom-ribbon-tabs/llms.txt)
- [Building a Touch-enabled Button Bar with Javascript](https://www.textcontrol.com/blog/2015/05/27/building-a-touch-enabled-button-bar-with-javascript/llms.txt)
- [TextControl.Web: Inserting Merge Fields Using Javascript](https://www.textcontrol.com/blog/2015/05/16/textcontrolweb-inserting-merge-fields-using-javascript/llms.txt)
- [MailMerge: Merge Hyperlinks into Merge Fields](https://www.textcontrol.com/blog/2015/05/15/mailmerge-merge-hyperlinks-into-merge-fields/llms.txt)
- [Mail Merge: Suppress Lines with Empty Merge Fields](https://www.textcontrol.com/blog/2014/08/01/mail-merge-suppress-lines-with-empty-merge-fields/llms.txt)
- [Interactive Spelling Suggestions Using TX Spell .NET](https://www.textcontrol.com/blog/2014/07/30/interactive-spelling-suggestions-using-tx-spell-net/llms.txt)
- [Alternate Row Colors Using NEXT Fields](https://www.textcontrol.com/blog/2013/12/30/alternate-row-colors-using-next-fields/llms.txt)
- [Loading Dictionaries Dynamically in TX Spell .NET](https://www.textcontrol.com/blog/2013/12/20/loading-dictionaries-dynamically-in-tx-spell-net/llms.txt)
- [Restart the Page Numbering Fields Per Section](https://www.textcontrol.com/blog/2013/11/08/restart-the-page-numbering-fields-per-section/llms.txt)
- [TX Barcode .NET: Test the Barcode Engine Live!](https://www.textcontrol.com/blog/2013/07/10/tx-barcode-net-test-the-barcode-engine-live/llms.txt)
- [SD Times .NET Component Buyers Guide 2013: Simplify Report Creation](https://www.textcontrol.com/blog/2013/06/06/sd-times-net-component-buyers-guide-2013-simplify-report-creation/llms.txt)
- [Inserting MS Word Compatible Page Number Fields](https://www.textcontrol.com/blog/2013/05/30/inserting-ms-word-compatible-page-number-fields/llms.txt)
