Asp DropDownList

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Miguel Dias Moura

    Asp DropDownList

    Hello,

    I have two questions concerning Asp:DropDownLis t

    1. How to fill a DropDownList from runtime and set its default item?

    2. How do use a DropDownList in each datagrid record?
    All dropdownlist should be filled with the values in field NAME from
    the same dataset used in the datagrid.
    The default value of each dropdownlist should be the value of NAME by
    that record.

    Thanks,
    Miguel

  • Ken Cox

    #2
    Re: Asp DropDownList

    Hi Miguel,

    Here's a way to add a dropdownlistbox to a grid and select the corresponding
    item.

    Filling the dropdown could be much more efficient, but this gives you the
    idea.

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]


    <%@ Page Language="VB" %>
    <%@ import Namespace="syst em.data" %>

    <script runat="server">
    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArg s) _
    Handles MyBase.Load
    If Not IsPostBack Then
    DataGrid1.DataS ource = CreateDataSourc e()
    DataGrid1.DataB ind()
    End If

    End Sub
    Private Sub DataGrid1_ItemD ataBound(ByVal sender As Object, _
    ByVal e As System.Web.UI.W ebControls.Data GridItemEventAr gs) _
    Handles DataGrid1.ItemD ataBound
    If (e.Item.ItemTyp e = ListItemType.Al ternatingItem) _
    Or (e.Item.ItemTyp e = ListItemType.It em) Then
    Dim DRV As DataRowView = CType(e.Item.Da taItem, DataRowView)
    Dim itemID As String = DRV("CatID")
    Dim DropDownList1 As DropDownList =
    e.Item.FindCont rol("DropDownLi st1")
    Dim item As ListItem
    DropDownList1.D ataSource = CreateDataSourc e()
    DropDownList1.D ataTextField = "CatName"
    DropDownList1.D ataValueField = "CatID"
    DropDownList1.D ataBind()
    item = DropDownList1.I tems.FindByValu e(itemID)
    If Not item Is Nothing Then item.Selected = True
    End If
    End Sub

    Function CreateDataSourc e() As DataTable
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add( New DataColumn _
    ("CatID", GetType(Int32)) )
    dt.Columns.Add( New DataColumn _
    ("CatName", GetType(String) ))
    dt.Columns.Add( New DataColumn _
    ("CurrencyValue ", GetType(Double) ))
    dt.Columns.Add( New DataColumn _
    ("Boolean", GetType(Boolean )))
    Dim i As Integer
    For i = 0 To 8
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function 'CreateDataSour ce



    </script>

    <html >
    <head runat="server">
    <title>Untitl ed Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    <asp:DataGrid ID=DataGrid1 runat=server>
    <Columns>
    <asp:TemplateCo lumn HeaderText="DDL ">
    <ItemTemplate >
    <asp:DropDownLi st ID="DropDownLis t1" runat="server">
    </asp:DropDownLis t>
    </ItemTemplate>

    </asp:TemplateCol umn>
    </Columns>
    </asp:DataGrid>
    </div>
    </form>
    </body>
    </html>

    "Miguel Dias Moura" <mdmoura*NOSPAM *@gmail.*DELETE 2SEND*com> wrote in
    message news:OgybhYH8FH A.1188@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hello,
    >
    > I have two questions concerning Asp:DropDownLis t
    >
    > 1. How to fill a DropDownList from runtime and set its default item?
    >
    > 2. How do use a DropDownList in each datagrid record?
    > All dropdownlist should be filled with the values in field NAME from the
    > same dataset used in the datagrid.
    > The default value of each dropdownlist should be the value of NAME by
    > that record.
    >
    > Thanks,
    > Miguel
    >[/color]


    Comment

    Working...