Solved

FindControl nested within multiple controls

Posted on 2010-11-12
2
1,121 Views
Last Modified: 2012-06-27
I have a DropDownList which is located within a TemplateField of a DetailsView control which is in turn located within the EmptyDataTemplate of a GridView control. I am trying fruitlessly to locate the DDL via FindControl so that I can run commands against it/reference it within the Inserting command of a DataSource. Help :(

Here is the markup and below it I have used Stringbuilder to further determine all the parent objects of the control but I still cannot coerce FindControl into locating it...


<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="default.Account.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        DataKeyNames="contactid" DataSourceID="SDSTest1"
        onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="firstname" HeaderText="firstname"
                SortExpression="firstname" />
            <asp:BoundField DataField="middlename" HeaderText="middlename"
                SortExpression="middlename" />
            <asp:BoundField DataField="lastname" HeaderText="lastname"
                SortExpression="lastname" />
            <asp:BoundField DataField="contactid" HeaderText="contactid"
                InsertVisible="False" ReadOnly="True" SortExpression="contactid" />
            <asp:ButtonField CommandName="Insert" Text="New" />
        </Columns>
        <EmptyDataTemplate>
            <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
                DataKeyNames="id" DataSourceID="SDSTest2" DefaultMode="Insert" Height="50px"
                Width="125px">
                <Fields>
                    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
                        ReadOnly="True" SortExpression="id" />
                    <asp:TemplateField HeaderText="contactid" SortExpression="contactid">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("contactid") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SDSTest2"
                                DataTextField="title" DataValueField="contactid">
                            </asp:DropDownList>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("contactid") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
                    <asp:CommandField ShowInsertButton="True" />
                </Fields>
            </asp:DetailsView>
            <asp:SqlDataSource ID="SDSTest2" runat="server"
                ConnectionString="<%$ ConnectionStrings:cmlmConnectionString1 %>"
                DeleteCommand="DELETE FROM [tblTest] WHERE [id] = @id"
                InsertCommand="INSERT INTO [tblTest] ([contactid], [title]) VALUES (@contactid, @title)"
                SelectCommand="SELECT [id], [contactid], [title] FROM [tblTest]"
                UpdateCommand="UPDATE [tblTest] SET [contactid] = @contactid, [title] = @title WHERE [id] = @id">
                <DeleteParameters>
                    <asp:Parameter Name="id" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="contactid" Type="Int32" />
                    <asp:Parameter Name="title" Type="String" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="contactid" Type="Int32" />
                    <asp:Parameter Name="title" Type="String" />
                    <asp:Parameter Name="id" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>
        </EmptyDataTemplate>
    </asp:GridView>
    <asp:SqlDataSource ID="SDSTest1" runat="server"
        ConnectionString="<%$ ConnectionStrings:sdsConnectionString1 %>"
        DeleteCommand="DELETE FROM [tblContacts] WHERE [contactid] = @contactid"
        InsertCommand="INSERT INTO [tblContacts] ([firstname], [middlename], [lastname]) VALUES (@firstname, @middlename, @lastname)"
        SelectCommand="SELECT [firstname], [middlename], [lastname], [contactid] FROM [tblContacts]"
        UpdateCommand="UPDATE [tblContacts] SET [firstname] = @firstname, [middlename] = @middlename, [lastname] = @lastname WHERE [contactid] = @contactid">
        <DeleteParameters>
            <asp:Parameter Name="contactid" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="firstname" Type="String" />
            <asp:Parameter Name="middlename" Type="String" />
            <asp:Parameter Name="lastname" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="firstname" Type="String" />
            <asp:Parameter Name="middlename" Type="String" />
            <asp:Parameter Name="lastname" Type="String" />
            <asp:Parameter Name="contactid" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
</asp:Content>


Stringbuilder output:

DropDownList1 (System.Web.UI.WebControls.DropDownList) ->
(System.Web.UI.WebControls.DataControlFieldCell) ->
(System.Web.UI.WebControls.DetailsViewRow) ->
(System.Web.UI.WebControls.ChildTable) ->
DetailsView1 (System.Web.UI.WebControls.DetailsView) ->
(System.Web.UI.WebControls.TableCell) ->
(System.Web.UI.WebControls.GridViewRow) ->
(System.Web.UI.WebControls.ChildTable) ->
GridView1 (System.Web.UI.WebControls.GridView) ->
MainContent (System.Web.UI.WebControls.ContentPlaceHolder) ->
(System.Web.UI.HtmlControls.HtmlForm) ->
(ASP.site_master) ->
__Page (ASP.account_webform1_aspx)
0
Comment
Question by:alright
2 Comments
 
LVL 12

Accepted Solution

by:
CmdoProg2 earned 125 total points
ID: 34123800
I set the cancel property to true for my testing..
The First is from DetailsView1_ItemInserting event where you can get the detailveiw fro the sender object.
The gridview has more control nodes since is has multiple rows and repeating cells(columns).  I have found gridviews and detailsviews as certain type of containers that has a single child control that contains all of their child controls. Therefore, you need to use DetailsView1.Controls[0] as the object to find controls on the DetailsView.

Try

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)

    {

      DropDownList ddl = (DropDownList)((DetailsView)(sender)).Controls[0].FindControl("DropDownList1");

      // Or 

      DetailsView dtv = (DetailsView)GridView1.Controls[0].Controls[0].Controls[0].FindControl("DetailsView1");

      DropDownList ddl2 =(DropDownList)dtv.Controls[0].FindControl("DropDownList1"); 

      e.Cancel = true;

    }

Open in new window

0
 

Author Closing Comment

by:alright
ID: 34124172
     DetailsView dtv = (DetailsView)GridView1.Controls[0].Controls[0].Controls[0].FindControl("DetailsView1");
      DropDownList ddl2 =(DropDownList)dtv.Controls[0].FindControl("DropDownList1");

Was exactly what I needed! Thank you!! And thank you for the explanation
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now