Monday, February 17, 2014

Creating an Extender Control to Associate a Client Behavior with a Web Server Control, without a Class library project (aka in Visual Studio Express 2010)

I am referring to the article
http://msdn.microsoft.com/en-us/library/bb386403(v=vs.90).aspx

where one can make Extender Controls, but i figured out that as i have an express edition i can't do practice of control extenders. But i did wanted to find out a way by which this can be achieved even without making a separate project. The only option i had was asp.net's code folder.

You need to follow every step mentioned there..plus the sections mentioned below.. after this you will be stuck. as your project wont run. You will get Assembly Samples not found error. Here my article would be useful for you.

1) Download the sample from microsoft page.
http://go.microsoft.com/fwlink/?LinkId=185643

Refer sections Dynamically Compiling the Extender Control for Testing & Testing the Dynamically Compiled Extender Control in a Web Page

Now here, there are some more steps which have not been mentioned which i am mentioning here. The downloaded code will be useful as you dont have to type all the stuff again, but if someone wants to do it, he can surely do it. And when he is done, he can check the accuracy of his writing by the website http://www.diffnow.com/

Now follow these steps....

1) Make your register control declaration like
<%@ Register Namespace="Samples.CS" TagPrefix="sample" %> remove the word assembly.
If you dont remove it will expect an assembly and will give the error Assembly . As you want to develop without using dependencies and would be using only the App_Code then its better to remove the words 'Assembly="Samples"'

2) Modify your script manager tag, by adding reference to the javascript file FocusBehavior.js
Your script manager tag on your aspx should look like this...
<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/Scripts/FocusBehavior.js" />
    </Scripts>
    </asp:ScriptManager>

If you dont do, this you will get the Error Type is Not defined. and javascript on your page wont work.
3) You can put all the styles mention in the page to the styles.css and refer the css file in your aspx page, but that step is optional.
4) Go to App code, and from your downloaded code, add the FocusExtender.cs there.
5) Go to FocusExtender.cs and change this method on line 34.
        protected override IEnumerable<ScriptReference> GetScriptReferences()
        {
            ScriptReference reference = new ScriptReference();
            reference.Path = ResolveClientUrl("FocusBehavior.js");
            //reference.Assembly = "Samples";
            //reference.Name = "Samples.FocusBehavior.js";

            return new ScriptReference[] { reference };
        }

Note that as we are not using an Assembly and we are using App_Code folder, we wont be needing the two lines. Also, as our js will not be a compiled resource we are not using reference.Name property. It is also worthy to comment the first line(as it was in downloaded code) and uncomment remaining lines to see what errors it produce. Its' kinda fun and educative. After you changed this method.

7) Now you run the project. You will be having the same Extender Control without a need for a Class library project all of the things needed were App_Code and Scripts.

Important thing to note that getting all these extender control functionality in the class library and making the resource as a compiled resource does make more sense, this solution is only for those guys who are in project where App_Code is the order of the day and they still needed the functionality of an extender control.

Thanks.




No comments: