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

How to access Form Elements from Code Behind when inside a RadGrid's FormTemplate

2 Answers 605 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jay
Top achievements
Rank 1
Jay asked on 17 Mar 2014, 06:21 PM

Hello, I have a RadGrid with an EditFormType set to 'Template'. I can't figure out how to access form elements inside this FormTemplate from the code behind. I'm trying to make one drop down change its options when another one is selected.

When I try to compile this code, it tells me "The name 'drpEditTarget' does not exists in the current context.". If I move that drop-down outside of the RadGrid, it's suddenly accessible again. I guess my two questions are...
--1) How can I access this element from the code behind?
--2) What's the difference between elements outside and inside the Grid? Why can I access the former but not the latter? (the answer to this might be a more broad asp.net question)

Default.aspx

<Telerik:RadGrid ID="RequestsGrid"runat="server" OnNeedDataSource="RequestsGrid_NeedDataSource" OnCancelCommand="RequestGrid_CancelCommand" OnDeleteCommand="RequestsGrid_DeleteCommand" OnItemCommand="RequestGrid_ItemCommand" OnItemDataBound="RequestsGrid_ItemDataBound" AllowAutomaticDeletes="false" AllowAutomaticInserts="false" AllowAutomaticUpdates="true" DataSourceID="requests">
    <MasterTableView AutoGenerateColumns="false" DataKeyNames="id,requester,output,notes" DataSourceID="requests" CommandItemDisplay="Bottom">
        <EditFormSettings EditFormType="Template">
            <EditColumn UniqueName="EditColumn"></EditColumn>
            <FormTemplate>
                        <b>First Target:</b>
                        <asp:DropDownList runat="server" SelectedValue='<%# Eval("target") %>' ID="drpEditTarget" AutoPostBack="true" DataSource='<%# getPossibleTargets( (string)Eval("output") ) %>' OnSelectedIndexChanged="drpEditTarget_SelectedIndexChanged"></asp:DropDownList>
                        <b>Second Target:</b></td>
                        <asp:DropDownList runat="server" ID="drpEditSecondTarget"></asp:DropDownList>
                <asp:Button ID="btnUpdate" CommandName="Update" Text="Update" runat="server" />
                <asp:Button ID="btnCancel" CommandName="Cancel" Text="Cancel" runat="server" />
            </FormTemplate>
        </EditFormSettings>
        <Columns>
            <telerik:GridEditCommandColumn UniqueName="EditButton" Visible="false" ItemStyle-CssClass="skinny"/>
            <telerik:GridButtonColumn Text="Delete" UniqueName="DeleteButton" CommandName="Delete"></telerik:GridButtonColumn>
            <telerik:GridBoundColumn DataField="id" HeaderText="ID" UniqueName="id"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="requester" HeaderText="Requester" UniqueName="requester"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="output" HeaderText="Output" UniqueName="output"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="notes" HeaderText="Notes" UniqueName="notes"></telerik:GridBoundColumn>
            <telerik:GridButtonColumn Text="Approve" UniqueName="ApproveButton" CommandName="Approve"></telerik:GridButtonColumn>
            <telerik:GridButtonColumn Text="Deny" UniqueName="DenyButton" CommandName="Deny"></telerik:GridButtonColumn>
        </Columns>
    </MasterTableView>
</Telerik:RadGrid>

Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        populateGrid();
  
        if (!Page.IsPostBack)
        {
            setFilterOptions();
            RequestsGrid.Rebind();
        }
    }
      
    protected void drpEditTarget_SelectedIndexChanged(object sender, EventArgs e)
    {
        drpEditSecondTarget.DataSource = getPossibleDestinations( drpEditTarget.SelectedValue.ToString() );
        drpEditSecondTarget.DataBind();
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 18 Mar 2014, 04:39 AM
Hi Jay,

Please try the following code snippet to access the controls in edit mode.

C#:
protected void drpEditTarget_SelectedIndexChanged(object sender, EventArgs e)
{
  DropDownList drpEditTarget = (DropDownList)sender;
  GridEditableItem edit = (GridEditableItem)drpEditTarget.NamingContainer;
  //Accessing the second DropDownList
  DropDownList drpEditSecondTarget = (DropDownList)edit.FindControl("drpEditSecondTarget");
  drpEditSecondTarget.DataSource = getPossibleDestinations(drpEditTarget.SelectedValue.ToString());
  drpEditSecondTarget.DataBind();
}

Also take a look at this article to more about Accessing Cells and Rows of RadGrid.

Thanks,
Princy
0
Jay
Top achievements
Rank 1
answered on 18 Mar 2014, 05:51 AM
Thank you for both the answer and the documentation. That worked!
Tags
Grid
Asked by
Jay
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jay
Top achievements
Rank 1
Share this question
or