This is a migrated thread and some comments may be shown as answers.

[Solved] radGrid dynamic item bind and access values

1 Answer 291 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dank
Top achievements
Rank 1
dank asked on 19 Jun 2009, 06:37 AM
hi I am bindig custom controls(tradextbox,radadateinput vs.)on runtime on a grid.And After post i wantto got values from the custom columns..
the code for creating control snippet like this

 


 

    private void CreateCustomControl(TableCell templateControl, Property property)  
        {  
            switch (property.UiType)  
            {  
                case "RadTextBox":  
                    #region  
                    RadTextBox radTextBox1 = new RadTextBox();  
                    radTextBox1.ID = string.Format("RadTextBox_{0}", property.Id);  
                    if(!IsPostBack)radTextBox1.Text = property.DefaultValue;  
                    if (property.MaxLength != null)  
                    {  
                        radTextBox1.MaxLength = (int)property.MaxLength;  
                    }  
                    templateControl.Controls.Add(radTextBox1);  
                    #endregion  
                    break;  
                case "MultilineRadTextBox":  
                    #region  
                    RadTextBox radTextBox2 = new RadTextBox();  
                    radTextBox2.ID = string.Format("MultilineRadTextBox_{0}", property.Id);  
                    radTextBox2.TextMode = InputMode.MultiLine;  
                    if (!IsPostBack) radTextBox2.Text = property.DefaultValue;  
                    if (property.MaxLength != null)  
                    {  
                        radTextBox2.MaxLength = (int)property.MaxLength;  
                    }  
                    templateControl.Controls.Add(radTextBox2);  
                    #endregion  
                    break;  
                case "NumericTextBox":  
                    #region  
                    RadNumericTextBox radNumericTextBox = new RadNumericTextBox();  
                    radNumericTextBox.ID = string.Format("NumericTextBox_{0}", property.Id);  
                    if (!IsPostBack) radNumericTextBox.Value = !string.IsNullOrEmpty(property.DefaultValue) ? double.Parse(property.DefaultValue) : (double)0;  
                    radNumericTextBox.MaxValue = (double)property.MaxValue;  
                    radNumericTextBox.MinValue = (double)property.MinValue;  
                    templateControl.Controls.Add(radNumericTextBox);  
                    #endregion  
                    break;  
                case "RadDateInput":  
                    #region  
                    RadDateInput radDatePicker = new RadDateInput();  
                    radDatePicker.ID = string.Format("RadDateInput_{0}", property.Id);  
                    radDatePicker.ReadOnly = true;  
                      
                    if (!string.IsNullOrEmpty(property.DefaultValue))  
                    {  
                        if (!IsPostBack) radDatePicker.SelectedDate = Convert.ToDateTime(property.DefaultValue);  
                    }  
                    templateControl.Controls.Add(radDatePicker);  
                    #endregion  
                    break;  
                case "RadComboBox":  
                    #region  
                    RadComboBox radComboBox = new RadComboBox();  
                    radComboBox.ID = string.Format("RadComboBox_{0}", property.Id);  
                    string[] options = property.Options.Split('#');  
                    string[] optionItemKeyValPair;  
                    foreach (string optionItem in options)  
                    {  
                        optionItemoptionItemKeyValPair = optionItem.Split('$');  
                        radComboBox.Items.Add(new RadComboBoxItem(optionItemKeyValPair[0], optionItemKeyValPair[1]));  
                    }  
                    radComboBox.Items.Insert(0, new RadComboBoxItem(GetGlobalResourceObject("GeneralResource", "PleaseSelect").ToString(), "-1"));  
                    if (!IsPostBack) radComboBox.FindItemByValue(property.DefaultValue).Selected = true;  
                    templateControl.Controls.Add(radComboBox);  
                    #endregion  
                    break;  
                case "CheckBox":  
                    #region  
                    CheckBox checkBox = new CheckBox();  
                    checkBox.ID = string.Format("CheckBox_{0}", property.Id);  
                    if (!IsPostBack) checkBox.Checked = property.DefaultValue == "0" ? false : true;  
                    templateControl.Controls.Add(checkBox);  
                    #endregion  
                    break;  
            }  
        }  
     

