Dynamically add options select using javascript

Dynamically add options select using javascript

Author: Kaklopin Date of post: 23.06.2017

Here is the skeleton of class DynamicallyTemplatedGridViewHandler implementing ITemplate with a list of data members and methods. ItemType keeps the type of a list item type: Item, EditItem, Header, Footer, AlternatingItem, Pager, SelecetdItem or Separator. In this demo version we need only three of these; we need Header For heading of each columnItem for showing fields when GridView will be in normal mode and EditItem for showing fields when GridView will be in Edit mode. FieldName keeps the name of each template field that will be displayed in the header.

InfoType keeps an indicator in string form for a type of information within a template field i. A "Command" Edit, Delete, and Insert requires instantiation in the Button control while he "String" requires a Label or TextBox.

Now, coming to the member methods listed above, there is a constructer which simply sets the aforementioned data members with those passed as parameters. Here is the explanation of InstantiateIn the only method of ITemplate being implemented by our class DynamicallyTemplatedGridViewHandler. InstantiateIn ensures that the list item type of each template is created in its appropriate control. For better understanding of functionality of this method see its name "InstantiateIn. Like in the case of a Header, it is instantiated in Literal control as shown in this part of the implementation of InstantiateIn.

InstantiateIn takes a "Container," a Control type object as a parameter. Container's control collection is filled with all controls in which items of each type are instantiated. Finally, add this control to the control collection of the Container control passed as parameter to InstantiateIn method. Similarly, we have to write instantiation code for ItemType if it is "Item" and "EditItem.

If InfoType is a Button then it creates three buttons for the aforementioned tasks. It is simple to do; create a button object, set its all properties accordingly, also add the button's click event handler and finally, add it into the control collection of the control Container passed as an argument.

When InfoType is not a Command it means we have to instantiate it with a label as in GridView's normal mode when each cell text of GridView's rows is displayed in label. Therefore, by default, control is instantiated with Label and is added into the Container.

Since we have no more info types other than command and string then no further checks are required. We come to the outer check when ItemType is EditItem. Now, we need an inner check for info type command or string. The template field will be instantiated in Button if the info type is Command; otherwise it requires TextBox for the cell text of edit item. If the these text boxes are emptied, they can be used for insertion instead of editing without dedicating an extra row for it.

This is easy and handy as it becomes a better approach when it is not known in advance how many columns there are in GridView's data source. I have taken a session variable InsertFlag that is set to 0 and 1 for Edit and Inert operations respectively. Since we have to bind the labels in Item template and text boxes in EditItem template with corresponding cell values, the data binding event handler OnDataBinding of both label and text box populates the fields with cell values accordingly.

And you might also want to know how text boxes for each field get emptied when Insert button is clicked. The solution is simple; do not bind them with a database and apply a check while adding the data binding event handler of text box. Do not call OnDataBinding if the insert button is clicked. The implementation of the data binding event handler "OnDataBindin"' is simple. They do nothing except the former sets the Session[InsertFlag] to 1 and the later sets it to 0.

Hi Mohyudhin, Actually i tested this for my scenario and its not working when i try to add the grid control itself from code behind instead of declaring it in DOM HTML. In that case the edit item template update and cancel button events are not firing. Anybody faced similar issue? Hey Bro, Hope you are doing good. Thanks very very much Bro. Nicely explained and clean code dude. Can you please help with many have asked also having a dropdown in the column.

Would be of great help. Aprreciate your efforts bro. Thanks again Take Care Bro. Hi, I have followed your codes, but once I click on the Edit button, event onRowEdit of gridview doesn't fire at all. Then I decide to use AutogenerateEditButton of Gridview, but I can't get the control of the gridview after I click on updating.

Can you please help me out? Hi, The sample code is simply great and also the explanation on the code was really very useful. But, I need further help on modifying the same code for adding dropdownlist control instead of textbox control as EditTemplate on clicking the Edit button for string data types. Please help me with a sample source code on the above regards.

Thanks and regards, Ram Email Id: Really awesome Work done,Keep on the great work Title: The film features an international ensemble cast including Leonardo DiCaprio, Ken Watanabe, Joseph Gordon-Levitt, Marion Cotillard, Ellen Page, Tom Hardy, Cillian Murphy, Dileep Rao, Tom Berenger, and Michael Caine. DiCaprio plays Dom Cobb, a specialized corporate spy and thief whose work consists of secretly extracting valuable commercial information from the unconscious minds of his targets while they dream.

Wanted for murder and unable to visit his children, Cobb is offered a chance to regain his old life as payment for a task considered to be impossible: Really you have described it very well. Keep up the great work Amiya Ranjan sahoo Date: Hi i want from which content textbox and gridview but gridview also content textbox that data can be store in database.

Hi Only a sugestion, have you check to see what kind of code generate your application? Maybe the solution cames from that direction. As written in title a great sample u have given. Thanks a lot and keep on this: THANK YOU FOR THIS SAMPLE Title: Hello Mahr, I have one problem. If I clik the edit button means, the corresponding field must populate in the text box above the grid.

Please explain how to do this. Thanks for the Link. I appreciate it very much. NET Version of this? This article was very useful for me, but I was wandering about one problem I am having with this. If I insert a new record into the gridview, how to set the FOCUS to any particular text-field in this new record?

Did you have this problem before and how to solve that? Your response will be apreciated. Pardon me, a correction. If you use AddHandler your GridView doesn't need to be a field; it can be a variable as you describe. You also don't need the Handles keyword. This is a great solution. I am using this to Dynamically create the entire GridView object. Dim gvSchedule As New GridView But I'm not sure how I would reference a Dynamically created GridViews RowEditing, RowDeleting This is a very very nice and helpful article.

I want to update only particular fields in my gridview,but when i click on edit entire row get selected Hi, i want to make the first column a dropdown list how do i go about doing this. I had asked for help in sorting and paging. I have been able to implement. I am posting the solution incase somebody is in search for it. Create the grid in oninit. Add the follwing code. Only Header text and sort expression. This article has been very helpful.

javascript - Adding options to a