Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 30th, 2007, 08:51 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with dropdownlist in editItemTemplate

Hi,

i want to populate a dropdownlist with code-behind. The dropdownlist is wrapped into an editItemTemplate of a gridview like this:

<asp:TemplateField HeaderText="min" SortExpression="min">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("min") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("min") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

The code-behind (not finished) is:
Protected Sub DropDownList2_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim dd As DropDownList
        Dim z As ListItem
        Dim i As Integer
        dd = ?? (sender ...) 'how to refer to drpdownlist2?
        'sender =
        If Not Page.IsPostBack Then
            For i = 0 To 200
                z = New ListItem(i, i)
                dd.Items.Add(z)
            Next
        End If
    End Sub

My problem is: how to reference dropdownlist2?
I can't use this: dd = gridview1.FindControl("dropdownlist2")
because the dropdownlist is actually not in the gridview, but in one of its cells.
I think i should use the parameter 'sender', but how?
Thanks for help
H.


 
Old July 30th, 2007, 09:04 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Usually I would do this by creating a handler for the gridview ItemDataBound event. Then you can use code like this:

If e.Row.RowType == DataRow Then 'need to filter out the header/footer/pager rows
   Dim ddl As DropDownList = CType(e.Row.FindControl("DropDownList2"), DropDownList)
   'Populate the DDL using the "ddl" variable
End If

The ItemDataBound handler will be called for each item in the gridview. "e" is the gridview event args. Then you can find the control in the row (e.Row) by it's ID and you'll be able to manipulate each instance of the DDL for each grid row.

-Peter
 
Old July 31st, 2007, 03:30 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, thanks for replying.

I tried your way, but there is then a problem with the selectedvalue:
"'DropDownList2' has a SelectedValue which is invalid because it does not exist in the list of items."
Parameter name: value
I tried with both "GridView1_RowDataBound" and "GridView1_DataBound".
I think those events come to early, before the listitem is populated.

 
Old July 31st, 2007, 04:53 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Instead of using the databinding syntax to set the selected value, set it in the RowDataBound event handler after you databind the DDL. That way you create the list of items, then set the selected one.

-Peter
 
Old July 31st, 2007, 05:56 PM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, thanks
i'll try






Similar Threads
Thread Thread Starter Forum Replies Last Post
Gridview bound dropdownlist edititemtemplate Bushwhacker ASP.NET 2.0 Basics 1 March 15th, 2008 07:35 PM
strange problem with EditItemTemplate hertendreef ASP.NET 2.0 Professional 0 August 17th, 2006 08:44 AM
Gridview - EditItemTemplate DropDownList issue Break40 ASP.NET 2.0 Basics 0 June 21st, 2006 08:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.