Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
This is going to be a bit lengthy:( Kindly read it to the end.

I have a page to add products. When the user adds product, he/she is asked to add the specifications . I have all the attributes for a particular category say `Mobiles` like Camera,OS,Type etc etc. All the attributes are divided into groups(See Table Below). If the user is uploading a product in `Mobiles` category , I want to select all the groups ith the category and then the attributes in the Group and display it on the UI. Kindly go through the steps to understand the scenario.

Table (CategoryGroups)

http://i.stack.imgur.com/D20o3.png[^]


Step 1 : Select all the groups from a particular `NodeID`

Table (GroupAttributes) :

http://i.stack.imgur.com/qNHFW.png[^]

Step 2 : Display all the Attributes to the user Grouped By `GroupName` i.e `AttributeID` 1 & 2 should come under `Group 1` , `AttributeID` 3 & 4 should come under `Group 2`

Step 3 : For each row check if `canSelectMultiple` is `True` or `False` .

Step 4 : If `canSelectMultiple` is `False` Add `DropDownList` to the page and add each Value in `Value` column to the `DropDownList`[**Value column has semi colon separated values**] i.e for the 1st row Add `DropDownList` with Values "Android","Bada","Blackberry",Symbian,"iOS"

ElseIf `canSelectMultiple` is `True` the Add `CheckBoxes` to the page with one `CheckBox` for every value in `Value` Column.

Step 5 : Save the values selected by the user in DB on Button Click.


This is the way i intend to do it (using loops i.e FOR NEXT). Any other way to do it?? Can i do it in client end using `jQuery/Javascript`. I personally do not want to use these many loops because it would be too costly. Any help appretiated.
Posted

Loops are really basic tools in programming. You can not avoid them. Why do you think they are "costly"? Actually you can make a code consume too many resources, but that would not be just because you use loops. Can you imagine how many loops are in jquery for example, or in the code of asp.net itself?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Oct-12 15:30pm    
You are right, of course, my 5, but how to measure this kind of cost and manage it? Difficult question.
I provided some introductory material, good to get started -- please see my answer.
--SA
Member 7781963 1-Oct-12 15:36pm    
Thanks a lot for the articles. I always thought that loops take a lot of time and therefore degrade the applications performance. Anyway, how do i place controls dynamically on the page. How do i keep a list of all the controls added to the page, because i will have to fetch the values from the controls added when the user clicks on Save Button. Kindly point me in the right direction.
Zoltán Zörgő 1-Oct-12 15:50pm    
I would be careful with semicolon separated lists. You will have to parse these every time you generate the page, and when you validate the answer. I would simply add a new table with the possible attributes and a foreign key to the attribute table. Actually might not be a really huge performance issue in this case, but it looks an unnecessary violation of 3NF. You don't fetch the data from the controls. They are sent by the client, you just have to get them from the POST data, and of course, validate them before you store anything in the database. So you don't need to keep a list of the controls, but you should know on what name you will get the answer. This is an other argument for using an other table: you could use the primary key of that table to generate the control names and option values. And so you have a quite simple link between the possible values and the controls.
Not the number of loops measures the cost for the performance, but many other factors, and they usually make those "multiple loops" concerns nearly insignificant.

This is something you need to understand first:
http://en.wikipedia.org/wiki/Computational_complexity_theory[^],
http://en.wikipedia.org/wiki/Time_complexity[^].

Now, the measurement and notation. Even though the notation does not have the fundamental role compared to the role of the theory itself, you won't be able to understand many statements and issues if you don't understand the notation. This is the one which is nearly always referred to:
http://en.wikipedia.org/wiki/Big_O_notation[^].

It's also good to understand a number of similar or related notations mentioned in the same article:
http://en.wikipedia.org/wiki/Small_o_notation#Related_asymptotic_notations[^]

Good luck,
—SA
 
Share this answer
 
v2
Comments
Member 7781963 1-Oct-12 15:35pm    
Thanks a lot for the articles. I always thought that loops take a lot of time and therefore degrade the applications performance. Anyway, how do i place controls dynamically on the page. How do i keep a list of all the controls added to the page, because i will have to fetch the values from the controls added when the user clicks on Save Button. Kindly point me in the right direction.
Sergey Alexandrovich Kryukov 1-Oct-12 15:46pm    
You are not along. This is one of the typical misjudgements of the novice. If you really have many controls, placing them "dynamically" (do you know, that there is no such thing as placing "not dynamically" or "statically"?), this is a right choice. You just should not use the Designer for that; use something like Control.Controls.Add.

Basically, keep references to all controls added in some array (or some collection from System.Collections.Generic) and access by iterating them in a loop. You won't need the names for the controls; all the references would be in an array/collection.

--SA
Member 7781963 1-Oct-12 16:34pm    
I am going through this article http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx

Is it worth it??
Sergey Alexandrovich Kryukov 1-Oct-12 17:28pm    
Hard to say for sure from the first glance, but at least basic techniques are covered and the code should work. This should be enough for your to get the ideas.
The motivation is no sufficient, I would say. This is not only about extra flexibility. This is more the maintenance.

Imagine you really need an array of controls. Adding them with the Designer is tedious manual work, in contrast to programming where you never repeat anything, but use the loop, and the like. Good HTML is fluid, but the designer might provoke people to do some people to go fixed-position style, which is nearly always the evil...
The controls are identified in some fixed way, which you don't want to happen if you really need an array/collection of them...

So, there are many reasons to do it in code, so your idea was productive, a good one. Well, if you use this article as the basic reference (not it's also not too big) and also use your own brain, you won't loose anything...
--SA
Zoltán Zörgő 1-Oct-12 15:42pm    
Oh yes. But I think this is a little bit to much theory for the OP :) But still an extensive answer: +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900