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

How to fill data for a DropDownList in a FormTemplate

2 Answers 115 Views
Grid
This is a migrated thread and some comments may be shown as answers.
t800t8
Top achievements
Rank 1
t800t8 asked on 03 Feb 2009, 09:35 AM
I have a FormTemplate

                        <FormTemplate> 
                            <table> 
                                <tr> 
                                    <td> 
                                        First name: 
                                    </td> 
                                    <td> 
                                        <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox> 
                                    </td> 
                                    <td> 
                                        Last name: 
                                    </td> 
                                    <td> 
                                        <asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox> 
                                    </td> 
                                   <td> 
                                        Role: 
                                    </td> 
                                    <td> 
                                        <asp:DropDownList ID="ddlRoles" runat="server"></asp:DropDownList> 
                                    </td> 
                                    <td align="right"
                                        <asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Visible='<%# Eval("Id").ToString().Length != 0  %>'>Update</asp:LinkButton> 
                                        <asp:LinkButton ID="btnAdd" runat="server" CommandName="PerformInsert" Visible='<%# Eval("Id").ToString().Length == 0 %>'>Add</asp:LinkButton> 
                                        &nbsp;&nbsp;&nbsp; 
                                        <asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton> 
                                    </td> 
                                </tr> 
                            </table> 
                        </FormTemplate> 

        protected void rgUsers_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode)) 
            { 
                GridEditFormItem editedItem = (GridEditFormItem) e.Item; 
                DropDownList ddlRoles = (DropDownList)editedItem.FindControl("ddlRoles"); 
                ddlRoles.DataSource = Role.GetAll(); 
                ddlRoles.DataTextField = "Name"
                ddlRoles.DataValueField = "Id"
            } 
        } 

I tried to fill data for the DropDownList in ItemDataBound event but the DropDownList is empty even Role.GetAll() returns 3 items.

Also I tried to apply as this example: http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/templateformupdate/defaultcs.aspx, but in Visual Studio, SelectedValue of DropDownList doesn't show up.


2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 04 Feb 2009, 06:23 AM
Hello,

In the code snippet shown above I can't see a DataBind() method being called after setting the DataSource for the DropDownList. Try calling the DataBind() method after setting the DataSource. Also try moving the above code logic to Item Created event.

CS:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
       if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))  
            {  
                GridEditFormItem editedItem = (GridEditFormItem) e.Item;  
                DropDownList ddlRoles = (DropDownList)editedItem.FindControl("ddlRoles");  
                ddlRoles.DataSource = Role.GetAll();  
                ddlRoles.DataBind(); 
                ddlRoles.DataTextField = "Name";  
                ddlRoles.DataValueField = "Id";  
            }  
    } 


Thanks
Shinu
0
t800t8
Top achievements
Rank 1
answered on 04 Feb 2009, 09:00 AM
I tried it with ItemDataBound event and it works. Actually it should be put inside ItemCreated event as in your code. But now I use an ObjectDataSource instead. Thanks anyway, Shinu.
Tags
Grid
Asked by
t800t8
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
t800t8
Top achievements
Rank 1
Share this question
or