0

I'm working on an aspx page where I'm trying to reference a dropdownlist from my code behind, now I add the dropdownlist to the page (ddlProgram) and its fine but soon as I move it into the InsertItemTemplate or EditItemTemplate or my GridView where I need it to go the designer entry disappears and I can no longer reference it from the code behind, is there a way around this? I have tried using the FindControl("ControlID") method but it always returns null. I need to set the selected index to 0 when the cancel button is click on my page. Thanks!

                <asp:TemplateField HeaderText="Program" SortExpression="IDProgram">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlProgram" AppendDataBoundItems="true" runat="server" DataSourceID="odsProgram" DataTextField="NameWithCode" DataValueField="IDProgram" CssClass="form-control ddl-normalize">
                            <asp:ListItem Text="No Program" Value="-1"></asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="programName" runat="server" Text='<%# getProgramName((Eval("IDProgram")?? string.Empty).ToString()) %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
Steve Holland
  • 619
  • 1
  • 12
  • 29
  • Seem to have solved this, Need to reference the parent control, in this case the GridView and use the "GridViewID".FindControls("ddlControlID") method to locate it. Does any one know a better way to do this? – Christopher Guigue Apr 27 '16 at 14:37
  • In which event are you trying to retrieve the DropDownList? – ConnorsFan Apr 27 '16 at 16:20
  • In a click event of a button, the button is located below the gridview, I wanted to empty the textboxes and dropdowns located in the InsertItemTemplate of the GridView protected void btnCancelSurveyDefinition_Click(object sender, EventArgs e) { DropDownList ddlPrograms = (DropDownList)fvSurveyDefinition.FindControl("ddlPrograms"); ddlPrograms.SelectedIndex = 0; } – Christopher Guigue Apr 27 '16 at 17:32

0 Answers0