Thursday, August 26, 2010

Manipulate Dynamic DOM elements in JavaScript with JQuery

JQuery have a huge set of selectors to manipulate DOM elements. But when it comes to manipulate dynamically generated elements it needs some logic to access all of them and perform operation.

Ex. Suppose you want to check all checkboxes inside GridView OR any DataBound control, you need to loop over them and perform operation.

To loop over them you need to access them by ID or by class, now because IDs are dynamically generated you can’t access them directly.  You can have two solutions in this scenario.

 

Case-1: You can give unique class to them and find them by class name.

 

Suppose you have a Grid having textboxes in one column. You want to change text of all of them to

‘Deleted’, when user check the checkbox in  Header.

   <div id="dvProducts">

        <asp:GridView ID="grvProducts" runat="server">

            <Columns>

                <asp:TemplateField>

                    <HeaderTemplate>

<asp:CheckBox ID="chkAllProduct" runat="server" onclick="chkSetStatus_Click(this.checked)" />

                    </HeaderTemplate>

                    <ItemTemplate>

<asp:TextBox ID="txtStatus" runat="server" CssClass="dummyForSelect"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>

    </div>

Here is the function which will perform this operation.

function chkSetStatus_Click(ischecked) {

            $('.dummyForSelect', $("#dvProducts")).each(

                                        function(d, obj) {

                                        if(ischecked)

                                            obj.value = 'Deleted';

                                            else

                                                obj.value = '';

                                        });

        }

 

Case-2: You can use pattern of generated IDs and find them using regular expression.

 

Suppose you have a Grid having Checkboxes in one column. You won’t to check all when checkbox in the header checked, and remove selection when user unselects it.

 

  <div id="dvProducts">

        <asp:GridView ID="grvProducts" runat="server">

            <Columns>

                <asp:TemplateField>

                    <HeaderTemplate>

         <asp:CheckBox ID="chkAllProduct" runat="server" onclick="chkAllProduct_Click(this.checked)" />

                    </HeaderTemplate>

                    <ItemTemplate>

                        <asp:CheckBox ID="chkProduct" runat="server"  />

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>

    </div>

You can’t use CssClass here because it will not work for you, you have to use pattern in which ID of checkboxes is generated.

ID generated by asp.net will be “grvProducts_ctl03_chkProduct”, we need to make RegEx to find out checkboxes with such pattern and make operation on them.

So our RegEx will look like this var rExp = /.*grvProducts.*chkProduct/;

Using this we can write JavaScript function below to finish task.

function chkAllProduct_Click(ischecked) {

            var rExp = /.*grvProducts.*chkProduct/;

            $('input:checkbox', $("#dvProducts")).each(

                                        function(d, obj) {

                                            if (rExp.test(obj.id)) {

                                                obj.checked = ischecked;

                                            }

                                        });

        }

 

This is the tasks becomes too easy because of the JQUERY. Please put your suggestion here if any.

 

Hope this will help you.

 

Thanks,

Haresh Dhameliya

Performance consideration using collection .Net framework

 

.Net framework is loaded with good amount of collection to make programming as easier, when we are developing small application it is ok if we don’t consider which

Programming practice is batter then other. But if we are working on any application having enterprise customers we must have to think about which programming element is batter over other. Here I have tried to make some flash over this.

 

1) Array:

·         Use strongly typed array if you don’t won’t much operations like searching and sorting.

·         Strongly Arrays gives best performance because they avoids boxing and unboxing.

2) Iteration:

·         Index based iteration will be efficient then enumerating (for each). Use loops for very big size collections.

3) Collections:

·         Prefer Array over Collection for storing Value types, this will avoid boxing and unboxing.

·         Use strongly typed collection, Use StringCollection for storing string instead of ArrayList.

·         Use binary search method while searching large size ArrayList.

·         Store small amount of data in ListDictionary instead of Hashtable.

·         Use StringDictionary for storing strings.

·         Avoid using SortedList if you are performing add/update operation frequently, use ArrayList instead.

·         If you are Using ArrayList declare it with approximate possible length, this will save memory, remember that doing this you will not lost variable length feature of ArrayList.(e.g. Use ArrayList arrList = new ArrayList(4);)

 

Any inputs will always be accepted.

 

Thanks,

Haresh Dhameliya.

Monday, August 16, 2010

How To: InternalsVisibleTo Example

You must have read my previous post on Difference Between PublicKey and PublicKeyToken. Now continue to that, where we need PublicKey?

Here I am giving one example to use PublicKey

We all know about “internal” keyword for access permission. Generally we are using internal keyword to restrict access for class, method or property from outside of assembly.

But there are certain case where we need to set some class or method accessible to only selected assemblies.

- In maintenance mode we are introducing some new assemblies

- We are providing add-ons on request.

To achieve this .Net framework provides, assembly attribute InternalsVisibleTo.

Below line should be added in AssemblyInfo.cs file of your project.

1 [assembly: InternalsVisibleTo("Your.Assembly.Name")]

Now in case of you have signed your assembly with key, it is mandatory for you to specify your public key in the attribute.

Syntax with PublicKey will be

1 [assembly: InternalsVisibleTo("Your.Assembly.Name, PublicKey=............Your.Public.Key............")]

Below are steps to generate your public key

1. Generate a strong name key for this example and store it in the file MySignedProject.snk:

sn -k MySignedProject.snk

2. Extract the public key from MySignedProject.snk and put it into MySignedProject.publickey:

sn -p MySignedProject.snk MySignedProject.publickey

3. Display the public key stored in the file MySignedProject.publickey:

sn -tp MySignedProject.publickey

Hope this will help you to provide guidance with multiple assembly intercommunication security.

 

Nirav