Link to home
Start Free TrialLog in
Avatar of olootu
olootuFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Find Controls inside a DetailsView Item Template

Hi all,
I have a google Map control inside a Detailsview ( My detailsview is part of. ascx file).

 I have the following code in my code behind:

GMap GMap2 = (GMap) Page.Form.FindControl("GamblingDetailsView:GMap2");
            GMap2.enableDragging = false;
            GMap2.enableGoogleBar = true;
            GMap2.Language = "en";
            GMap2.Style.Value= "background-color:white";



 but the page throws a object null reference error when viewed
<div class="detailsview" id="detailview" runat="server" style="border:1px solid #222222; margin-bottom:15px;">
                <asp:SqlDataSource ID="GamblingDetailsView" runat="server"
                    ConnectionString="<%$ ConnectionStrings:NVCOREConnectionString %>" 
                    
                     
                    SelectCommand="SELECT * FROM [LicensingTesting] WHERE ([AltRef] = @AltRef)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="GridView1" Name="AltRef" 
                            PropertyName="SelectedValue" Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
                    DataKeyNames="SystemKey" DataSourceID="GamblingDetailsView" Height="50px" 
                    Width="125px">
                    <Fields>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <table style="width:100%;">
                                    <tr>
                                        <td>
                                            &nbsp;</td>
                                        <td>
                                            <cc1:GMap ID="GMap2" runat="server" />
                                        </td>
                                        <td>
                                            &nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td>
                                            &nbsp;</td>
                                        <td>
                                            <asp:Label ID="Label16" runat="server" Text='<%# Eval("TradingName") %>'></asp:Label>
                                        </td>
                                        <td>
                                            <asp:Label ID="Label17" runat="server" Text='<%# Eval("LocAddress1") %>'></asp:Label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            &nbsp;</td>
                                        <td>
                                            &nbsp;</td>
                                        <td>
                                            &nbsp;</td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Fields>
                </asp:DetailsView>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Member_6283346
Member_6283346

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of olootu

ASKER

GMap GMap2 = (GMap) FindControlRecursive (Page.Form, "GMap2");

the underlined throws this error:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Form.get'
Avatar of Member_6283346
Member_6283346

Where did you put this line? I meant that you should replace your
GMap GMap2 = (GMap) Page.Form.FindControl("GamblingDetailsView:GMap2");

Avatar of olootu

ASKER

Still not working.


Error on Page:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:



Code Behind Code:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Subgurim.Controles;


public partial class _Default : System.Web.UI.Page
{
    private Control FindControlRecursive(Control rootControl, string controlID)
    {
        if (rootControl.ID == controlID) return rootControl;

        foreach (Control controlToSearch in rootControl.Controls)
        {
            Control controlToReturn = FindControlRecursive(controlToSearch, controlID);

            if (controlToReturn != null) return controlToReturn;

        }
        return null;
       
    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        GMap GMap2 = (GMap)FindControlRecursive(Page.Form, "GMap2");
        GMap2.enableDragging = false;
        GMap2.enableGoogleBar = true;
        GMap2.Language = "en";
        GMap2.Style.Value = "background-color:white";
       
    }
   
   
 
}





Page Code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="GMaps" namespace="Subgurim.Controles" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
v\:* { behavior:url(#default#VML); }
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [EmployeeID], [LastName], [PostalCode] FROM [Employees] WHERE ([EmployeeID] = @EmployeeID)">
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" Name="EmployeeID"
                    PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:DetailsView ID="GamblingDetailsView" runat="server" AutoGenerateRows="False"
            DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" Height="50px"
            Width="125px">
            <Fields>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
                        <br />
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("PostalCode") %>'></asp:Label>
                        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                        <br />
                        <br />
                        <cc1:GMap ID="GMap2" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Fields>
        </asp:DetailsView>
        <br />
        <br />
        <asp:SqlDataSource ID="datasource" runat="server"
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title] FROM [Employees]"></asp:SqlDataSource>
        <br />
        <asp:GridView ID="GridView1" runat="server" DataSourceID="datasource"
            DataKeyNames="EmployeeID" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False">
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID"
                    InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />
                <asp:BoundField DataField="LastName" HeaderText="LastName"
                    SortExpression="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                    SortExpression="FirstName" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Hi!
Then try to seek control by Type not by ID:
public partial class _Default : System.Web.UI.Page
{
    private Control FindControlRecursive(Control rootControl, Type controlType)
    {
        if (rootControl.GetType().GUID == controlType.GUID) return rootControl;

        foreach (Control controlToSearch in rootControl.Controls)
        {
            Control controlToReturn = FindControlRecursive(controlToSearch, controlID);

            if (controlToReturn != null) return controlToReturn;

        }
        return null;
       
    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        GMap GMap2 = (GMap)FindControlRecursive(this, typeof(GMap));
        GMap2.enableDragging = false;
        GMap2.enableGoogleBar = true;
        GMap2.Language = "en";
        GMap2.Style.Value = "background-color:white";
       
    }
   
   
 
}
Avatar of olootu

ASKER

@Ivan

It is now working! I twicked the Page_Load a bit. Please find the Code:

Many thanks.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Subgurim.Controles;


public partial class _Default : System.Web.UI.Page 
{

    private Control FindControlRecursive(Control rootControl, string controlID)
{
    if (rootControl.ID == controlID) return rootControl;

    foreach (Control controlToSearch in rootControl.Controls)
    {
        Control controlToReturn = 
            FindControlRecursive(controlToSearch, controlID);
        if (controlToReturn != null) return controlToReturn;
    }
    return null;
}

    
    protected void Page_Load(object sender, EventArgs e)
    {
        
        Control olootu = FindControlRecursive(Page.Form, "GMap2");

        // make sure something was returned (ie: not null).
        if (olootu != null )
        {
        // cast the control to the GMap. 
            GMap GMap2 = (GMap)olootu;
           
        // do something with the GMap control.

           GMap2.enableDragging = false;
           GMap2.enableGoogleBar = true;
          GMap2.Language = "en";
        //GMap2.Style.Value = "background-color:white";

        }
       
    }
    
    
  
}

Open in new window

Avatar of olootu

ASKER

It is now working! I twicked the Page_Load a bit. Please find the Code:

Many thanks.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Subgurim.Controles;


public partial class _Default : System.Web.UI.Page 
{

    private Control FindControlRecursive(Control rootControl, string controlID)
{
    if (rootControl.ID == controlID) return rootControl;

    foreach (Control controlToSearch in rootControl.Controls)
    {
        Control controlToReturn = 
            FindControlRecursive(controlToSearch, controlID);
        if (controlToReturn != null) return controlToReturn;
    }
    return null;
}

    
    protected void Page_Load(object sender, EventArgs e)
    {
        
        Control olootu = FindControlRecursive(Page.Form, "GMap2");

        // make sure something was returned (ie: not null).
        if (olootu != null )
        {
        // cast the control to the GMap. 
            GMap GMap2 = (GMap)olootu;
           
        // do something with the GMap control.

           GMap2.enableDragging = false;
           GMap2.enableGoogleBar = true;
          GMap2.Language = "en";
        //GMap2.Style.Value = "background-color:white";

        }
       
    }
    
    
  
}

Open in new window