?
Solved

FindControl nested within multiple controls

Posted on 2010-11-12
2
Medium Priority
?
1,133 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

839 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