This is a migrated thread and some comments may be shown as answers.
VB.NET syntax for OnDocumentReady
3 Answers 58 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Patrick Barranis
Top achievements
Rank 1
Patrick Barranis asked on 04 Feb 2010, 06:50 PM
Hi.  I can't seem to figure out the VB.NET syntax for using ScriptRegistrar.OnDocumentReady.  Can someone please translate this snippet from the Demo to VB.NET please?

 

.OnDocumentReady(() => { %>prettyPrint();<% }) 

Also, is it possible to call OnDocumentReady from code-behind?  Specifically, I want to make a simple HtmlHelper extension method, and I can't figure out what the syntax must be.  The following compiles but doesn't appear to do a thing:

    <Extension()> _  
    Public Sub SetDefaultFocusedTextbox(ByVal html As HtmlHelper, ByVal id As String)  
        html.Telerik.ScriptRegistrar.OnDocumentReady(Function() String.Format("$(""#{0}"").focus();", id))  
    End Sub 
 

Thanks!

3 Answers, 1 is accepted

Sort by
0
Kelly Stuard
Top achievements
Rank 1
answered on 04 Feb 2010, 07:45 PM
The <%="Test" %> is a shortcut for <% Response.Write("Test") %>.

So in your case the following should work:
Html.Telerik.ScriptRegistrar.OnDocumentReady(Function() Response.Write("Test")) 
0
Patrick Barranis
Top achievements
Rank 1
answered on 04 Feb 2010, 08:26 PM
I had the same thought, but that produces the compiler error "Expression does not produce a value.".  Also, from the HtmlHelper object you have to get to the response object, so the code ends up looking like this:

html.Telerik.ScriptRegistrar.OnDocumentReady(Function() html.ViewContext.HttpContext.Response.Write("alert();")) 

I've also tried changing "Function" to "Sub"; given the error message, that seemed logical.  But then I got the compiler error "Expression expected."

Thanks in advance,
Patrick
0
Kelly Stuard
Top achievements
Rank 1
answered on 04 Feb 2010, 08:46 PM
Ahh yes, the good old Action delegate in VB.net.

There is no direct equal to C#'s:
() => DoSomethingThatReturnsVoid(); 
This will fail because a function has to return something:
Function() DoSomethingThatsASub() 
However, this will work:
Function() DoSomethingThatReturnsInteger() 

http://msdn.microsoft.com/en-us/library/018hxwa8.aspx
"Visual Basic requires that a lambda expression return a value. As a result, that return value must be discarded if the lambda expression is to be used with the Action<(Of <(T>)>) delegate."

Take a look at the work-arounds they do in the lambda example to make it work. Basically, "Function()" has to return a value, otherwise it's a Sub. But the language is nice enough to discard the return if it's running an Action delegate.
Tags
General Discussions
Asked by
Patrick Barranis
Top achievements
Rank 1
Answers by
Kelly Stuard
Top achievements
Rank 1
Patrick Barranis
Top achievements
Rank 1
Share this question
or