and i call the method  on grid's itemcreated event

        protected void SelectGrid_ItemCreated(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridDataItem)  
            {  
                GridDataItem item = e.Item as GridDataItem;  
 
                Entities.Domain.Property property = item.DataItem as Entities.Domain.Property;  
 
                if (property != null && property.PropertyTypeCodelookup != null && !string.IsNullOrEmpty(property.PropertyTypeCodelookup.UiType))  
                {  
                    CreateCustomControl(item["PropertyValueColumn"], property);  
                }  
            }  
        } 
and
if i bind grid every page_load event of page,
when i want to take values that entered into this custom controls ,it does resets values.
and if i only bind grid once at pagepostback
when i want to take values over the controls it return null referance...
public List<Property> SelectedProperties  
        {  
            get  
            {  
                List<Property> _selectedProperties=new List<Property>();  
                Property tmpProperty;  
                if (_currentPropertyCollection != null && _currentPropertyCollection.Count > 0)  
                {  
                    foreach (GridDataItem item in PropertySelectGrid.Items)  
                    {  
                        if (((CheckBox)item["Selection"].Controls[1]).Checked)  
                        {  
                            tmpProperty = _currentPropertyCollection.Find(p => p.Id == (Convert.ToInt64(item["Id"].Text)));  
                      
 
                            Control c = item["PropertyValueColumn"].Controls[0];  
                            string text = string.Empty;  
                            if (c is RadTextBox || c is RadNumericTextBox)  
                            {  
                                if (c is RadTextBox)  
                                {  
                                    text = ((RadTextBox)item["PropertyValueColumn"].Controls[0]).Text;  
                                }  
                                else if (c is RadNumericTextBox)  
                                {  
                                    text = ((RadNumericTextBox)item["PropertyValueColumn"].Controls[0]).Text;  
                                }  
                            }  
                            else if (c is RadDateInput)  
                            {  
                                if (((RadDateInput)item["PropertyValueColumn"].Controls[0]).SelectedDate.HasValue)  
                                {  
                                    text = ((RadDateInput)item["PropertyValueColumn"].Controls[0]).SelectedDate.Value.ToString();  
                                }  
                            }  
                            else if (c is RadComboBox)  
                            {  
                                if (((RadComboBox)item["PropertyValueColumn"].Controls[0]).SelectedItem != null)  
                                {  
                                    if (((RadComboBox)item["PropertyValueColumn"].Controls[0]).SelectedItem.Value != "-1")  
                                    {  
                                        text = ((RadComboBox)item["PropertyValueColumn"].Controls[0]).SelectedItem.Value;  
                                    }  
                                }  
                            }  
                            else if (c is CheckBox)  
                            {  
                                if (((CheckBox)item["PropertyValueColumn"].Controls[0]).Checked)  
                                {  
                                    text = "1";  
                                }  
                            }  
 
                            tmpProperty.DefaultValue = text;  
                            _selectedProperties.Add(tmpProperty);  
                        }  
                    }  
                }  
              //string key = "UnitPropertySelectedList_" + WebSessionManager.DataFilter.CurrentId;  
                //if (!WebSessionManager.DataFilter.ObjectList.ContainsKey(key) || WebSessionManager.DataFilter.ObjectList[key] == null)  
                //{  
                //    List<Property> propertyList = new List<Property>();  
                //    WebSessionManager.DataFilter.Add(key, propertyList);  
                //}  
                //return (List<Property>)WebSessionManager.DataFilter.ObjectList[key];  
                return _selectedProperties;  
             } 
what's wrong with this code?
and how can i  bind controls on grid dynamically  and got values  entered to  them ?

1 Answer, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 24 Jun 2009, 03:57 PM
Hello Dank,

Generally if you supply your RadGrid with data on NeedDataSource event you do not need to bind it on every Page.Load event. If you bind it on every post back it is expected to reset control's values. You can find more information on the following help article:
http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html

Regarding RadGrid lifecycle and event sequence you find more information on the links below:

help articles:
http://www.telerik.com/help/aspnet-ajax/grdeventsequence.html
http://www.telerik.com/help/aspnet-ajax/grddistinguishdifferencesbetweenitemcreateditemdatabound.html 
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

demos:
http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/definingstructure/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/groupby/defaultcs.aspx

One more think that you should have in mind is that DataItem property of GridDataItem has meaning only in the context of ItemDataBound event when the item is already bound.

I hope all this directions are helpful.

All the best,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
dank
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Share this question
or