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

How to manually bind to a control created in ItemCreated event

5 Answers 346 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JJ
Top achievements
Rank 1
JJ asked on 06 Mar 2009, 04:36 PM
I have a dropdownlist that was created in a radgrid in the ItemCreated event.  The radgrid is auto-generating the columns, so I have implemented code found elsewhere on the site to replace the textbox control with a dropdownlist.  I have successfully populated the dropdownlist with values at runtime.  Now I wish to bind the new control to the field that the old textbox was bound to.

Here is the code that creates the control in the ItemCreated Event:
                            else if (key == "3"
                            { 
                                objtxt.Visible = false
                                DropDownList drp = new DropDownList(); 
                                drp.DataSource = dataarray; 
                                item[SRfields[valuecount]].Controls.Add(drp); 
                                item[SRfields[valuecount]].Controls[1].DataBind(); //don't know if this is required 
                            } 
How do I bind the new control so the insert and update forms work?

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Mar 2009, 07:28 AM
Hi JJ,

Try replacing the TextBox with DropDownList when the Grid is in edit mode in the ItemDataBound event .

ASPX:
 
<
telerik:GridBoundColumn UniqueName="ProductName"  HeaderText="ProductName" DataField="ProductName" ></telerik:GridBoundColumn> 
            

CS:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
           
        if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode)) 
        { 
            GridEditableItem editItem = (GridEditableItem)e.Item; 
            DropDownList ddl = new DropDownList(); 
            ddl.ID = "DropDownList1"
            editItem["ProductName"].Controls.Clear(); 
            editItem["ProductName"].Controls.Add(ddl); 
            //populate the DropDownList 
            ddl.DataSource=dataarray 
            ddl.DataBind(); 
 
        }   
 
    } 



note: Also call DataBind() after setting the DataSource for the DropDownList.

Thanks
Shinu.
0
JJ
Top achievements
Rank 1
answered on 09 Mar 2009, 02:56 PM
Shinu,
Thank you for your reply.  I attempted to implement the code but could not do so exactly:
Unfortunately, the field names are not known until runtime; they are dynamically generated.  As a result of this, the ASPX supplied would not be a possibility.  I did otherwise apply the code suggested.  It appears that in my particular context, using the lines:
editItem["fieldname"].Controls.Clear();
editItem["fieldname"].Controls.Add(ddl);
will cause no controls to render for that field if I add it during itemcreated event.  When I add it during the ItemDataBound event, attempting to save a record results in this error:
  • Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

My previous example did indeed render the control, but did not bind it to the field when adding or update.  I am assuming that this has to do with the fact that the fields are being automatically generated by radgrid during runtime from the supplied datasource.  The various help files alluded to using the itemdatabound event in conjunction with itemcreated for this situation, but I found no code for this manual process.
0
JJ
Top achievements
Rank 1
answered on 13 Mar 2009, 01:14 PM
*bump* Does anyone have a line on this?  The help files talk about it, but there is no example.
0
JJ
Top achievements
Rank 1
answered on 06 May 2009, 08:18 PM
Still having this problem; I cannot find a solution.  I would provide more information if I new what to provide.

0
JJ
Top achievements
Rank 1
answered on 20 May 2009, 08:48 PM
I did fix this by adding a new dropdownlist control in the itemcreated event.  I then used client-side java to copy the value to the bound field using the client onchange event.  This way the form acts as it would without mucking about in the viewstate.
Tags
Grid
Asked by
JJ
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
JJ
Top achievements
Rank 1
Share this question
or