problem with populating a dropdownlist in editTemplate of detailsview

J

Jan

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
 
G

Guest

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
 
J

Jan

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



......
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top