Reputation: 703
I've got a very simple user control written in c# and compiled into a dll. I place that control into an aspx page using an object tag and then try and do things with it in javascript. I've got other controls that work just fine, but for some reason this one doesn't. Here's the code:
using System.Windows.Forms;
namespace FileBrowser {
public partial class theBrowser : UserControl {
public theBrowser() {
InitializeComponent();
MessageBox.Show("TBI");
}
public string theFile = "foobar";
}
}
Here's the web page code:
<object id='fileBrowserControl' classid='http:FileBrowser.dll#FileBrowser.theBrowser'>
<span>File control did not initialize.</span>
Then
<script type="text/javascript">
$(function() {
var mfc = $('#fileBrowserControl')[0];
alert(mfc.theFile);
});
When I load the page, the MessageBox shows that the constructor has fired, but the javascript alert gives 'undefined' for the component string. This is a stripped down version, in the real version, I also cannot call public functions from javascript. I get the error, 'the object doesn't support this property or method.'
I'm obviously missing something really simple, but I don't see it. Thanks for any help. Jon
Upvotes: 2
Views: 3010
Reputation: 703
Solved! Thanks for the input.
Here's an article that shows how: http://www.olavaukan.com/2010/08/creating-an-activex-control-in-net-using-c/
Upvotes: 0
Reputation: 8882
Yeah, ASP.NET managed code doesn't hook into client side script automatically, you've got to wire that up yourself. My suggestion would be to:
1) Create your UserControl as a Web UserControl
2) Register it on your aspx page: http://msdn.microsoft.com/en-us/library/sbz9etab.aspx
3) In the code-behind of your user control, after it loads, push your "theFile" property value into your control's script using something like this:
Page.ClientScript.RegisterStartupScript(Page.GetType(),"inject","var theFile='" + this.theFile + "';",true);
4) Then you can access that value in JavaScript using the "theFile" variable
Upvotes: 1