ASP.Net DataGrid VS. DropDownLists

C

Chris

Hello All,

Need some help here. 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
 
E

Eric Marvets

What you could do is when you set up the dropdownlist, place the selected
value into ViewState, so ViewState(drp.UniqueID.ToString()) =
drp.SelectedValue. Later in your change event, you can use 'lastVal =
ViewState(DirectCast(sender, DropDownList).UniqueID.ToString()). At the end
of the change event after you have done whatever with the last value, set
ViewState(DirectCast(sender, DropDownList).UniqueID.ToString()) =
drp.SelectedValue.

--
Eric Marvets
Principal Consultant

the bang project

<shameless self promotion>

Email (e-mail address removed) for Information on Our Architecture and
Mentoring Services

</shameless self promotion>
 

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,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top