Solved

Loop through rows to get control value within nested gridview ASP.NET 2.0

Posted on 2007-11-30
1
7,677 Views
Last Modified: 2013-11-26
Scenario: Gridview with employee, nested gridview with tasks that require completion, an item template within nested gridview that has a dropdown box where the user selects whether the task is completed, in process, not started, etc. An update button in the employee item tempate field (in same cell as nested gridview) that houses the nested gridview. When the update button is clicked, I want to iterate through the nested gridview and get the new value of the dropdownlist and send it to the database.

 Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gvInside As GridView = CType(FindControl("gvTasks"), GridView)
        Dim iStatus As Integer
        Dim StatusBox As DropDownList
        For Each row As GridViewRow In gvInside.Rows
            StatusBox = CType(FindControl("ddCompletionStatus"), DropDownList)
            iStatus = StatusBox.SelectedValue
        Next
    End Sub
The code above generates the following error: System.NullReferenceException was unhandled by user code   Message="Object reference not set to an instance of an object."

Any help is appreciated. Thank you!
 
0
Comment
Question by:cmcormick
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 20382269
You need to use FindControl with the Row.  You would need to find out what the current row for the outer grid is, and then get a reference to the inside GridView.

One possibility:




Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gvInside As GridView = CType(gvOutSide.Rows(0).FindControl("gvTasks"), GridView)
        Dim iStatus As Integer
        
        For Each row As GridViewRow In gvInside.Rows
            Dim StatusBox As DropDownList = CType(row.FindControl("ddCompletionStatus"), DropDownList)
            iStatus = StatusBox.SelectedValue
        Next
    End Sub

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

778 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