Exploring the depths and potentials of ASP.NET RSS 2.0 or Subscribe to .BenRush by Email
 Tuesday, June 19, 2007

What if you had a web service method like this:

[WebMethod]
public List<String> Test() {
List<String> blah = new List<string>();
blah.Add("First");
blah.Add("Second");

return blah;
}

The method returns a generic List<> type of String objects. What happens if you want to consume it from AJAX via a web service method call like this?

function callWebService()
{
WebService.Test(OnMethodSucceeded,OnMethodFailed);
return;
}

The result will get sent back to the browser as an array type in JavaScript. Doing an alert() call on the result returned from OnMethodSucceeded like this:

function OnMethodSucceeded(result, eventArgs)
{
alert(result);
}

Yields the string "First,Second". You may also index into the array too and get the individual strings.

So, what about parameters? ECMA script doesn't support generics in the same way .NET does, but given that we've identified the runtime returns the generic list as an array back to ECMA script, it seems logical that we could pass a "generic list" to the runtime via ECMA script as an array. Indeed, we can. If we change the web service method to something like this:

[WebMethod]
public String Test(List<String> arg) {
if (arg.Count > 0)
return arg[0];
else
return "NA";
}


We can call it from script like this:

function callWebService()
{
var arr= new Array(5)
arr[0]="1";
arr[1]="2";
arr[2]="3";
arr[3]="4";
arr[4]="5";
WebService.Test(arr,OnMethodSucceeded,OnMethodFailed);
return;
}

The web service method returns, as a string, the first item in the array. OnMethodSucceeded, with our alert() method, will display "1" returned to the client.


kick it on DotNetKicks.com
Tuesday, June 19, 2007 9:10:20 AM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
AJAX | ASP.Net | JavaScript
 Sunday, April 29, 2007

My article has been written for MSDN now and so I'm back to blogging. The article wasn't done quite as well as I would have hoped because I've got about three or four other things going on at the same time right now and so focus has been thin; but it was an article and we'll see if it makes its way through the submission process.

Apart from that I have decided that I'm going to blog on the ASP.NET AJAX control toolkit for the next month or two, exploring each of the control's from the inside out and teaching people how to use them. Hopefully I can even contribute some of the things I write to the community project itself. Regardless, my hope is that we can all explore the internals of the AJAX controls a bit and begin to leverage them more productively.

I'll continue posting answers to people's questions too.

I'm going to go alphabetically through the control toolit, so starting tomorrow we'll begin an exploration of the Accordion control and I'll try to spend, at least, a couple days (maybe even a week) on each control.

Cheers.


kick it on DotNetKicks.com
Sunday, April 29, 2007 2:38:34 PM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback

 Wednesday, April 18, 2007

I have been busy :)

Currently I'm writing an article for MSDN detailing the internals of the ScriptManager; and so between that and work I have had next to no spare time to really do anything else. When the article if done then I'll have plenty of time for exploring ASP.net, AJAX, .Net and the like...but for now, I gotta get this article done!

Thanks....

(about another couple days, probably)


kick it on DotNetKicks.com
Wednesday, April 18, 2007 9:01:16 AM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback

 Thursday, April 12, 2007

