Solved

FindControl in GridView

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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