[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

DropDownList in edit mode in my detailsView

Posted on 2009-04-02
6
Medium Priority
?
2,056 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

650 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