Solved

FindControl nested within multiple controls

Posted on 2010-11-12
2
1,123 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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