I quickly through this sample together because someone in the newsgroups wanted a way to create a filesystem view through the ASP.Net TreeView control. Note: I'm not touching this with icons or any other niceness, that's an exercise left to the reader:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<script runat="server" language="C#">
    protected void Page_Load(object sender, EventArgs e)
    {
        System.IO.DirectoryInfo di =
            new System.IO.DirectoryInfo(@"c:\mydirectory\mydirectoryschild\");
        TreeNode root = new TreeNode(di.Name);
        this.TreeView1.Nodes.Add(root);
        HydrateTree(di, root);
    }

    private void HydrateTree(System.IO.DirectoryInfo dir, TreeNode parent)
    {
        foreach (System.IO.FileInfo file in dir.GetFiles())
        {
            parent.ChildNodes.Add(new TreeNode(file.Name));
        }
        foreach (System.IO.DirectoryInfo di in dir.GetDirectories())
        {
            TreeNode child = new TreeNode(di.Name);
            parent.ChildNodes.Add(child);
            HydrateTree(di, child);
        }
        return;
    }
</script>
<asp:TreeView ID="TreeView1" runat="server">
</asp:TreeView>

This is what it looks like on my machine:



Recommended reading:


kick it on DotNetKicks.com
Thursday, April 12, 2007 7:37:08 PM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
ASP.Net | Programming
 Tuesday, April 10, 2007

Kind of an interesting mental exercise. Say you wanted to enumerate all the classes in any particular namespace. Unfortunately there doesn't appear to be a way to say, "Get all classes in a namespace" using reflection, so you kind of have to roll your own implementation. But, this is how I did it....

namespace TestingGroundsCSharpConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly a = Assembly.GetExecutingAssembly();
            List<String> namespaces = new List<string>();
            foreach (Type t in a.GetTypes())
            {
                if (t.Namespace == "TestingGroundsCSharpConsole")
                    namespaces.Add(t.Name);
            }

            foreach (String s in namespaces)
                Console.WriteLine(s);

            Console.ReadLine();

            return;
        }
    }

    class Class1
    {
        private Int32 _myInt;
    }

    class Class2
    {
        private Int32 _myotherInt;
    }
}

....the output is

Program
Class1
Class2



kick it on DotNetKicks.com
Tuesday, April 10, 2007 10:07:02 PM (Central Standard Time, UTC-06:00)  #    Comments [1] - Trackback
Programming | .Net Runtime

You more than likely get this exception when you attempt to use the "My.Settings" object through ASP.Net. Settings scoped for the user are valid for WinForms or other such desktop applications which typically run under a particular user account, but ASP.Net accounts run, usually, under a special server or ASP.Net account - therefore this object will throw an exception when used in this context.

The best way around this is to use ConfigurationManager.AppSettings for settings, or Profiles for per-user configuration in ASP.Net 2.0.


kick it on DotNetKicks.com
Tuesday, April 10, 2007 6:57:55 PM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
ASP.Net | Programming

I saw a question in the newsgroups about this and I thought I would post about it right quick; "how do you manually set focus with javascript". Using .Net it's easy, you simply call .Focus() on a server control:

this.LinkButton1.Focus();

But if you have to do it from script within a page itself, you do it this way:

<html id="total" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body id="Body" onload="document.form1.Text2.focus();">
    <form id="form1" runat="server" style="width:1000px;height:1000px;" >
        <div  >
           <input id="Text1" type="text" /><input id="Text2" type="text"/>
        </div>
    </form>
</body>
</html>

Notice the onload event within the body. This will effectively focus the cursor within Text2. Obviously you don't have to limit it to the body onload event, you can do it from any event or any script; I just did it there from my example.

Easy as cake.


kick it on DotNetKicks.com
Tuesday, April 10, 2007 6:46:23 PM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
ASP.Net | JavaScript | Programming
 Sunday, April 08, 2007

Thanks to Sebastian and his source here for my direction on this topic. Cheers man; I owe you Guinness.



There seems to be a severe lack of writing on the internet about how to do just this very thing - slowly blending one image to another using DirectX. I'm not an expert at DirectX, I spend most of my time these days doing web-based development, but I am also a software consultant and so get picked up for different types of jobs from time to time, and obviously it makes no sense to turn a job down....



....well, I wanted to implement a system where one texture slowly blended into another texture; kind of like a slow fade effect. As it turns out it's pretty trivial, but again not something I was very good at....



