Solved

FindControl in GridView

Posted on 2006-07-21
4
2,134 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 24

Accepted Solution

by:
DBAduck - Ben Miller earned 500 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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