?
Solved

DropDownList in edit mode in my detailsView

Posted on 2009-04-02
6
Medium Priority
?
2,050 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 2000 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 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