This is a migrated thread and some comments may be shown as answers.

[Solved] Fluent Interface and Conditional Event

4 Answers 122 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Hardy Wang
Top achievements
Rank 1
Hardy Wang asked on 05 Jul 2011, 02:25 AM
Hi all,

I know the whole Telerik MVC controls use fluent interface to setup the control, such as
<%= Html.Telerik().TreeView()
    .Name("myName")
        .ClientEvents(events => events.OnSelect("Event1").OnLoad("Event2"))
%>

But how about I need to wire up control events based on some condition. For example what I need here is that if some property value of my view model is some certain value I wire OnLoad("Event2") event, all other time I don't need such event. I don't think I can do if..else.. in the control setup code.

Thanks

Hardy

4 Answers, 1 is accepted

Sort by
0
John DeVight
Top achievements
Rank 1
answered on 06 Jul 2011, 12:17 PM
Hi Hardy,

I believe you could use an inline conditional statement to determine which event handler to invoke for OnLoad.  Here is an example based on a Model of type Person where the Person.Age > 30, then invoke "Event1" else invoke "Event2":

<%
Html.Telerik().TreeView()
    .Name("myName")
    .ClientEvents(events => events.OnLoad(Model.Age > 30 ? "Event1" : "Event2")
%>


Let me know whether this works for you or not.  If your condition is more complicated than this, we could look at using an extension method.

Regards,

John DeVight

0
Hardy Wang
Top achievements
Rank 1
answered on 08 Jul 2011, 01:22 AM
Hello John,

Thanks for your suggestion, but you solution works if I need to select between different event names.

If I need to turn on/off one particular event then it does not work, because parameters to client event definition methods could not be empty.

Thanks
Hardy
0
Accepted
John DeVight
Top achievements
Rank 1
answered on 08 Jul 2011, 02:25 AM
Sorry Hardy.  I misunderstood.  How about this:

<%
    Telerik.Web.Mvc.UI.Fluent.TreeViewBuilder treeViewBuilder = Html.Telerik().TreeView().Name("myName");
    if (Model.Age > 30)
    {
        treeViewBuilder.ClientEvents(events => events.OnLoad("OnLoadEvent"));
    }
    treeViewBuilder.Render();
%>

Regards,

John DeVight
0
Hardy Wang
Top achievements
Rank 1
answered on 09 Jul 2011, 12:23 AM
Great, this is what I need.
Tags
General Discussions
Asked by
Hardy Wang
Top achievements
Rank 1
Answers by
John DeVight
Top achievements
Rank 1
Hardy Wang
Top achievements
Rank 1
Share this question
or