Solved

FindControl in GridView

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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