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

RadGrid Insert Form

3 Answers 259 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sunil
Top achievements
Rank 1
Sunil asked on 25 Mar 2013, 01:56 PM
Hello,

I am using a RadGrid and I want to insert a new item into the grid using the built-in RadGrid functionality.  The new item has been defined as follows:

<EditFormSettings EditFormType="Template">

<FormTemplate>

<div>

<h3>

New Team Culture Assessment

</h3>

<table>

<tr>

<td>

<asp:Label ID="EntitiesLabel" runat="server" Text="Entities"></asp:Label>

</td>

<td>

<asp:DropDownList ID="EntityList" runat="server" AutoPostBack="True"

OnSelectedIndexChanged = "Entity_selected">

</asp:DropDownList>

 

</td>

</tr>

<tr>

<td>

<asp:Label ID="TeamsLabel" runat="server" Text="Teams"></asp:Label>

</td>

<td>

<asp:DropDownList ID="TeamList" runat="server">

</asp:DropDownList>

</td>

</tr>

</table>

</div>

</FormTemplate>

</EditFormSettings

 

 

>

My questions:

1.  I want to populate the EntityList and TeamList drop-downs in the page-load using "object data source", but when I access them directly, I am getting an error saying that the name EntityList is not found in current context...here is what I am doing in !postback in page_load:

var bd = new BfEntityDAO();

List<BfEntity> ents = bd.GetEntities();

EntityList.DataSource = ents;

EntityList.DataTextField = "BfEntityName";

EntityList.DataBind();

How can I populate these drop-downs so that the user can see the values when he inserts a new item?

2.  When the user opens the insert form and selects an entity from the entitylist drop-down, I want to populate the teamlist drop-down based on the value selected in the entitylist drop-down.  How can I accomplish this?

This is the code snippet I am using currently in the codebehind, but the method is not being called:

 Method called when item in entity drop own list selected

===========================================

try
{

var cd = new TeamDAO();

List<Team> teams = cd.GetTeamsForEntity(entityDropDown.SelectedValue);

teamDropDown.DataSource = teams;
teamDropDown.DataTextField = "TeamName";
teamDropDown.DataValueField = "Id";
teamDropDown.DataBind();

}
catch (Exception ex)
{
Common.Utils.HandleException(ex);
}



3.  Finally, when the user clicks OK on the form, I want a insert the data into the database from the code-behind file. How can I get a callback to method in the code-behind and how can I access the data entered by the user?

Thank you

 

 

 

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 25 Mar 2013, 06:36 PM
Hello,

<EditFormSettings EditFormType="Template">
                    <FormTemplate>
                        <div>
                            <h3>
                                New Team Culture Assessment
                            </h3>
                            <table>
                                <tr>
                                    <td>
                                        <asp:Label ID="EntitiesLabel" runat="server" Text="Entities"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="EntityList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Entity_selected">
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="TeamsLabel" runat="server" Text="Teams"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="TeamList" runat="server">
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </FormTemplate>
                </EditFormSettings>
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            DropDownList TeamList = item.FindControl("TeamList") as DropDownList; ;
            DropDownList EntityList = item.FindControl("EntityList") as DropDownList;
            //access both drop down here
        }
 
        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            DropDownList TeamList = item.FindControl("TeamList") as DropDownList; ;
            DropDownList EntityList = item.FindControl("EntityList") as DropDownList;
            //access both drop down here
        }
 
 protected void Entity_selected(object sender, EventArgs e)
        {
            DropDownList EntityList = sender as DropDownList;
            GridEditFormItem item = EntityList.NamingContainer as GridEditFormItem;
            DropDownList TeamList = item.FindControl("TeamList") as DropDownList; ;
            // Acceess entitylist dropdown
            // assign datasource to TeamList Dropdown
        }
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem item = e.Item as GridEditFormItem;
                DropDownList EntityList = item.FindControl("EntityList") as DropDownList;
                // Assign datasource here
            }
}


Thanks,
Jayesh Goyani
0
Sunil
Top achievements
Rank 1
answered on 26 Mar 2013, 01:24 AM
Hi Jayesh,

Thank you for the information.  I don't need edit/update, only insert.  

I have a quick follow-on question.

1. I am assuming that when the user clicks the insert button, the Grid_InsertCommand method is called.  Here, I am populating the two drop-downs using the object datasource in the code behind.

2.  When the user selects the entities drop-down, I am assuming that the Entity_Selected method is called in the code-behind.  I am populating the team dropdown based on the selected entity value

3.  Which method in code-behind is called when the user clocks the OK button on the form to insert the form data?
0
Shinu
Top achievements
Rank 2
answered on 26 Mar 2013, 04:07 AM
Hi,

You can populate the dropdownlist in insert mode as shown below.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
        if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
        {
            GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
           DropDownList EntityList = item.FindControl("EntityList") as DropDownList;
        }
}
When user clicks Ok button,UpdateCommand event is fired. Also check the demo for more.
Grid - Form Template Edit Form

Thanks,
Shinu
Tags
Grid
Asked by
Sunil
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Sunil
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or