ASP.Net DataGrid VS. DropDownLists

C

Chris

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
 
R

Rick Spiewak

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top