472,989 Members | 3,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

Need help with accessing a control within a template field

Hello
I have a dropdownlist inside the gridview as a template column defined
as follows:
<asp:TemplateField HeaderText="Choose Location">
<ItemTemplate>
<asp:DropDownList ID="ddlChooseLoc" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
I have the gridview inside of a master page- content hierarchy.
I have declared the CodeFileBaseClass in your @ Page directive; I have
also declared a variable of the type dropdownlist in the base class.
When I try to access it from the partial class I am getting a null
value for the dropdownlist variable.
I even tried to a FindControl on the dropdownlist still I am getting a
null.
Any idea what I may be missing?
Thanks
-Siva

Apr 28 '06 #1
5 2701
I am not sure how the Master Page bit fits into this problem because you
havn't said where you are trying to access the GridView from and where the
GridView is (i.e. master or child page for both questions).

Generally the problem with accessing a control within a GridView
TemplateField is that there are lots of the (i.e. one on each row) so you
can't use GridView.FindControl("ChildControlID");.

What you have to do is use find control on a particular row e.g.

GridView.Rows[2].FindControl("ChildControlId");

If you want to set a property on a control based on a set of criteria a good
place to do it is in the RowDataBound event handler. For example the below
code sets the colour of a lable control to Red when the text Value = Invalid
Product

protected void gvwOrderDetails_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (((Label)e.Row.FindControl("lblDescription")) != null)
{
if (((Label)e.Row.FindControl("lblDescription")).Text ==
"Invalid Product")
{
((Label)e.Row.FindControl("lblDescription")).ForeC olor =
System.Drawing.Color.Red;
lblInvalidProduct.Visible = true;
btnNext4.Visible = false;
}
}
}

Please note that i also check to see if the control i am looking for is set
to null to account for rows such as headers etc that don't contain the
control i am looking for.
"Siva" wrote:
Hello
I have a dropdownlist inside the gridview as a template column defined
as follows:
<asp:TemplateField HeaderText="Choose Location">
<ItemTemplate>
<asp:DropDownList ID="ddlChooseLoc" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
I have the gridview inside of a master page- content hierarchy.
I have declared the CodeFileBaseClass in your @ Page directive; I have
also declared a variable of the type dropdownlist in the base class.
When I try to access it from the partial class I am getting a null
value for the dropdownlist variable.
I even tried to a FindControl on the dropdownlist still I am getting a
null.
Any idea what I may be missing?
Thanks
-Siva

Apr 28 '06 #2
Thank you for the reply. Actually as a matter of fact I am trying the
access the dropdown control from the Row_DataBound method.
I am also trying to do the FindControl on the row I am trying to update
like this:

ddl2 = (DropDownList)e.Row.FindControl("ddlChooseLoc");---> at this
point I am getting
a null value for ddl2

I have defined the variable ddl2 in the base class file (which is
inherited from Page class
placed under app_code folder and my partial class derives from this
base class) as follows:
protected DropDownList ddl2;
I have the CodeBasefile in the @Page set to the base class.

Here is a part of my aspx page:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="OrderTable.aspx.cs"
Inherits="OrderTable" Title="Untitled Page"
CodeFileBaseClass="OrderTable_Base"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1" AllowPaging="True"
AutoGenerateColumns="False" AllowSorting="True"
AutoGenerateEditButton="True"
OnRowDataBound="GridView1_RowDataBound"
OnRowUpdating = "GridView1_RowUpdating"
OnSelectedIndexChanged="GridView1_SelectedIndexCha nged">
<Columns>
</Columns>
<asp:TemplateField HeaderText="Choose Location">
<ItemTemplate>
<asp:DropDownList ID="ddlChooseLoc" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

Thanks
-Siva

clickon wrote:
I am not sure how the Master Page bit fits into this problem because you
havn't said where you are trying to access the GridView from and where the
GridView is (i.e. master or child page for both questions).

Generally the problem with accessing a control within a GridView
TemplateField is that there are lots of the (i.e. one on each row) so you
can't use GridView.FindControl("ChildControlID");.

What you have to do is use find control on a particular row e.g.

GridView.Rows[2].FindControl("ChildControlId");

If you want to set a property on a control based on a set of criteria a good
place to do it is in the RowDataBound event handler. For example the below
code sets the colour of a lable control to Red when the text Value = Invalid
Product

protected void gvwOrderDetails_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (((Label)e.Row.FindControl("lblDescription")) != null)
{
if (((Label)e.Row.FindControl("lblDescription")).Text ==
"Invalid Product")
{
((Label)e.Row.FindControl("lblDescription")).ForeC olor =
System.Drawing.Color.Red;
lblInvalidProduct.Visible = true;
btnNext4.Visible = false;
}
}
}

