.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

GridView EmptyDataTemplate and FindControl Method

Posted By:      Posted Date: August 23, 2010    Points: 0   Category :ASP.Net

I have 2 textboxes, a dropdownlist and a button in the EmptyDataTemplate of a GridView control and when I click the button I want to get the text from the text boxes and DropDownList put it in the QueryString of a URL i redirect to, but I can't work out why the FindControl method cannot find the controls.  I am using the following code:

Dim ddlNumPpl As DropDownList = GridView1.FindControl("ddlNumPpl")

I get an "Object reference not set to an instance of an object" error though when I try to refer to the SelectedValue property of it though.  I would be very grateful if someone could let me know why the FindControl method can't find the control and what I should do to rectify it.

Many Thanks (in advance),


View Complete Post

More Related Resource Links

findcontrol problem in a gridview

Hey guys I've got a gridview with a few textboxes in each row which I want to insert into a database, after a user clicks a submit button (thats outside of the gridview)  This is my code for the gridview<asp:GridView ID="GridView2" AutoGenerateColumns="false" runat="server" EnableModelValidation="True"> <Columns> <asp:TemplateField > <ItemTemplate> name: <asp:Label ID="Label12" runat="server" Text='<%# Bind("PlayN") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:TextBox ID="TxtFirst" runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:TextBox ID="txtLast" runat="server"></asp:TextBox> </ItemTemplate>

Why it returns null (GridView)TemplateContainer.FindControl("gridView")?



I created a new custom field definition.

But on CreateChildControls event it always return null in my components. Where is error? What should I do to make work it properly?


protected override void CreateChildControls()


if (this.Field == null) return;


if (this.Field != null && this.ControlMode!= SPControlMode.Display)

this.MyPrefix = (Label)TemplateContainer.FindControl("MyPrefix");
if (this.MyPrefix == null)
    throw new ArgumentException("Corrupted PermissionControl template - missing ProofHQUserPermissionPrefix.");
this.gridView = (GridView)TemplateContainer.FindControl("gridView");
if (this.gridView == null)
    throw new ArgumentException("Corrupted PermissionControl template - missing GridView.");



All components are defined in template PermissionControl.

GridView and FindControl issue..


Hi guys,

I dont seem to find any control using the FindControl method over gridviews "on edit". I have pasted my code below, please tell me if m doing something wrong or missing something..

ASPX page:

<asp:GridView ID="gvCountry" runat="server" AutoGenerateColumns="False" 
                <asp:TemplateField HeaderText="Edit">
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                            CommandName="Update" Text="Update"></asp:LinkButton>
                         <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                            CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 

How to access multiple header rows of the gridview in Render method


I have extended a gridview to add an additional header with following two hyperlink controls "Select All" and "Clear All". These will operate on checkboxes in the data rows of the grid. I hide the column headers as I only want to show one column with checkboxes and "Select/Clear All" links. Everything is working as expected. 

Now, I wanted to add a scrollbar to my grid control, I did add the scroll bar using div but what it does is, it includes the "Select All and Clear All" links aswell. I only need to add the scrollbar to datarows and not to the header.

I am trying to extend my control to include hte scrollbar by adding div during Render function. But how will I determine or loop through header rows? How will i get the header row with hyperlinks that I created above?? In Render if I do this.HeaderRow, it gets the original column header and not the custom header. Please help!!!

Gridview emptydatatemplate problem


Hiiii Coders,

I m working on an application. Initially when the page load, my gridview is empty. I have taken an emptydatatemplate for holding controls like textboxes and checkboxes. I have a button b1 on my page. I want that whenever user click on it, data should be inserted in sql server.

After that, the record i have inserted will be shown in gridview. In this case i m inserting data from my footer of gridview.


I m using

  TextBox part_no = GridView1.Controls[0].Controls[0].FindControl("Nodatapartno") as TextBox;

TextBox part_no = GridView1.FooterRow.FindControl("partno") as TextBox;

above code to access the value of emptydatatemplate controls and footertemplate controls.

My problem is that when gridview is empty then on clicking button it should insert the values of controls in emptydatatemplate and

