Showing posts with label Dynamic. Show all posts
Showing posts with label Dynamic. Show all posts

Wednesday, May 18, 2011

Dynamic Tabs using AJAX tab container and add controls and read them dynamically?


In this article we will see that how to create dynamic tabs.
For making dynamic tabs I will use Ajax Tab Container controls. I will create tabs dynamically, will add controls to each of the tabs dynamically and after that we will read the value of each control on some button click.
Step 1: Open visual studio and create a web site.
image
Step 2: Add the refrence to AjaxToolkit.

image
Step 3: Declare Globally TabContainer.
image
Step 4: Next is we need to create tabs dynamically. We can write code for making Dynamic Tabs in Page_load event also but the problem of writing this code in Pageload event is: If we are creating dynamic tabs for a Sharepoint custom Webpart than it will throw error on postback events. But it will work for normal ASPX pages. For this reason I will write it in Page_Init function.
image
image
Step 5: After creating dynamic tabs we will add dynamic controls to it. Here I will create one asp table and then add the controls to it and after adding controls I will add this dynamically generated table to the tabs of the Tab Container control.
The reason why I am adding a table is just for formatting. You can directly add controls to the panel.
image
Step 6: Here I have added one button and created one click event on it. This button click event I will use for reading the dynamically generated controls

image
Here I used one placeholder PC1 also. If we are creating Dynamic tabs for ASP application than no problem but if we are building it for Sharepoint than it is very helpful. If we are not using it than sometimes postback triggers are showing some ajax javascript error.
Now press F5 and see the result.
image
image
Hope this article will be a help to you!
Cheers,
Ravish

Dynamic Type (New Feature in C# 4.0)

Dynamic Type is a part of DLR (Dynamic Language Runtime). This DLR is nothing but some added feature to common language runtime.
For using DLR features we need to add System.Dynamic namespace.

Dynamic type is the feature of C# 4.0. It is a powerful feature of 4.0 framework which allows us to write code in such a way that we can bypass compile time checking. Here bypass doesn’t mean that we can remove the compile time errors, it means if the operation is not valid than we cannot detect the error at compile time. This error will appear only in run time.
Difference between var keyword and dynamic keyword:
The objects defined with dynamic keyword can change its type at runtime. But it is not possible with the var keyword. When we use var keyword it means the determination of the type is delayed but it will happen only in compile time. It means once the type has been specified than we cannot change the type of the object declared using var keyword.
And in the case of dynamic we can change its type in run time and as many times as required.
In the following example we will see how we can use dynamic objects:
static void Main(string[] args)
        {
            dynamic dynamicVariable;
            var oMyClass = new MyClass();
            dynamicVariable = oMyClass.DisplayGreeting();
            Console.WriteLine(dynamicVariable+ "\n\nand the type of dynamic variable is: "+ dynamicVariable.GetType());

            dynamicVariable = oMyClass.DisplayInteger();
            Console.WriteLine("\n" + dynamicVariable + "\n\nand the type of dynamic variable is: " + dynamicVariable.GetType());
            Console.Read();
        }

        class MyClass
        {
            private string sName = "Jack";
            private string sGreeting = "Good Morning";
            public string DisplayGreeting()
            {
                return (sGreeting + sName);
            }
            public int DisplayInteger()
            {
                return 5;
            }
        }

Here in this example we have MyClass containing two functions ‘DisplayGreeting’ and ‘DisplayInteger’.  Both functions are returing different values.
In our main function we have declared a dynamic variable ‘dynamicVariable’.
dynamicVariable = oMyClass.DisplayGreeting();
            Console.WriteLine(dynamicVariable+ "\n\nand the type of dynamic variable is: "+ dynamicVariable.GetType());

Here in the above line we are assigning string value to it and printing it in console, value as well as its type.
In the below mentioned line line we are assigning string value to it and printing it in console, value as well as its type.
            dynamicVariable = oMyClass.DisplayInteger();
            Console.WriteLine("\n" + dynamicVariable + "\n\nand the type of dynamic variable is: " + dynamicVariable.GetType());


When we are compiling it than we are getting the output as:
clip_image002
This is all about Dynamic type.

Thank You.
Ravish