Please note that i also check to see if the control i am looking for is set
to null to account for rows such as headers etc that don't contain the
control i am looking for.
"Siva" wrote:
Hello
I have a dropdownlist inside the gridview as a template column defined
as follows:
<asp:TemplateField HeaderText="Choose Location">
<ItemTemplate>
<asp:DropDownList ID="ddlChooseLoc" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
I have the gridview inside of a master page- content hierarchy.
I have declared the CodeFileBaseClass in your @ Page directive; I have
also declared a variable of the type dropdownlist in the base class.
When I try to access it from the partial class I am getting a null
value for the dropdownlist variable.
I even tried to a FindControl on the dropdownlist still I am getting a
null.
Any idea what I may be missing?
Thanks
-Siva


Apr 28 '06 #3
OK, lets forget about master page, base class et all. I just created a
simple website in ASP.Net 2.0 with a gridview with a template field
and tried to place a dropdownlist control in it. In my Row_DataBound
method, when I try to access it using FindControl , I am getting a
null.
Here is my aspx page:
*********************************************
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
DataSourceID="XmlDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="location">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1"
runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/App_data/data.xml">
</asp:XmlDataSource>

</div>
</form>
</body>
</html>
***********************************************
Here is my code behind:
*************************************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
DropDownList ddl =
(DropDownList)e.Row.FindControl("DropDownList1");
ArrayList al = new ArrayList();
al.Add("1");
al.Add("2");
ddl.DataSource = al;
ddl.DataBind();
}
}

The line where it does e.Row.FindControl , it returns a null for the
ddl variable.
The same code used to work (of course inside of a datagrid) very well
in asp.net 1.1
Why does this give this error?
What am I missing here?

Any help will be appreciated.

Thanks
-Siva

Apr 28 '06 #4
You will always get null on some rows becasue the control isn't in every row,
for example header rows. If you try to retrieve the control with the
Findcontrol on any rows that don't contain it the it will be set to null so
you will get a run time error if you try to set/get a property or call a
method. The trick is to test to see if it is null first. The below code
will work. and it will work on every line that contains the DDL.

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
DropDownList ddl =
(DropDownList)e.Row.FindControl("DropDownList1");

if ( ddl != null)
{
ArrayList al = new ArrayList();
al.Add("1");
al.Add("2");
ddl.DataSource = al;
ddl.DataBind();
}

}
}

"Siva" wrote:
OK, lets forget about master page, base class et all. I just created a
simple website in ASP.Net 2.0 with a gridview with a template field
and tried to place a dropdownlist control in it. In my Row_DataBound
method, when I try to access it using FindControl , I am getting a
null.
Here is my aspx page:
*********************************************
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
DataSourceID="XmlDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="location">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1"
runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/App_data/data.xml">
</asp:XmlDataSource>

</div>
</form>
</body>
</html>
***********************************************
Here is my code behind:
*************************************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
DropDownList ddl =
(DropDownList)e.Row.FindControl("DropDownList1");
ArrayList al = new ArrayList();
al.Add("1");
al.Add("2");
ddl.DataSource = al;
ddl.DataBind();
}
}

The line where it does e.Row.FindControl , it returns a null for the
ddl variable.
The same code used to work (of course inside of a datagrid) very well
in asp.net 1.1
Why does this give this error?
What am I missing here?

Any help will be appreciated.

Thanks
-Siva

Apr 28 '06 #5
Great. Thanks for the tip. I actually added a line to test if it is a
datarow and only then do a findRow as follows and it works.

if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl =
(DropDownList)e.Row.FindControl("DropDownList1");
......
}

Apr 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: microdevsolutions | last post by:
Hello I am using ASP.NET - I have written a Custom Control (to output some information) that has a public property declared. I have a user control that is a wrapper around the above custom...
1
by: Paul Maidment | last post by:
Hi, I am a bit green when it comes to .net development so please excuse me if this question is obvious or has been asked before... I am trying to programmatically access a label control...
0
by: mike | last post by:
Hi there: I've read an excellent "how to"-article by Microsoft (no. 306227) - partly cited cited at the end of this email). I have implemented the code related to the part "How to Add a...
0
by: mike | last post by:
How do i perform a databind on a web user control within a repeater or rather how can I access the datasource that is already bound? I have a web user control that displays a table of values (the...
3
by: Joe | last post by:
Hello All: I have two webforms (WebForm1.aspx and WebForm2.aspx) that inherit from BasePage.aspx. BasePage.aspx inherits System.Web.UI.Page and has one hidden field (hdnSessionId) that I want...
1
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
0
by: Siva | last post by:
Hello I have a dropdownlist inside the gridview as a template column defined as follows: <asp:TemplateField HeaderText="Choose Location"> <ItemTemplate> <asp:DropDownList ID="ddlChooseLoc"...
4
by: aaragon | last post by:
Hello everyone. I'm reading Andrei's Alexandrescu's book on Modern C++ Design and I decided to implemente in my research code the use of policy classes. My objective is to abstract the storage of...
2
by: Hvid Hat | last post by:
Hi Can anyone help me with the following transition? My problem is how to create a fieldset each time I run into a heading and then include the following text elements within the fieldset. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.