when gridview has records then it should insert the values of my controls in footer template 




OnPreRender method of GridView control


I was using a reflector and came up on this method OnPreRender() of GridView.

protected internal override void OnPreRender(EventArgs e) 
   if (this.DetermineRenderClientScript() && (this.Page != null)) 
      clientScript.RegisterClientScriptResource(typeof(GridView), "GridView.js"); 

Its calling JavaScript (GridView.js).

Can any one explain the usage of this .js file.

Can we override the file prior rendering of GridView

gridview cell value via FindControl......


I have a page that is using an unbound gridview, that has a mixture of template based columns (drop down list, text boxes, etc).  I've tried the following syntax to retrieve the values currently in the cells.


 TextBox1.Text = grdDataEntry.Rows(1).FindControl("mydropdownlist").ToString        

TextBox2.Text   = grdDataEntry.Rows(1).FindControl("mytextbox").ToString


What am I missing???

Inplace Edit in GridView

In this article I am presenting a different way to look at EditItemTemplate, using which user navigation for editing is reduced and . it looks very different (see screeen shots) then regular implementation of GridView.

GridView Confirmation Box Using JQuery BlockUI in asp.net

JQuery is the most popular JavaScript library. One of the main reasons for its fame is the plugin architecture. Anyone can create a separate library that leverage the JQuery library. In this article we are going to look at the BlockUI library which is used to block the user screen while performing a task.

Thinking about new C# method prototypes: object as dictionary

I recently had to write a small Flickr API. I know many .Net API for Flickr already exist but I needed one for a Silverlight application. Whatever, it's only about building some querystrings so I did it by myself. It's been an opportunity to think again about a classical question: how to pass parameters to a method ?

Imagine you have a generic method to call some Flickr functions.

Textbox Editable GridView in ASP.NET

This article describes how I made a fully editable gridview in C# using ASP.NET 2. The idea is that the GridView looks and works like an Excel spreadsheet. You see all the cells in the table, and you can edit any of the cells you like, and they are automatically updated (i.e. saved to the database). This is very often how people expect data tables to work in web pages, and I've often seen people clicking in vain on un-editable gridview cells, somehow expecting to edit them, and not understanding that they need to click an Edit button at the end of the row.

Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality

In my previous examples, I have demonstrated on how to add dynamic rows in GridView control with TextBoxes and how to save the values into the database. Now, seems that most of the developers are asking if how to add a delete functionality with it. So in this example, I'm going to show on how to delete a certain row in the dynamic GridView with TextBoxes.

AJAX GridView Checkbox Select All in ASP.Net

In the previous article GridView Select Multiple Rows you learnt the multiple row selection in GridView control. Here you will get the C# code sample to select all the gridview rows using single checkbox server control. You can highlight the selected rows with different color and Font state Bold. If you want to use the selection values on different ASP.Net

AJAX GridView Paging and Sorting using C# in ASP.Net

To enable the GridView Paging and Sorting to update the GridView Data without refreshing the ASP.Net web page AJAX Extension controls are used. UpdatePanel is used to hold the GridView control that triggers the events for the controls placed inside it. By Default UpdatePanel triggers the asynchronous postbacks for its child controls.

GridView Paging Using Ajax in ASP.Net

To enable the GridView Paging and Sorting to update the GridView Data without refreshing the ASP.Net web page AJAX Extension controls are used. UpdatePanel is used to hold the GridView control that triggers the events for the controls placed inside it. By Default UpdatePanel triggers the asynchronous postbacks for its child controls.

Displaying Row Details Tooltip on GridView using JQuery

Continuing exploring JQuery! I decided to implement a feature to display some kind of details related to a row displayed on GridView. For example when displaying employees details on GridView some information might not fit in the GridView because it will make it huge and wide. These infomration can be diplayed as Tooltip. Or when you want to display a picture and some kind of formated text to be diplayed as tooltip when mouse hover on the image.

jQuery: Highlight Gridview Rows with Checkbox

Im learning and experimenting with jQuery and ASP.NET. Below is a simple solution I created to highlight rows in a gridview by clicking on a checkbox next to the row.

Gridview setup:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend