[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

FindControl in GridView

Posted on 2006-07-21
4
Medium Priority
?
2,141 Views
Last Modified: 2010-05-18
I have a DropDownList in the EditItemTemplate of a Gridview Column and need to retrieve its SelectedValue.  My code is throwing the
following error:  "Object reference not set to an instance of an object"

heres what I have:

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = new DropDownList();
        string str;
        ddl = (DropDownList)(GridView1.FindControl("DropDownList1"));
        str = ddl.SelectedValue.ToString();  // this line throws the error
}

See anything wrong here?  I'm banging my head!



0
Comment
Question by:BoggyBayouBoy
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
DBAduck - Ben Miller earned 2000 total points
ID: 17158667
It is more about that you will not trap the SelectedIndexChanged event of the Dropdownlist because you are in an EditItemTemplate.

You will want to trap the EditItem event in the Gridview and then trap the GridViewEditEventHandler.

Details are here:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewediteventhandler.aspx

Then you can use the event args to find the right index and then do a FindControl like

DropDownList ddl = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1");
get the ddl.SelectedValue.

Hope that makes sense!

Ben.
0
 
LVL 1

Author Comment

by:BoggyBayouBoy
ID: 17158673
I'll try that.  Thanks.  
0
 
LVL 2

Expert Comment

by:SKumar_1981
ID: 17158864
Try this

Dim grdCellData As DataGridItem
Dim ddl As System.Web.UI.WebControls.DropDownList
ddl = grdCellData.FindControl("ddl ")

regards,
skumar
0
 
LVL 1

Author Comment

by:BoggyBayouBoy
ID: 17162922
Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month18 days, 17 hours left to enroll

834 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