Solved

FindControl in GridView

Posted on 2006-07-21
4
2,131 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
Comment Utility
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
Comment Utility
I'll try that.  Thanks.  
0
 
LVL 2

Expert Comment

by:SKumar_1981
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now