Ian (Fluxtah) Warwick's blog RSS 2.0
# Sunday, October 05, 2008

In an SnippetCallback, when defining mapping between a web service method parameter and a control, there is a client-side function for each control type that deals with extracting the value from the control and passing it as the web service methods parameter.

By default most of the simple web controls (TextBox, Checkbox, RadioButtonList, etc) that come out-of-the-box with ASP.NET already have a corresponding client-side value extractor function defined, but obviously this is not good enough if you want to use custom controls.

To override these or add new ones there are two things that need to be done, first off a new extractor function would have to be defined in configuration.

<configSections>
  <section name="ajaxSnippets" type="AjaxSnippets.Configuration.AjaxSnippetsConfigurationSection, AjaxSnippets"/>
</configSections>
<ajaxSnippets>
  <extractors>
    <add controlType="System.Web.UI.WebControls.TextBox" functionName="myCustomTextBoxExtractor" />
    <add controlType="My.Custom.Control" functionName="fromMyCustomControl" />
  </extractors>
</ajaxSnippets>

In this example, I am overriding the textbox extractor with my own function myCustomTextBoxExtractor, secondly I am defining a new value extractor fromMyCustomControl for My.Custom.Control.

Now the next thing that needs to be done is to write the client functions, this can be done by creating a new javascript file, which would need to be included in the page that Ajax Snippets are being used.

AjaxSnippets.ValueExtractors.prototype.myCustomTextBoxExtractor = function(id){
 var el = $get(id);
 // Extract value from el and return it here 
}
AjaxSnippets.ValueExtractors.prototype.fromMyCustomControl = function(id){
 var el = $get(id);
 // Extract value from el and return it here 
}

Ajax Snippets has a ValueExtractors class, and its simply just a job of adding functions to its prototype.

To give a better example, the ValueExtractors default prototype is below in full, in its current state from Ajax Snippets 0.3a release.

AjaxSnippets.ValueExtractors.prototype = {
    fromAny: function(id) {
        return id;
    },
    fromTextBox: function(id) {
        return $get(id).value;
    },
    fromDropDown: function(id) {
        var el = $get(id);
        return el.options[el.selectedIndex].value;
    },
    fromRadioList: function(id) {
        var i, els = $get(id).getElementsByTagName('input');
        for (i = 0; i < els.length; i++) {
            if (els[i].checked) { return els[i].value; }
        }
        return null;
    },
    fromCheckList: function(id) {
        var i, els = $get(id).getElementsByTagName('input'), vals = new Array();
        for (i = 0; i < els.length; i++) {
            vals.push(els[i].checked);
        }
        return vals;
    },
    fromListBox: function(id) {
        var i, el = $get(id), vals = new Array();
        for (i = 0; i < el.options.length; i++) {
            if (el.options[i].selected) { vals.push(el.options[i].value); }
        }
        return vals;
    },
    fromCheckBox: function(id) {
        return $get(id).checked;
    }
};
Sunday, October 05, 2008 5:19:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
ASP.NET | General

After a busy weekend I finally got a wizard working to help with the configuration of a SnippetCallback. In the latest build of Ajax Snippets there is now an option from the action list of a SnippetCallback on the designer named 'Configure...'.

After clicking this option a wizard will popup where an asmx file can be selected and a web method from this service.

One problem with this is that the web application must be compiled before the wizard can get access to the web service method list, the wizard basically pulls out web service type defined by the class attribute in the asmx file and tries to use Type.GetType() on it, if the web application is not compiled with asmx file then the methods do not show up in the wizards list, but I did stick in a red warning label under the drop-down-list if this happens.

The next step allows the mapping of controls to the selected web methods parameters.

The main benefit of the wizard is it avoids any mapping mistakes and typos, it could also do with a page to configure other options of a SnippetCallback but at least these two steps cover the main options.

 

Sunday, October 05, 2008 4:41:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
General
# Saturday, October 04, 2008

All last night till 2am and when I could find the time today I have been having some great fun with developing a configuration wizard for a SnippetCallback control that is part of my Ajax Snippets project.

I wanted the user to select a webservice and then select a method from that service, but there did not seem to be an easy way to get at the web projects files during design-time, at least thats what I initially thought.

It turns out that there is an IWebApplication interface that can be got at by using the GetService method of a component sited in design mode, this then allows you to get at a project item. In my case I needed to get at the asmx file and pull out the type name of its associated code-behind file so I could get a list of methods.

This is a very simple example of how to get at project files physical path on disk from a control designer

[code:c#]
    public class MyDesigner : ControlDesigner
    {
        public void DoSomethingWithProjectItemExample()
        {
            IWebApplication app = (IWebApplication)Component.Site.GetService(typeof(IWebApplication));
            IProjectItem item = app.GetProjectItemFromUrl("~/Default.aspx");
            string physicalPath = item.PhysicalPath;
            // do something with file
        }
    }
[/code]

 

Saturday, October 04, 2008 4:02:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -

# Friday, October 03, 2008

After creating a rough guide to Ajax Snippets video it brought a few things to light, one thing was the awkwardness of having to define callback functions in a SnippetCallback and then having to write them out in the page that hosts the snippet.

I added a feature that allows the developer to select an option from the SnippetCallback controls action list that will copy some javascript function stubs into the clipboard based on the controls settings.

 

The relay snippets example will copy the following into the clipboard

[code:js]
function beforeRelayFields(){
}
function relayFieldsSuccess(result){
}
function relayFieldsFailed(error){
}
[/code]

This may have been an overkill feature but still its good for forgetful people like me that could do with some extra help.

Friday, October 03, 2008 4:00:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -

Whilst working on Ajax Snippets I wanted to provide an option for the user in a DesignerActionList to copy some javascript function stubs to the clipboard, at first I was stumped, I have worked with clipboard data before but I could not remember how to do it let alone the environment was giving me doubts to wether the answer was the same as working with winforms.

Like winforms you can get at the clipboard via the not suprisingly named Clipboard class.

 

[code:c#]
    public class MyDesigner : ControlDesigner
    { 
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection actionLists = new DesignerActionListCollection();
                actionLists.AddRange(base.ActionLists);
               
                actionLists.Add(new MyDesignerActionList(this));
                return actionLists;
            }
        }
    }

    public class MyDesignerActionList : DesignerActionList
    {
        MyDesigner _Designer;
        DesignerActionItemCollection _Items;
        public MyDesignerActionList(MyDesigner designer) : base(designer.Component)
        {
            _Designer = designer;
        }

        public void SetClipboard()
        {
            Clipboard.SetDataObject("Hello World");
        }
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            if (_Items == null)
            {
                _Items = new DesignerActionItemCollection();

                _Items.Add(new DesignerActionMethodItem(this, "SetClipboard", "Set clipboard"));
            }

            return _Items;
        }
    }
[/code]

Friday, October 03, 2008 3:10:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -

Archive
<October 2008>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Ian Warwick
Sign In
Statistics
Total Posts: 33
This Year: 0
This Month: 0
This Week: 0
Comments: 4
Themes
Pick a theme:
All Content © 2012, Ian Warwick
DasBlog theme 'Business' created by Christoph De Baene (delarou)