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

[Solved] Dynamic Control within EditForm based on SelectedValue

3 Answers 108 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David Rhodes
Top achievements
Rank 1
David Rhodes asked on 12 Apr 2013, 11:13 AM
Hi,

I'm trying to create dynamic controls within and EditForm of the RadGrid. I've read from previous posts that I need to do this within ItemCreated. The problem I have is that I need to create the dynamic controls based on what the user selects in a DropDownList as shown here

<EditFormSettings EditFormType="Template">
    <FormTemplate>
        <asp:DropDownList ID="AreaOfPracticeSelect" runat="server" AutoPostBack="true" DataSourceID="AreaOfPracticeDataSource" DataTextField="mps_name" DataValueField="mps_areaofpractice_lookupid" AppendDataBoundItems="true">
            <asp:ListItem Value="" Text="" />
        </asp:DropDownList>
        <h5>Criteria</h5>
        <asp:PlaceHolder ID="CriteriaPlaceHolder" runat="server" />
    </FormTemplate>
</EditFormSettings>

protected void AreaOfPracticeGrid_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        DropDownList areaOfPracticeSelect = e.Item.FindControl("AreaOfPracticeSelect") as DropDownList;
 
        if (areaOfPracticeSelect != null && !string.IsNullOrEmpty(areaOfPracticeSelect.SelectedValue))
        {
            Guid areaOfPracticeId = new Guid(areaOfPracticeSelect.SelectedValue);
 
            LoadCriteria(e.Item as GridEditFormItem, areaOfPracticeId);
        }
    }
}

The problem is that AreaOfPracticeSelect DropDown is always empty, any ideas how I can accomplish this?

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Apr 2013, 11:39 AM
Hi,

I guess you want to get the DropDownList Selected value. You will not be able to get the Selectedvalue in the ItemCreated event. Please take a look into the following code snippet.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
        DropDownList areaOfPracticeSelect =edit.FindControl("AreaOfPracticeSelect") as DropDownList;
        areaOfPracticeSelect.SelectedIndexChanged += new EventHandler(areaOfPracticeSelect_SelectedIndexChanged);
    }
}
 
void areaOfPracticeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    string val = ddl.SelectedValue;// here you will get the selected value
}

Thanks,
Princy.
0
David Rhodes
Top achievements
Rank 1
answered on 12 Apr 2013, 11:45 AM
Thanks for the reply

Yes I can get the selectedvalue from the SelectedIndexChanged but I then need to use that value to create dynamic controls within CriteriaPlaceHolder.

Obviously I can do this from the SelectedIndexChanged method (below) but then if any of the dynamic controls postback they're all lost which is why i'm trying to do it in ItemCreated

protected void AreaOfPracticeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList areaOfPracticeSelect = (DropDownList)sender;
    GridEditFormItem editItem = (GridEditFormItem)areaOfPracticeSelect.NamingContainer;
 
    if (!string.IsNullOrEmpty(areaOfPracticeSelect.SelectedValue))
    {
        Guid areaOfPracticeId = new Guid(areaOfPracticeSelect.SelectedValue);
 
        LoadCriteria(editItem, areaOfPracticeId);
    }
}

The LoadCriteria method creates all my dynamic controls
0
David Rhodes
Top achievements
Rank 1
answered on 12 Apr 2013, 12:29 PM
I think I've got it working.

I used Request.Form to get the value of AreaOfPracticeSelect

protected void AreaOfPracticeGrid_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        DropDownList areaOfPracticeSelect = e.Item.FindControl("AreaOfPracticeSelect") as DropDownList;
 
        if (areaOfPracticeSelect != null && !string.IsNullOrEmpty(Request.Form[areaOfPracticeSelect.UniqueID]))
        {
            Guid areaOfPracticeId = new Guid(Request.Form[areaOfPracticeSelect.UniqueID]);
 
            LoadCriteria(e.Item as GridEditFormItem, areaOfPracticeId);
        }
    }
}
Tags
Grid
Asked by
David Rhodes
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
David Rhodes
Top achievements
Rank 1
Share this question
or