olootu
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("Gam blingDetai lsView:GMa p2");
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
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("Gam
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>
</td>
<td>
<cc1:GMap ID="GMap2" runat="server" />
</td>
<td>
</td>
</tr>
<tr>
<td>
</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>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Where did you put this line? I meant that you should replace your
GMap GMap2 = (GMap) Page.Form.FindControl("Gam blingDetai lsView:GMa p2");
GMap GMap2 = (GMap) Page.Form.FindControl("Gam
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.NullReferenceExcept ion: 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(Contr ol rootControl, string controlID)
{
if (rootControl.ID == controlID) return rootControl;
foreach (Control controlToSearch in rootControl.Controls)
{
Control controlToReturn = FindControlRecursive(contr olToSearch , 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.Contro les" 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-micro soft-com:v ml">
<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:Northwin dConnectio nString %>"
SelectCommand="SELECT [EmployeeID], [LastName], [PostalCode] FROM [Employees] WHERE ([EmployeeID] = @EmployeeID)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="EmployeeID"
PropertyName="SelectedValu e" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="GamblingDetailsView" runat="server" AutoGenerateRows="False"
DataKeyNames="EmployeeID" DataSourceID="SqlDataSourc e1" 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:Northwin dConnectio nString %>"
SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title] FROM [Employees]"></asp:SqlData Source>
<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>
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.NullReferenceExcept
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.
using System.Xml.Linq;
using Subgurim.Controles;
public partial class _Default : System.Web.UI.Page
{
private Control FindControlRecursive(Contr
{
if (rootControl.ID == controlID) return rootControl;
foreach (Control controlToSearch in rootControl.Controls)
{
Control controlToReturn = FindControlRecursive(contr
if (controlToReturn != null) return controlToReturn;
}
return null;
}
protected void Page_Load(object sender, EventArgs e)
{
GMap GMap2 = (GMap)FindControlRecursive
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"
<%@ Register assembly="GMaps" namespace="Subgurim.Contro
<!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-micro
<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:Northwin
SelectCommand="SELECT [EmployeeID], [LastName], [PostalCode] FROM [Employees] WHERE ([EmployeeID] = @EmployeeID)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="EmployeeID"
PropertyName="SelectedValu
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="GamblingDetailsView" runat="server" AutoGenerateRows="False"
DataKeyNames="EmployeeID" DataSourceID="SqlDataSourc
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:Northwin
SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title] FROM [Employees]"></asp:SqlData
<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(Contr ol rootControl, Type controlType)
{
if (rootControl.GetType().GUI D == controlType.GUID) return rootControl;
foreach (Control controlToSearch in rootControl.Controls)
{
Control controlToReturn = FindControlRecursive(contr olToSearch , 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";
}
}
Then try to seek control by Type not by ID:
public partial class _Default : System.Web.UI.Page
{
private Control FindControlRecursive(Contr
{
if (rootControl.GetType().GUI
foreach (Control controlToSearch in rootControl.Controls)
{
Control controlToReturn = FindControlRecursive(contr
if (controlToReturn != null) return controlToReturn;
}
return null;
}
protected void Page_Load(object sender, EventArgs e)
{
GMap GMap2 = (GMap)FindControlRecursive
GMap2.enableDragging = false;
GMap2.enableGoogleBar = true;
GMap2.Language = "en";
GMap2.Style.Value = "background-color:white";
}
}
ASKER
@Ivan
It is now working! I twicked the Page_Load a bit. Please find the Code:
Many thanks.
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";
}
}
}
ASKER
It is now working! I twicked the Page_Load a bit. Please find the Code:
Many thanks.
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";
}
}
}
ASKER
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.g