Exploring the depths and potentials of ASP.NET RSS 2.0 or Subscribe to .BenRush by Email
 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
 Thursday, April 05, 2007

ASP.Net doesn't like multiple forms on a webform; indeed, it is the paradigm now that one webform has one "form" on it (hence the name - webform). However, as people begin buliding tricky input forms on their pages it becomes necessary to specify "default" buttons throughout the page. As an example, say you're filling out one of two forms on a page and you want to hit the Enter key to submit your form data. If there are multiple forms on the page, then you will run into a problem as the browser attempts (and possibly fails) to ascertain which button should recieve the keydown event.

In ASP.Net the Panel control supports a default button so that you can "subsection" out your form with various different buttons being the default. If focus happens to be on a control within a panel that has one of these secondary submit buttons specified, then THAT button will get respond to your Enter keystroke.

Here's an example:

First, create a webform with two panels on it like this:



When you run the above form, regardless of whether your cursor is in the textbox of the first form or the second form, hitting ENTER will always act as if you clicked the button in the first.

To submit the button in the second via the ENTER key if your cursor is active in that textbox (or any control in that panel), do the following to the panels:

            First form:
            <asp:Panel ID="Panel1" DefaultButton="Button1" runat="server" Height="127px" Width="151px">
                &nbsp;
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Button" /></asp:Panel>
            <br />
            Second form<br />
            <asp:Panel ID="Panel2" DefaultButton="Button2" runat="server" Height="127px" Width="151px">
                &nbsp;
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /></asp:Panel>
            </div>

Notice how I specify the "DefaultButton" property in the panel. Now if your cursor is active in any control within Panel2, Button2 will be the default submit button for your ENTER keystroke.

Addendum:

Please see here for the documentation to this attribute. It is available in the .Net frameworks 2.0 and 3.0 only.

Recommended reading:


kick it on DotNetKicks.com
Thursday, April 05, 2007 9:04:17 PM (Central Standard Time, UTC-06:00)  #    Comments [3] - Trackback
ASP.Net | Programming

This exercise requires no C# or VB.Net code at all (unless you want to start getting fancy), and is relatively well documented on the internet. However, I like to recap things quickly through these little tutorials, so hopefully this might summarize how to quickly get up and running with parent<->child window script communication.

First, create two web forms (they don't need to be, but for our exercise they will be). We'll call the Default.aspx and Default2.aspx.

Second, open up Default.aspx and add the following script functionality:

        <input id="Button1" type="button" value="button" onclick="window.open('default2.aspx');" />
        <input id="Text_Value" type="text" />

...here you see that you open up default2.aspx when you click on the button.

Third, go to your Default2.aspx file and add the following HTML:

        Please Enter Some Data:&nbsp;
        <input id="Text1" type="text" />
        <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" /></div>

Fourth...add the following script:

function Button1_onclick() {
    opener.document.getElementById('Text_Value').innerText =
        document.getElementById('Text1').value;
    self.close();
   
    return;
}

The result will be opening up the default2.aspx form from default.aspx, you can enter text into the Text1 field (see above), click the Button1 which calls the Button1_onclick method. This method communicates with the parent window, sets a text box in that window's document and closes itself.

This requries no .Net code to accomplish; this can all be done through JavaScript.


Recommended reading:


kick it on DotNetKicks.com
Thursday, April 05, 2007 12:32:07 AM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
ASP.Net | JavaScript | Programming
 Wednesday, April 04, 2007

Say you want to use the browser component to automate some form of web browser experience - for example, say you want to programmatically enter some value into a textbox and click a button using C# or VB.Net. To do this is actually quite simple (and, dare we say it, fun).

First, you simply add your browser component to your windows form.

Second, you register an event handler with the browser component for the DocumentCompleted event:

            this.webBrowser1.Url = new Uri("http://www.google.com");
            this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

Third, you fill in the event handler with something that looks like this:

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (!_clicked)
            {
                _clicked = true;

                HtmlElement queryBox = this.webBrowser1.Document.All["q"];
                queryBox.SetAttribute("value", "hello world");

                HtmlElement goButton = this.webBrowser1.Document.All["btnG"];
                goButton.InvokeMember("click");
            }

            return;
        }

This will load the www.google.com homepage, fill in their submit text box and click the Search button.

In the golden days of MSHTML component reuse (before the niceness of .Net'ifying the browser DOM) you had to use a RCW (Runtime Callable Wrapper) around the MSHTML COM library. Everything is obviously implemented through IDispatch here (because you have to be able to do this stuff from VB6 or scripting environments), and so much of the invocation of methods (like click) was done by loosly picking the name for the method and building the stack frame through arrays, etc. The RCW made things a bit nicer for you (you didn't have to think about IDispatch), but you still had to think about COM.

It's been years since I've developed in COM and ATL and I'm glad I don't have to anymore.

Recommended reading:


kick it on DotNetKicks.com
Wednesday, April 04, 2007 10:55:24 AM (Central Standard Time, UTC-06:00)  #    Comments [0] - Trackback
Programming | WinForms and WPF

Say you have a master page and content page scenario, and from the content page you want to interact with DOM elements described from the master page (for example, the main body element). This is how I do it:

First, add the following attributes to your body element so that it is included in the server-side runtime and can be interacted with from there:

<body id="mainbody" runat="Server">

...notice how the body element now has an id and has the runat="server" attribute set.

Second, from code within the content page (I'm doing it in Page_Load) add code like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlGenericControl ctrl = (HtmlGenericControl)this.Master.FindControl("mainbody");
        ctrl.Attributes.Add("onload", "alert('hello');");
        return;
    }

The key here is to now grab the body element, add some script to it, and then let it all run as normal. When I run the solution, the content page is loaded, its Page_Load called, the script injected into the body element (of the master page) and I see "hello".

Recommended reading:

kick it on DotNetKicks.com
Tuesday, April 03, 2007 11:47:56 PM (Central Standard Time, UTC-06:00)  #    Comments [2] - Trackback
AJAX | ASP.Net | Computing

Computers Blogs - Blog Top Sites

Archive
<April 2007>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
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 2008
Benjamin Rush
Sign In
Statistics
Total Posts: 444
This Year: 0
This Month: 0
This Week: 0
Comments: 127
Themes
Pick a theme:
All Content © 2008, Benjamin Rush
DasBlog theme 'Business' created by Christoph De Baene (delarou)