problem with populating a dropdownlist in editTemplate of detailsview

Discussion in 'ASP .Net' started by Jan, Aug 11, 2007.

  1. Jan

    Jan Guest

    Hi,

    i defined a dropdownlist in an EditTemplate of a detailsview like this:

    <EditItemTemplate>
    <asp:DropDownList ID="drop1" runat="server"></asp:DropDownList>
    </EditItemTemplate>

    I want to populate it in code-behind; i tried this:

    Protected Sub DetailsView1_ItemCreated(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles DetailsView1.ItemCreated
    If DataControlRowState.Edit Then 'only in edit mode
    If DataControlRowType.DataRow Then 'only datarow
    Dim dd1 As DropDownList
    Dim z1 As ListItem
    Dim i As Integer
    dd1 = CType(DetailsView1.Rows(5).FindControl("drop1"),
    DropDownList)
    For i = 0 To 20
    z1 = New ListItem(i, i)
    dd1.Items.Add(z1)
    Next
    End If
    End If
    End Sub

    But i get the error:"Object reference not set to an instance of an object"
    at line: dd1.Items.Add(z1)

    Thanks for help
    Jan
    Jan, Aug 11, 2007
    #1
    1. Advertising

  2. RE: problem with populating a dropdownlist in editTemplate of detailsv

    Jan,

    Seems your dropdownlist is not plaved in the 5th row (remember in this case
    rows are numbered starting from zero). Anyway, you don't have to use
    row(index), just try DetailsView1.FindControl("drop1"). If this doesn't work,
    try to debug this line of the code and find the control location (ie. by
    traversing the control tree within the with Quick View window).

    HTH
    --
    Milosz


    "Jan" wrote:

    > Hi,
    >
    > i defined a dropdownlist in an EditTemplate of a detailsview like this:
    >
    > <EditItemTemplate>
    > <asp:DropDownList ID="drop1" runat="server"></asp:DropDownList>
    > </EditItemTemplate>
    >
    > I want to populate it in code-behind; i tried this:
    >
    > Protected Sub DetailsView1_ItemCreated(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles DetailsView1.ItemCreated
    > If DataControlRowState.Edit Then 'only in edit mode
    > If DataControlRowType.DataRow Then 'only datarow
    > Dim dd1 As DropDownList
    > Dim z1 As ListItem
    > Dim i As Integer
    > dd1 = CType(DetailsView1.Rows(5).FindControl("drop1"),
    > DropDownList)
    > For i = 0 To 20
    > z1 = New ListItem(i, i)
    > dd1.Items.Add(z1)
    > Next
    > End If
    > End If
    > End Sub
    >
    > But i get the error:"Object reference not set to an instance of an object"
    > at line: dd1.Items.Add(z1)
    >
    > Thanks for help
    > Jan
    >
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Aug 12, 2007
    #2
    1. Advertising

  3. Jan

    Jan Guest

    Re: problem with populating a dropdownlist in editTemplate of detailsv

    Ji Milosz, thanks for replying.

    I tried DetailsView1.FindControl("drop1") but still same error ...
    I use VWD (Visual web dev.) and i don't understand what you mean with:"try
    to debug this line of the code and find the control location (ie. by
    traversing the control tree within the with Quick View window)."

    I get the error even in normal mode. Maybe are these lines not correct:
    If DataControlRowState.Edit Then

    If DataControlRowType.DataRow Then



    ......


    "Milosz Skalecki [MCAD]" <> schreef in bericht
    news:...
    > Jan,
    >
    > Seems your dropdownlist is not plaved in the 5th row (remember in this
    > case
    > rows are numbered starting from zero). Anyway, you don't have to use
    > row(index), just try DetailsView1.FindControl("drop1"). If this doesn't
    > work,
    > try to debug this line of the code and find the control location (ie. by
    > traversing the control tree within the with Quick View window).
    >
    > HTH
    > --
    > Milosz
    >
    >
    > "Jan" wrote:
    >
    >> Hi,
    >>
    >> i defined a dropdownlist in an EditTemplate of a detailsview like this:
    >>
    >> <EditItemTemplate>
    >> <asp:DropDownList ID="drop1" runat="server"></asp:DropDownList>
    >> </EditItemTemplate>
    >>
    >> I want to populate it in code-behind; i tried this:
    >>
    >> Protected Sub DetailsView1_ItemCreated(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles DetailsView1.ItemCreated
    >> If DataControlRowState.Edit Then 'only in edit mode
    >> If DataControlRowType.DataRow Then 'only datarow
    >> Dim dd1 As DropDownList
    >> Dim z1 As ListItem
    >> Dim i As Integer
    >> dd1 = CType(DetailsView1.Rows(5).FindControl("drop1"),
    >> DropDownList)
    >> For i = 0 To 20
    >> z1 = New ListItem(i, i)
    >> dd1.Items.Add(z1)
    >> Next
    >> End If
    >> End If
    >> End Sub
    >>
    >> But i get the error:"Object reference not set to an instance of an
    >> object"
    >> at line: dd1.Items.Add(z1)
    >>
    >> Thanks for help
    >> Jan
    >>
    >>
    >>
    Jan, Aug 12, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yan Wang

    DropdownList in EditTemplate

    Yan Wang, May 23, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,287
    Natty Gur
    May 24, 2004
  2. Krish

    EditTemplate - Dropdownlist

    Krish, Sep 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    558
    Krish
    Sep 21, 2005
  3. Replies:
    0
    Views:
    139
  4. ES
    Replies:
    0
    Views:
    928
  5. Joe Reiss

    Formview EditTemplate Dropdownlist

    Joe Reiss, Feb 20, 2007, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    135
    Joe Reiss
    Feb 20, 2007
Loading...

Share This Page