ASP.Net DataGrid VS. DropDownLists

Discussion in 'ASP .Net Datagrid Control' started by Chris, May 23, 2004.

  1. Chris

    Chris Guest

    Hello All,

    Need some help here. I posted this in the framework newsgroup and
    noticed this group so I'm posting it here as well. Sorry for the
    redundancy and/or bad etiquette.

    I'm building an asp.net app that uses a datagrid. I have created a
    template column and added a dropdownlist to it. What I'd like to
    happen is have the database get updated each time the user selects a
    different value from the dropdownlist without having to press a submit
    button or anything. I've been trying to get it to work when the
    SelectedIndexChanged event fires off but with the AutoPostBack = TRUE,
    by the time SelectedIndexChanged runs I've lost my selected value.

    The tricky part is that there really aren't any events associated with
    the dropdownlist when you add them to a template column in a datagrid.
    I've added a handler to the dropdownlist that handles the
    SelectedIndexChanged event.

    I feel like I'm going about this the LONG way. Is there a better way
    to do this? I read about the UpdateCommand as well, should I be using
    that instead?

    I've posted my code below, and added some comments to explain what I'm
    doing.
    Any help is appreciated.

    Chris


    '=============================================================================
    'Sub Page_Load
    '=============================================================================
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim objAsset As New AssetMgmtData
    Dim dtCostCenter As DataTable
    Dim intItems As Integer
    Dim dsDataset As New DataSet

    Try
    If Not (IsPostBack) Then
    dtCostCenter = Cache.Get("dtCostCenter")

    cboCostCenter.DataSource = dtCostCenter
    cboCostCenter.DataBind()

    dsDataset =
    objAsset.InventoryByCostCenter(cboCostCenter.SelectedItem.Text.ToString())
    dgInventory.DataSource = dsDataset
    dgInventory.DataBind()
    End If

    objAsset = Nothing
    dtCostCenter = Nothing
    dsDataset = Nothing

    Catch ex As Exception

    Response.Write(ex.Message)
    objAsset = Nothing
    dtCostCenter = Nothing
    dsDataset = Nothing

    End Try
    End Sub

    '=============================================================================
    'Sub DropDownList_SelectedIndexChanged
    '=============================================================================

    Sub DropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e
    As System.EventArgs)
    'In this sub i'm just trying to capture the dropdownlist's value when
    the user selects a different value.
    Response.Write(sender.selecteditem.value.ToString())
    End Sub

    '=============================================================================
    'Sub dgInventory_ItemCreated
    '=============================================================================

    Private Sub dgInventory_ItemCreated(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    dgInventory.ItemCreated

    'This sub gets fired off when the datagrid is created. This is where
    I'm
    'setting up the dropdownlist values and events. the first time the
    page gets
    'loaded it works, and if I look at the HTML in IE all the
    dropdownlists have
    'the appropriate value (from cell(0)) but once I select something
    different
    'from any of the dropdownlists it doesn't capture what I'm selecting.

    Dim strAssetNumber As String

    If (e.Item.ItemType = ListItemType.Item) Or
    (e.Item.ItemType = ListItemType.AlternatingItem) Then
    Dim cboSelect As DropDownList =
    CType(e.Item.FindControl("cboSelect"), DropDownList)
    e.Item.Cells(0).DataBind()
    cboSelect.Items(0).Value =
    e.Item.Cells(0).Text.ToString()
    cboSelect.Items(1).Value =
    e.Item.Cells(0).Text.ToString()
    cboSelect.Items(2).Value =
    e.Item.Cells(0).Text.ToString()

    AddHandler cboSelect.SelectedIndexChanged, AddressOf
    DropDownList_SelectedIndexChanged

    End If

    End Sub
    Chris, May 23, 2004
    #1
    1. Advertising

  2. Chris

    Rick Spiewak Guest

    I do something similar, but wire the eventhandlers for controls in the item
    being edited during the pageload event instead of the itemcreated event. I'm
    assuming that you only need these for the item being edited:
    If IsPostBack Then
    ' Wireup event handlers for the controls as needed, if editing
    With grdMilestone
    If .EditItemIndex <> -1 Then
    Dim itm As DataGridItem = .Items(.EditItemIndex)
    ' Do your findcontrol, etc. here
    End If
    End With
    Exit Sub
    End If

    "Chris" <> wrote in message
    news:...
    > Hello All,
    >
    > Need some help here. I posted this in the framework newsgroup and
    > noticed this group so I'm posting it here as well. Sorry for the
    > redundancy and/or bad etiquette.
    >
    > I'm building an asp.net app that uses a datagrid. I have created a
    > template column and added a dropdownlist to it. What I'd like to
    > happen is have the database get updated each time the user selects a
    > different value from the dropdownlist without having to press a submit
    > button or anything. I've been trying to get it to work when the
    > SelectedIndexChanged event fires off but with the AutoPostBack = TRUE,
    > by the time SelectedIndexChanged runs I've lost my selected value.
    >
    > The tricky part is that there really aren't any events associated with
    > the dropdownlist when you add them to a template column in a datagrid.
    > I've added a handler to the dropdownlist that handles the
    > SelectedIndexChanged event.
    >
    > I feel like I'm going about this the LONG way. Is there a better way
    > to do this? I read about the UpdateCommand as well, should I be using
    > that instead?
    >
    > I've posted my code below, and added some comments to explain what I'm
    > doing.
    > Any help is appreciated.
    >
    > Chris
    >
    >
    >

    '===========================================================================
    ==
    > 'Sub Page_Load
    >

    '===========================================================================
    ==
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > Dim objAsset As New AssetMgmtData
    > Dim dtCostCenter As DataTable
    > Dim intItems As Integer
    > Dim dsDataset As New DataSet
    >
    > Try
    > If Not (IsPostBack) Then
    > dtCostCenter = Cache.Get("dtCostCenter")
    >
    > cboCostCenter.DataSource = dtCostCenter
    > cboCostCenter.DataBind()
    >
    > dsDataset =
    > objAsset.InventoryByCostCenter(cboCostCenter.SelectedItem.Text.ToString())
    > dgInventory.DataSource = dsDataset
    > dgInventory.DataBind()
    > End If
    >
    > objAsset = Nothing
    > dtCostCenter = Nothing
    > dsDataset = Nothing
    >
    > Catch ex As Exception
    >
    > Response.Write(ex.Message)
    > objAsset = Nothing
    > dtCostCenter = Nothing
    > dsDataset = Nothing
    >
    > End Try
    > End Sub
    >
    >

    '===========================================================================
    ==
    > 'Sub DropDownList_SelectedIndexChanged
    >

    '===========================================================================
    ==
    >
    > Sub DropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e
    > As System.EventArgs)
    > 'In this sub i'm just trying to capture the dropdownlist's value when
    > the user selects a different value.
    > Response.Write(sender.selecteditem.value.ToString())
    > End Sub
    >
    >

    '===========================================================================
    ==
    > 'Sub dgInventory_ItemCreated
    >

    '===========================================================================
    ==
    >
    > Private Sub dgInventory_ItemCreated(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    > dgInventory.ItemCreated
    >
    > 'This sub gets fired off when the datagrid is created. This is where
    > I'm
    > 'setting up the dropdownlist values and events. the first time the
    > page gets
    > 'loaded it works, and if I look at the HTML in IE all the
    > dropdownlists have
    > 'the appropriate value (from cell(0)) but once I select something
    > different
    > 'from any of the dropdownlists it doesn't capture what I'm selecting.
    >
    > Dim strAssetNumber As String
    >
    > If (e.Item.ItemType = ListItemType.Item) Or
    > (e.Item.ItemType = ListItemType.AlternatingItem) Then
    > Dim cboSelect As DropDownList =
    > CType(e.Item.FindControl("cboSelect"), DropDownList)
    > e.Item.Cells(0).DataBind()
    > cboSelect.Items(0).Value =
    > e.Item.Cells(0).Text.ToString()
    > cboSelect.Items(1).Value =
    > e.Item.Cells(0).Text.ToString()
    > cboSelect.Items(2).Value =
    > e.Item.Cells(0).Text.ToString()
    >
    > AddHandler cboSelect.SelectedIndexChanged, AddressOf
    > DropDownList_SelectedIndexChanged
    >
    > End If
    >
    > End Sub
    Rick Spiewak, May 24, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    1
    Views:
    533
    Eric Marvets
    May 23, 2004
  2. dropdownlists in asp.net

    , Mar 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    419
    Nathan Sokalski
    Mar 20, 2006
  3. G-Fit
    Replies:
    6
    Views:
    188
    G-Fit
    Mar 4, 2004
  4. G Stark

    Dynamic DropDownLists in a DataGrid

    G Stark, Oct 15, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    118
    Drew Robbins
    Oct 19, 2004
  5. Dave M
    Replies:
    0
    Views:
    380
    Dave M
    Dec 14, 2004
Loading...

Share This Page