For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > July 2007 > how to populate programmatically a dropdownlist in a template?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to populate programmatically a dropdownlist in a template?
Chris

2007-07-28, 7:09 pm

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


Riki

2007-07-28, 7:09 pm

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

2007-07-28, 7:09 pm

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" <riki@dontnagme.com> schreef in bericht
news:ub0VGsT0HHA.1168@TK2MSFTNGP02.phx.gbl...
> Chris wrote:
>
> 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
>



chenhong

2007-07-28, 10:09 pm

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

"Chris" <gddfd@er.df> 写入消息新闻:us8pViS0HHA.1484@TK2MSFTNGP06.phx.gbl...
> 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

2007-07-29, 4:26 am

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

"chenhong" <chenhong@sinap.ac.cn> schreef in bericht
news:%23BsuAaX0HHA.484@TK2MSFTNGP06.phx.gbl...
> you should populated the dropdownlist in the dropdownlist_load event.
> hope this will help.
>
> "Chris" <gddfd@er.df>
> 写入消息新闻:us8pViS0HHA.1484@TK2MSFTNGP06.phx.gbl...
>
>



chenhong

2007-07-29, 7:09 pm

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" <gddfd@er.df> 写入消息新闻:uZXr7kb0HHA.5160@TK2MSFTNGP05.phx.gbl...
> Thanks again, but the dropdownlist is in a templatefield and there is no
> dropdownlist1_onload event ...
>
> "chenhong" <chenhong@sinap.ac.cn> schreef in bericht
> news:%23BsuAaX0HHA.484@TK2MSFTNGP06.phx.gbl...
>
>



Chris

2007-07-29, 7:09 pm

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

2007-07-29, 7:09 pm

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" <gddfd@er.df> 写入消息新闻:%23Aq26xg0HHA.4236@TK2MSFTNGP06.phx.gbl...
> 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

2007-07-30, 8:09 am

Thanks, i'll try

"chenhong" <chenhong@sinap.ac.cn> schreef in bericht
news:eBwXMoj0HHA.3768@TK2MSFTNGP06.phx.gbl...
> 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" <gddfd@er.df>
> 写入消息新闻:%23Aq26xg0HHA.4236@TK2MSFTNGP06.phx.gbl...
>
>



Riki

2007-07-30, 8:09 am

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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com