how to populate programmatically a dropdownlist in a template?

C

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
 
R

Riki

Chris said:
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.
 
C

Chris

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
 
C

chenhong

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

Chris

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

chenhong

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
 
C

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."
 
C

chenhong

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());
}
 
C

Chris

Thanks, i'll try

chenhong said:
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());
}
 
R

Riki

Chris said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top