To do this correctly, you really want to do it independent of the image format, so that whether you're using something that has an alpha layer in it or not is irrelevant (like, a JPG vs. a PNG). To do this, you fill in or interpolate the alpha value of an image acrossed it using the vertices in the vertex buffer. So - you need to make sure that your vertices have an alpha color component. Likewise, to change the degree by which something blends, you change the alpha components of each vertex in the buffer. This actually gives you great control over the blending because you can choose even more advanced scenarios than simply blending from one image to another - you can also choose where to start the blending (from a particular corner to another, etc.).

So, on to the code....in the drawing loop for the application (if you're implementing your DirectX application correctly, this should be the Paint method):

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            _device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
            _device.BeginScene();

            if (_zoomIn)
            {
                _alphaValue++;

                if (_alphaValue == 255)
                    _zoomIn = false;
            }
            else
            {
                _alphaValue--;
                if (_alphaValue == 0)
                    _zoomIn = true;
            }
            _verts[0] = new CustomVertex.PositionColoredTextured(-1.0F, 1.0F, 0.0F, Color.FromArgb(_alphaValue, Color.White).ToArgb(), 0.0F, 0.0F);
            _verts[1] = new CustomVertex.PositionColoredTextured(1.0F, 1.0F, 0.0F, Color.FromArgb(_alphaValue, Color.White).ToArgb(), 1.0F, 0.0F);
            _verts[2] = new CustomVertex.PositionColoredTextured(-1.0F, -1.0F, 0.0F, Color.FromArgb(_alphaValue, Color.White).ToArgb(), 0.0F, 1.0F);
            _verts[3] = new CustomVertex.PositionColoredTextured(1.0F, -1.0F, 0.0F, Color.FromArgb(_alphaValue, Color.White).ToArgb(), 1.0F, 1.0F);
            _verts[4] = new CustomVertex.PositionColoredTextured(-1.0F, -1.0F, 0.0F, Color.FromArgb(_alphaValue, Color.White).ToArgb(), 0.0F, 0.0F);
            _verts[5] = new CustomVertex.PositionColoredTextured(1.0F, -1.0F, 0.0F, Color.FromArgb(_alphaValue, Color.White).ToArgb(), 1.0F, 0.0F);

            _buffer.SetData(_verts, 0, LockFlags.None);

            _device.VertexFormat = CustomVertex.PositionColoredTextured.Format;
            _device.SetStreamSource(0, _buffer, 0);

            _device.SetTexture(0, _texture);
            _device.SetTexture(1, _texture2);

            _device.TextureState[1].TextureCoordinateIndex = 0;

            _device.SamplerState[1].AddressU = TextureAddress.Wrap;
            _device.SamplerState[1].AddressV = TextureAddress.Wrap;


            _device.SetTextureStageState(0, TextureStageStates.ColorArgument1, (int)TextureArgument.TextureColor);
            _device.SetTextureStageState(0, TextureStageStates.ColorOperation, (int)TextureOperation.SelectArg1);
            _device.SetTextureStageState(1, TextureStageStates.ColorArgument1, (int)TextureArgument.Current);
            _device.SetTextureStageState(1, TextureStageStates.ColorArgument2, (int)TextureArgument.TextureColor);
            _device.SetTextureStageState(1, TextureStageStates.ColorOperation, (int)TextureOperation.BlendDiffuseAlpha);

            _device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);

            _device.EndScene();
            _device.Present();

            Invalidate();
            return;
        }

The effect is two textures that slowly blend back and forth to each other.


Recommended Reading

kick it on DotNetKicks.com
Sunday, April 08, 2007 2:07:54 PM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
DirectX and XNA | Programming

Computers Blogs - Blog Top Sites

Archive
<June 2007>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567
Blogroll
About the author/Disclaimer

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

© Copyright 2009
Benjamin Rush
Sign In
Statistics
Total Posts: 444
This Year: 0
This Month: 0
This Week: 0
Comments: 128
Themes
Pick a theme:
All Content © 2009, Benjamin Rush
DasBlog theme 'Business' created by Christoph De Baene (delarou)