Solved

DropDownList in edit mode in my detailsView

Posted on 2009-04-02
6
2,027 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
  • 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now