Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

FindControl nested within multiple controls

Posted on 2010-11-12
2
Medium Priority
?
1,131 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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