Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 6th, 2005, 06:33 AM
Registered User
 
Join Date: Apr 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Losing my MIND!!!

Hi Everyone. I am recent convert from ASP to ASP.NET and I am currently working on a project which is sending me cRaZy. I am currently making some code changes to an ASP.Net app that users a data grid. Basically I am having issues effectively accessing and populating a dropdownlist box control that is within a TemplateColumn within a DataGrid.

Currently on an ASPX page when a user clicks on the Edit button the following subroutine is called. This subroutine trys to populate a DropDownList. Everytime I try to binddata i.e. populate the drop down list box I get an error telling me there is no instance of the referencing object (or something like that). Any help would be greatly appreciated. The error is definately related to way I am trying to access and populate the datagrid. It is as if the object is not in scope.

    Sub dgrDB_Edit(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)

        dgrDB.EditItemIndex = CInt(E.Item.ItemIndex)

        ''''''
        'Get MU Groups
        ''''''
        Dim MyConn As SqlConnection
        Dim strSQL As String
        Dim MyComm As SqlCommand
        Dim myReader As SqlDataReader

        MyConn = New SqlConnection(ConfigurationSettings.AppSettings("S qlConn.ConnectionString"))
        strSQL = "SELECT * from blah"
        MyComm = New SqlCommand(strSQL, MyConn)
        MyConn.Open()
        myReader = MyComm.ExecuteReader()

        editMUgroup = CType(dgrDB.Items(dgrDB.EditItemIndex).FindControl ("editMUgroup"), DropDownList)
' also tried
'editMUgroup = dgrDB.Items(dgrDB.EditItemIndex).FindControl("edit MUgroup")

        If myReader.HasRows = True Then
            editMUgroup.DataSource = myReader
            editMUgroup.DataBind()
            'editMUgroup.SelectedIndex = dgrDB.Items.IndexOf(dgrDB.Items.FindByValue(E.Item .Cells(MUDistGpID).Text))
        End If

        MyConn.Close()
        ''''''
        BindGrid()

    End Sub


Any help would be greatly appreciated!

 
Old July 6th, 2005, 01:48 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

  editMUgroup = CType(dgrDB.Items(dgrDB.EditItemIndex).FindControl ("editMUgroup"), DropDownList)


You are trying to find an object and assign it to itself

you need to do somthing like

dim edMUGroup as new dropdownlist
edMUGroup= = CType(dgrDB.Items(dgrDB.EditItemIndex).FindControl ("editMUgroup"), DropDownList)


 
Old July 6th, 2005, 05:42 PM
Registered User
 
Join Date: Apr 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

JB thanks for the reply.

What you are saying does make sense and I will give it a try.
Any reason why the following should not have worked - I had tried this early in the game?

CType(dgrDB.Items(dgrDB.EditItemIndex).FindControl ("editMUgroup"), DropDownList).DataSource = myReader

CType(dgrDB.Items(dgrDB.EditItemIndex).FindControl ("editMUgroup"), DropDownList).DataBind()

Thanks in advance
 
Old July 8th, 2005, 08:26 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

1) There is no reason that the following wouldn't work:

 editMUgroup = CType(dgrDB.Items(dgrDB.EditItemIndex).FindControl ("editMUgroup"), DropDownList)

When you declare a control (even with an ID) in a repeating construct such as a grid column template the control will not be instantiated against a class level member field ("Protected WithEvents editMUgroup As ..."). So you could therefore retrieve it from the data grid item's controls collection. Also, you could retrieve it to a server-side variable by the same name and you shouldn't have a problem. While this is technically feasible, I'd recommend varying the names just to avoid potential conflicts in declaration instances.

2) I suspect the core of the problem here is that you are binding the grid after you bind the drop down list. Because the drop down list is in the column template of the grid, it will get created from scratch when the grid is bound (which will discard your bound data). Therefore you need to bind the grid first. You need to write a handler for the ItemDataBound event of the grid where you can check for the item being edited (e.Item.ItemType = Edit) then locate the DDL control and bind that.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Mind map with Ajax TIA555 Ajax 0 June 28th, 2008 01:45 PM
Drop Down List losing value greenspacechunks ASP.NET 2.0 Basics 5 November 29th, 2007 09:44 PM
Losing ODBC connection kornshell SQL Server ASP 0 June 18th, 2007 09:52 AM
Losing variables bluemat XSLT 2 October 16th, 2004 01:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.