Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

how to populate programmatically a dropdownlist in a template?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris - 28 Jul 2007 16:04 GMT
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 - 28 Jul 2007 18:16 GMT
> Hi,
>
[quoted text clipped - 38 lines]
> 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 - 28 Jul 2007 18:55 GMT
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

>> Hi,
>>
[quoted text clipped - 56 lines]
>
> Riki
Riki - 30 Jul 2007 12:25 GMT
> 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.

Signature

Riki

chenhong - 29 Jul 2007 01:22 GMT
you should populated the dropdownlist in the dropdownlist_load event.
hope this will help.

"Chris" <gddfd@er.df> дÈëÏûÏ¢ÐÂÎÅ:us8pViS0HHA.1484@TK2MSFTNGP06.phx.gbl...
> Hi,
>
[quoted text clipped - 39 lines]
> Thanks for help
> Chris
Chris - 29 Jul 2007 09:19 GMT
Thanks again, but the dropdownlist is in a templatefield and there is no
dropdownlist1_onload event ...

> you should populated the dropdownlist in the dropdownlist_load event.
> hope this will help.
[quoted text clipped - 44 lines]
>> Thanks for help
>> Chris
chenhong - 29 Jul 2007 14:45 GMT
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 ...
[quoted text clipped - 46 lines]
>>> Thanks for help
>>> Chris
Chris - 29 Jul 2007 19:15 GMT
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 - 30 Jul 2007 00:41 GMT
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,
>
[quoted text clipped - 29 lines]
> If i suppress the line:  If Not Page.IsPostBack Then
> i get: "Object reference not set to an instance of an object."
Chris - 30 Jul 2007 11:19 GMT
Thanks, i'll try

> you should get the reference of DropDownList2
> by converting parameter sender to DropDownList.
[quoted text clipped - 45 lines]
>> If i suppress the line:  If Not Page.IsPostBack Then
>> i get: "Object reference not set to an instance of an object."

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.