Web Analytics Made Easy -
StatCounter Populating a dropdown list in a EditItemTemplate - CodingForum

Announcement

Collapse
No announcement yet.

Populating a dropdown list in a EditItemTemplate

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Populating a dropdown list in a EditItemTemplate

    Hi,

    How do I populate a DropDownList that is within a EditItemTemplate? I have tried to use the following code -
    PayBandDropDownList.items.insert(0,"A Test Item")


    But I get a NullException error. Basically I want to add a list of items that are from a database. I can't hard code these items in my asp page using "<asp:ListItem>" since these items may change in the database at a later date.

    Here is a *some* of my code:


    <asp:TemplateColumn HeaderText="Pay Band">
    <ItemTemplate>
    <asp:Label runat="server" Width="100"
    Text='<%# DataBinder.Eval(Container.DataItem, "PayBand") %>'/>
    </ItemTemplate>
    <EditItemTemplate>
    <aspropDownList runat="server"
    SelectedIndex='<%# GetPayBandIndex(Container.DataItem("PayBand")) %>'
    id="edit_payband">

    (These hard coded list items would be removed.)

    <asp:listitem>BAND1</asp:listitem> <asp:listitem>BAND2</asp:listitem>
    <asp:listitem>BAND3</asp:listitem>
    <asp:listitem>BAND4</asp:listitem>
    <asp:listitem>BAND5</asp:listitem>
    <asp:listitem>BAND6</asp:listitem>
    <asp:listitem>BAND7</asp:listitem>
    <asp:listitem>BAND8</asp:listitem>
    <asp:listitem>BAND9</asp:listitem>
    <asp:listitem>BNDC1</asp:listitem>
    <asp:listitem>BNDC2</asp:listitem>
    <asp:listitem>BNDC3</asp:listitem>
    <asp:listitem>BNDC4</asp:listitem>
    <asp:listitem>BNDC5</asp:listitem>
    <asp:listitem>BNDC6</asp:listitem>
    </asp:dropdownlist>
    </EditItemTemplate>
    </asp:TemplateColumn>


    Public Function GetPayBandIndex(PayBand As String) As Integer
    If PayBandIndex(PayBand) <> Nothing
    Return CInt(PayBandIndex(PayBand))
    Else
    Return 0
    End If
    End Function

    Sub emp_list_EditCommand(s As Object, e As DataGridCommandEventArgs)

    emp_list.EditItemIndex = e.Item.ItemIndex

    Dim PayBandDropDownList As DropDownList = e.Item.FindControl("edit_payband")

    PayBandDropDownList.items.insert(0,"A Test Item")


    BindData
    End Sub


    Any suggestions? Thanks.

  • #2
    Code:
    Dim myDs as DataSet = New DataSet()
    
    Function getmyDDL() as DataSet
    		myDs = populate the dataset
    		Return myDs 
    End Function
    
    Function GetSelectedIndex(id as integer) as Integer
    		Dim iLoop as Integer
    		Dim dt as DataTable
    		dt = myDs.Tables(0)
    		For iLoop = 0 to dt.Rows.Count - 1
    			If Int32.Parse(id) = Int32.Parse(dt.Rows(iLoop)("dbIdfield")) Then
    			     Return iLoop
    		         End If
    		Next iLoop
    End Function
    ........
    
    <EditItemTemplate>
    	<asp:DropDownList runat="server" id="myDDL"
    		DataValueField="fieldfromdb" 
                    DataTextField="fieldfromdb"
    		DataSource='<%# getmyDDL() %>'
                    SelectedIndex='<%#
                GetSelectedIndex(DataBinder.Eval(Container.DataItem,"id"))  %>' />
    </EditItemTemplate>
    I used a dataset above but you can use whatever datsource you would like. I believe I initially found this a while back on 4guysfromrolla.
    does this sig match?

    Comment


    • #3
      Thanks for that. The DataSource = '<%# arraylist %>' is what I needed. That did the trick! Thanks.

      Comment

      Working...
      X