Solved

DropDownList in edit mode in my detailsView

Posted on 2009-04-02
6
2,040 Views
Last Modified: 2013-11-07
Hey guys,
So I have detailsView that contains a single match information for a game.
The information in the detailsView are things such as: date and time of the game, opponent, points in favor, points agains, etc.
What I want to do is that when I click the edit button, a dropdown listing of all the opponents becomes available (obviously the selected item at the time of the update should be the opponent that was originally saved).

I can achieve this with no probelem using a sqlDatasource right on the .aspx page, but I will not like to that because I've already have function in a class that gets me that information. I will like to bind the dropdownlist from the code behind...I've tried to use the "findControl" function but that's not working... I'm getting the followin error:

Object reference not set to an instance of an object.

I'll attach the code.
I left commented out the code that I'm trying to use.

Thanks so much! this is been killing me for 2 days now :(
detailsView
===========
<asp:DetailsView ID="gamesDV" runat="server" AutoGenerateRows="false">
    <Fields>
        <asp:BoundField DataField="dtmGameStart" HeaderText="Game Date" />
        <asp:TemplateField HeaderText="Opponent">
            <ItemTemplate>
                <asp:Label ID="lblOpponentName" runat="server" Text='<%# Bind("strOpponentName") %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <%--<asp:DropDownList ID="ddOpponent" DataTextField="strOpponentName" DataValueField="intOpponentID" SelectedValue='<%# Bind("intOpponentID") %>' runat="server"/>--%>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="intPointsFavor" HeaderText="Points Favor" />
        <asp:BoundField DataField="intPointsAgainst" HeaderText="Points Against" />
        <asp:BoundField DataField="fIsHome" HeaderText="Home?" />
        <asp:BoundField DataField="strOpponentURL" HeaderText="Opponent Website" />
        <asp:BoundField DataField="strGameType" HeaderText="Game Type" />
        <asp:CommandField ShowEditButton="True" />
    </Fields>
    <HeaderTemplate>
        MBRFC Vs. <%#Eval("strOpponentName") %>
    </HeaderTemplate>
</asp:DetailsView>
 
 
==========
CODE BEHIND
===========
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
 
            BindDetails()
            
        End If
    End Sub
 
    Private Sub BindDetails()
        gamesDV.DataSource = schedule.GetGameByID(Request.QueryString("intGameID"))
 
        'Dim ddOpponent As DropDownList = DirectCast((gamesDV.FindControl("ddOpponent")), DropDownList)
        'ddOpponent.DataSource = teamLibrary.GetOpponents()
        'ddOpponent.DataBind()
 
        gamesDV.DataBind()
    End Sub
 
    Protected Sub gamesDV_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs) Handles gamesDV.ModeChanging
        gamesDV.ChangeMode(e.NewMode)
        BindDetails()
    End Sub

Open in new window

0
Comment
Question by:vthunder70
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:HarryNS
ID: 24057095
Once this this page loads in IE, right click -> view source. Check what is the client ID of the control you are looking for. Take that ID and use it in your code. Easy way to overcome your issue.
0
 
LVL 15

Accepted Solution

by:
NazoUK earned 500 total points
ID: 24057625
Your find control comes up with this error because it tries to find the drop down list regardless of what mode the detailsview is in. If the DV is not in edit mode and then the dropdown list does not exist and you get the null reference exception.
You could put the code that binds the data to the drop down list inside a If ddOpponent ISNot Nothing condition.

Also you mention the sqldatasource, there is an objectdatasource that binds to business object methods so you can bind the getOpponents method in the same way you would an sql statement.
0
 

Author Comment

by:vthunder70
ID: 24065057
Still giving me an error of:

Object reference not set to an instance of an object.

I guess I don't know under what even handler should I paste the code. right now I have the code inside the gamesDV_modeChanging event... I'll paste the code

Txs!
    Protected Sub gamesDV_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs) Handles gamesDV.ModeChanging
        gamesDV.ChangeMode(e.NewMode)
 
        Dim ddOpponent As DropDownList = DirectCast((gamesDV.FindControl("ddOpponent")), DropDownList)
        If ddOpponent Is Nothing Then
            ddOpponent.DataSource = teamLibrary.GetOpponents()
            ddOpponent.DataBind()
        End If
 
        BindDetails()
    End Sub

Open in new window

0
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!

 

Author Comment

by:vthunder70
ID: 24067486
Hi guys,

I could really use some help with this one please!

txs!
0
 
LVL 15

Expert Comment

by:NazoUK
ID: 24070761
Hi, sorry for the delay getting back to you.
I've done some testing with the code you supplied, the problem is that the dropdownlist has not been created yet in the modechanging event so you can't access it. DetailsView has a modechanged event that is supposed to fire after the mode has been changed but that doesn't seem to fire if you are databinding manually.

Anyway, the solution I managed to find was to handle the gamesDV.DataBound event.

I strongly recommend you look up how to use the ObjectDataSource though, it makes these kind of situations much simpler.
Protected Sub gamesDV_DataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs) Handles gamesDV.DataBound
    If gamesDV.CurrentMode<>DetailsViewMode.ReadOnly Then 
        Dim ddOpponent As DropDownList = DirectCast((gamesDV.FindControl("ddOpponent")), DropDownList)
        If ddOpponent IsNot Nothing Then
            ddOpponent.DataSource = teamLibrary.GetOpponents()
            ddOpponent.DataBind()
        End If
    End If
End Sub

Open in new window

0
 

Author Closing Comment

by:vthunder70
ID: 31566058
Sorry for completing not getting back to this question. I recently got laid off from my job so things haven't been to good.
I did use the objectDataSource adn it worked great! didn't try the lastes suggestion since I lost my job =(

Thank you very much
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

730 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