how to populate programmatically a dropdownlist in a template?

Discussion in 'ASP .Net' started by Chris, Jul 28, 2007.

  1. Chris

    Chris Guest

    Hi,

    i defined a dropdownlist in the EditTemplate of a gridview like this:
    <asp:TemplateField HeaderText="min" SortExpression="min">
    <EditItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
    Bind("min") %>' >
    </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%# Bind("min") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    Now i want to populate that dropdownlist programmatically because the values
    go from 0 to 2OO (it would be fastidious to do that manually).
    I tried this in code-behind (vb.net)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load
    Dim gv As GridView
    Dim dd As DropDownList
    Dim z As ListItem
    Dim i As Integer

    gv = form1.FindControl("gridview1")
    dd = gv.FindControl("dropdownlist1")
    If Not Page.IsPostBack Then
    For i = 0 To 200
    z = New ListItem(i, i)
    dd.Items.Add(z)
    Next
    End If
    End Sub

    But this gives this error at runtime: "Object reference not set to an
    instance of an object."
    at line: dd.Items.Add(z)

    What's wrong in my code? I could do this with succes when the dropdownlist
    was just in Form1 and not in a template.
    Thanks for help
    Chris
     
    Chris, Jul 28, 2007
    #1
    1. Advertising

  2. Chris

    Riki Guest

    Chris wrote:
    > Hi,
    >
    > i defined a dropdownlist in the EditTemplate of a gridview like this:
    > <asp:TemplateField HeaderText="min" SortExpression="min">
    > <EditItemTemplate>
    > <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
    > Bind("min") %>' >
    > </asp:DropDownList>
    > </EditItemTemplate>
    > <ItemTemplate>
    > <asp:Label ID="Label2" runat="server" Text='<%# Bind("min")
    > %>'></asp:Label> </ItemTemplate>
    > </asp:TemplateField>
    >
    > Now i want to populate that dropdownlist programmatically because the
    > values go from 0 to 2OO (it would be fastidious to do that manually).
    > I tried this in code-behind (vb.net)
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > Dim gv As GridView
    > Dim dd As DropDownList
    > Dim z As ListItem
    > Dim i As Integer
    >
    > gv = form1.FindControl("gridview1")
    > dd = gv.FindControl("dropdownlist1")
    > If Not Page.IsPostBack Then
    > For i = 0 To 200
    > z = New ListItem(i, i)
    > dd.Items.Add(z)
    > Next
    > End If
    > End Sub
    >
    > But this gives this error at runtime: "Object reference not set to an
    > instance of an object."
    > at line: dd.Items.Add(z)
    >
    > What's wrong in my code? I could do this with succes when the
    > dropdownlist was just in Form1 and not in a template.
    > Thanks for help
    > Chris


    First of all, Page_Load is too early for your purposes.
    At that time, the GridView has not been databound yet, so there even is no
    dropdownlist to fill.
    Therefore, move your code to the GridView1_DataBound event.

    Secondly, the line
    dd = gv.FindControl("dropdownlist1")
    will not work, because the dropdownlist is not right in the gridview, but in
    one of its cells.
    Change it into
    dd = gv.Rows(gv.EditItemIndex).Cells(x).FindControl("dropdownlist1")
    Replace x by the number of the column that's containing your field with the
    dropdownlist.

    --

    Riki
     
    Riki, Jul 28, 2007
    #2
    1. Advertising

  3. Chris

    Chris Guest

    Thanks for replying ...
    I get this error with your code: dd =
    gv.Rows(gv.EditItemIndex).Cells(x).FindControl("dropdownlist1")

    'EditItemIndex' is not a member of 'System.Web.UI.WebControls.GridView'.

    Thanks

    "Riki" <> schreef in bericht
    news:...
    > Chris wrote:
    >> Hi,
    >>
    >> i defined a dropdownlist in the EditTemplate of a gridview like this:
    >> <asp:TemplateField HeaderText="min" SortExpression="min">
    >> <EditItemTemplate>
    >> <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
    >> Bind("min") %>' >
    >> </asp:DropDownList>
    >> </EditItemTemplate>
    >> <ItemTemplate>
    >> <asp:Label ID="Label2" runat="server" Text='<%# Bind("min")
    >> %>'></asp:Label> </ItemTemplate>
    >> </asp:TemplateField>
    >>
    >> Now i want to populate that dropdownlist programmatically because the
    >> values go from 0 to 2OO (it would be fastidious to do that manually).
    >> I tried this in code-behind (vb.net)
    >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles Me.Load
    >> Dim gv As GridView
    >> Dim dd As DropDownList
    >> Dim z As ListItem
    >> Dim i As Integer
    >>
    >> gv = form1.FindControl("gridview1")
    >> dd = gv.FindControl("dropdownlist1")
    >> If Not Page.IsPostBack Then
    >> For i = 0 To 200
    >> z = New ListItem(i, i)
    >> dd.Items.Add(z)
    >> Next
    >> End If
    >> End Sub
    >>
    >> But this gives this error at runtime: "Object reference not set to an
    >> instance of an object."
    >> at line: dd.Items.Add(z)
    >>
    >> What's wrong in my code? I could do this with succes when the
    >> dropdownlist was just in Form1 and not in a template.
    >> Thanks for help
    >> Chris

    >
    > First of all, Page_Load is too early for your purposes.
    > At that time, the GridView has not been databound yet, so there even is no
    > dropdownlist to fill.
    > Therefore, move your code to the GridView1_DataBound event.
    >
    > Secondly, the line
    > dd = gv.FindControl("dropdownlist1")
    > will not work, because the dropdownlist is not right in the gridview, but
    > in one of its cells.
    > Change it into
    > dd = gv.Rows(gv.EditItemIndex).Cells(x).FindControl("dropdownlist1")
    > Replace x by the number of the column that's containing your field with
    > the dropdownlist.
    >
    > --
    >
    > Riki
    >
     
    Chris, Jul 28, 2007
    #3
  4. Chris

    chenhong Guest

    you should populated the dropdownlist in the dropdownlist_load event.
    hope this will help.

    "Chris" <> дÈëÏûÏ¢ÐÂÎÅ:...
    > Hi,
    >
    > i defined a dropdownlist in the EditTemplate of a gridview like this:
    > <asp:TemplateField HeaderText="min" SortExpression="min">
    > <EditItemTemplate>
    > <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
    > Bind("min") %>' >
    > </asp:DropDownList>
    > </EditItemTemplate>
    > <ItemTemplate>
    > <asp:Label ID="Label2" runat="server" Text='<%# Bind("min")
    > %>'></asp:Label>
    > </ItemTemplate>
    > </asp:TemplateField>
    >
    > Now i want to populate that dropdownlist programmatically because the
    > values go from 0 to 2OO (it would be fastidious to do that manually).
    > I tried this in code-behind (vb.net)
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > Dim gv As GridView
    > Dim dd As DropDownList
    > Dim z As ListItem
    > Dim i As Integer
    >
    > gv = form1.FindControl("gridview1")
    > dd = gv.FindControl("dropdownlist1")
    > If Not Page.IsPostBack Then
    > For i = 0 To 200
    > z = New ListItem(i, i)
    > dd.Items.Add(z)
    > Next
    > End If
    > End Sub
    >
    > But this gives this error at runtime: "Object reference not set to an
    > instance of an object."
    > at line: dd.Items.Add(z)
    >
    > What's wrong in my code? I could do this with succes when the dropdownlist
    > was just in Form1 and not in a template.
    > Thanks for help
    > Chris
    >
     
    chenhong, Jul 29, 2007
    #4
  5. Chris

    Chris Guest

    Thanks again, but the dropdownlist is in a templatefield and there is no
    dropdownlist1_onload event ...

    "chenhong" <> schreef in bericht
    news:%...
    > you should populated the dropdownlist in the dropdownlist_load event.
    > hope this will help.
    >
    > "Chris" <>
    > дÈëÏûÏ¢ÐÂÎÅ:...
    >> Hi,
    >>
    >> i defined a dropdownlist in the EditTemplate of a gridview like this:
    >> <asp:TemplateField HeaderText="min" SortExpression="min">
    >> <EditItemTemplate>
    >> <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
    >> Bind("min") %>' >
    >> </asp:DropDownList>
    >> </EditItemTemplate>
    >> <ItemTemplate>
    >> <asp:Label ID="Label2" runat="server" Text='<%# Bind("min")
    >> %>'></asp:Label>
    >> </ItemTemplate>
    >> </asp:TemplateField>
    >>
    >> Now i want to populate that dropdownlist programmatically because the
    >> values go from 0 to 2OO (it would be fastidious to do that manually).
    >> I tried this in code-behind (vb.net)
    >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles Me.Load
    >> Dim gv As GridView
    >> Dim dd As DropDownList
    >> Dim z As ListItem
    >> Dim i As Integer
    >>
    >> gv = form1.FindControl("gridview1")
    >> dd = gv.FindControl("dropdownlist1")
    >> If Not Page.IsPostBack Then
    >> For i = 0 To 200
    >> z = New ListItem(i, i)
    >> dd.Items.Add(z)
    >> Next
    >> End If
    >> End Sub
    >>
    >> But this gives this error at runtime: "Object reference not set to an
    >> instance of an object."
    >> at line: dd.Items.Add(z)
    >>
    >> What's wrong in my code? I could do this with succes when the
    >> dropdownlist was just in Form1 and not in a template.
    >> Thanks for help
    >> Chris
    >>

    >
    >
     
    Chris, Jul 29, 2007
    #5
  6. Chris

    chenhong Guest

    right click the gridview, select "edit template",
    select the column where the dropdownlist exist,
    now you could set the properties and events of the
    dropdownlist


    "Chris" <> дÈëÏûÏ¢ÐÂÎÅ:...
    > Thanks again, but the dropdownlist is in a templatefield and there is no
    > dropdownlist1_onload event ...
    >
    > "chenhong" <> schreef in bericht
    > news:%...
    >> you should populated the dropdownlist in the dropdownlist_load event.
    >> hope this will help.
    >>
    >> "Chris" <> дÈëÏûÏ¢ÐÂÎÅ:...
    >>> Hi,
    >>>
    >>> i defined a dropdownlist in the EditTemplate of a gridview like this:
    >>> <asp:TemplateField HeaderText="min" SortExpression="min">
    >>> <EditItemTemplate>
    >>> <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
    >>> Bind("min") %>' >
    >>> </asp:DropDownList>
    >>> </EditItemTemplate>
    >>> <ItemTemplate>
    >>> <asp:Label ID="Label2" runat="server" Text='<%# Bind("min")
    >>> %>'></asp:Label>
    >>> </ItemTemplate>
    >>> </asp:TemplateField>
    >>>
    >>> Now i want to populate that dropdownlist programmatically because the
    >>> values go from 0 to 2OO (it would be fastidious to do that manually).
    >>> I tried this in code-behind (vb.net)
    >>> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >>> System.EventArgs) Handles Me.Load
    >>> Dim gv As GridView
    >>> Dim dd As DropDownList
    >>> Dim z As ListItem
    >>> Dim i As Integer
    >>>
    >>> gv = form1.FindControl("gridview1")
    >>> dd = gv.FindControl("dropdownlist1")
    >>> If Not Page.IsPostBack Then
    >>> For i = 0 To 200
    >>> z = New ListItem(i, i)
    >>> dd.Items.Add(z)
    >>> Next
    >>> End If
    >>> End Sub
    >>>
    >>> But this gives this error at runtime: "Object reference not set to an
    >>> instance of an object."
    >>> at line: dd.Items.Add(z)
    >>>
    >>> What's wrong in my code? I could do this with succes when the
    >>> dropdownlist was just in Form1 and not in a template.
    >>> Thanks for help
    >>> Chris
    >>>

    >>
    >>

    >
    >
     
    chenhong, Jul 29, 2007
    #6
  7. Chris

    Chris Guest

    Hi again,

    i did what you told me like this:
    Protected Sub DropDownList2_Load(ByVal sender As Object, ByVal e As
    System.EventArgs)
    Dim dd As DropDownList
    Dim gv As GridView
    Dim z As ListItem
    Dim i As Integer
    gv = form1.FindControl("gridview1")
    dd = gv.FindControl("dropdownlist2") 'this must be a problem i think
    If Not Page.IsPostBack Then
    For i = 0 To 200
    z = New ListItem(i, i)
    dd.Items.Add(z)
    Next
    End If
    End Sub

    in aspx i have:
    <asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%#
    Bind("min") %>'
    OnLoad="DropDownList2_Load">
    </asp:DropDownList>

    But this gives (i think because of line: dd =
    gv.FindControl("dropdownlist2")
    "DropDownList2' has a SelectedValue which is invalid because it does not
    exist in the list of items.
    Parameter name: value"

    If i suppress the line: If Not Page.IsPostBack Then
    i get: "Object reference not set to an instance of an object."
     
    Chris, Jul 29, 2007
    #7
  8. Chris

    chenhong Guest

    you should get the reference of DropDownList2
    by converting parameter sender to DropDownList.

    Here is some C# code for you, you could turn it
    into VB code.And make the binding value exist in
    the range of 0 to 200.

    DropDownList ddl =(DropDownList)sender;
    IF (!IsPostBack)
    {
    for(int i=0;i<200;i++)
    ddl.items.Add(new ListItem(i.ToString(),i.ToString());
    }



    "Chris" <> дÈëÏûÏ¢ÐÂÎÅ:%...
    > Hi again,
    >
    > i did what you told me like this:
    > Protected Sub DropDownList2_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs)
    > Dim dd As DropDownList
    > Dim gv As GridView
    > Dim z As ListItem
    > Dim i As Integer
    > gv = form1.FindControl("gridview1")
    > dd = gv.FindControl("dropdownlist2") 'this must be a problem i think
    > If Not Page.IsPostBack Then
    > For i = 0 To 200
    > z = New ListItem(i, i)
    > dd.Items.Add(z)
    > Next
    > End If
    > End Sub
    >
    > in aspx i have:
    > <asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%#
    > Bind("min") %>'
    > OnLoad="DropDownList2_Load">
    > </asp:DropDownList>
    >
    > But this gives (i think because of line: dd =
    > gv.FindControl("dropdownlist2")
    > "DropDownList2' has a SelectedValue which is invalid because it does not
    > exist in the list of items.
    > Parameter name: value"
    >
    > If i suppress the line: If Not Page.IsPostBack Then
    > i get: "Object reference not set to an instance of an object."
    >
    >
     
    chenhong, Jul 30, 2007
    #8
  9. Chris

    Chris Guest

    Thanks, i'll try

    "chenhong" <> schreef in bericht
    news:...
    > you should get the reference of DropDownList2
    > by converting parameter sender to DropDownList.
    >
    > Here is some C# code for you, you could turn it
    > into VB code.And make the binding value exist in
    > the range of 0 to 200.
    >
    > DropDownList ddl =(DropDownList)sender;
    > IF (!IsPostBack)
    > {
    > for(int i=0;i<200;i++)
    > ddl.items.Add(new ListItem(i.ToString(),i.ToString());
    > }
    >
    >
    >
    > "Chris" <>
    > дÈëÏûÏ¢ÐÂÎÅ:%...
    >> Hi again,
    >>
    >> i did what you told me like this:
    >> Protected Sub DropDownList2_Load(ByVal sender As Object, ByVal e As
    >> System.EventArgs)
    >> Dim dd As DropDownList
    >> Dim gv As GridView
    >> Dim z As ListItem
    >> Dim i As Integer
    >> gv = form1.FindControl("gridview1")
    >> dd = gv.FindControl("dropdownlist2") 'this must be a problem i think
    >> If Not Page.IsPostBack Then
    >> For i = 0 To 200
    >> z = New ListItem(i, i)
    >> dd.Items.Add(z)
    >> Next
    >> End If
    >> End Sub
    >>
    >> in aspx i have:
    >> <asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%#
    >> Bind("min") %>'
    >> OnLoad="DropDownList2_Load">
    >> </asp:DropDownList>
    >>
    >> But this gives (i think because of line: dd =
    >> gv.FindControl("dropdownlist2")
    >> "DropDownList2' has a SelectedValue which is invalid because it does not
    >> exist in the list of items.
    >> Parameter name: value"
    >>
    >> If i suppress the line: If Not Page.IsPostBack Then
    >> i get: "Object reference not set to an instance of an object."
    >>
    >>

    >
    >
     
    Chris, Jul 30, 2007
    #9
  10. Chris

    Riki Guest

    Chris wrote:
    > Thanks for replying ...
    > I get this error with your code: dd =
    > gv.Rows(gv.EditItemIndex).Cells(x).FindControl("dropdownlist1")
    >
    > 'EditItemIndex' is not a member of
    > 'System.Web.UI.WebControls.GridView'.


    Sorry, it should be EditIndex.

    --

    Riki
     
    Riki, Jul 30, 2007
    #10
    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. mr2_93
    Replies:
    1
    Views:
    3,600
    mr2_93
    Oct 2, 2005
  2. bsdbsd
    Replies:
    0
    Views:
    935
    bsdbsd
    Oct 5, 2004
  3. Guest
    Replies:
    4
    Views:
    987
    Alexey Smirnov
    Jun 23, 2007
  4. Julia B
    Replies:
    1
    Views:
    882
    Julia B
    Mar 19, 2008
  5. Enjoy Life
    Replies:
    2
    Views:
    277
    Roland Hall
    Feb 23, 2005
Loading...

Share This Page