473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ASP.Net DataGrid VS. DropDownLists

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.Selec tedItem.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
Nov 18 '05 #1
1 1208
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 sa***@bangproject.com for Information on Our Architecture and
Mentoring Services

</shameless self promotion>
Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marlene Harkcom | last post by:
I've got a datagrid with 5 bound columns in it as well as an edit/update/cancel column. I'm binding this to a dataset. One of the fields is always going to be one of three values. I want the...
0
by: Wayneb | last post by:
Hi I have a datagrid with autogenerates columns based upon a dataset I have constructed from the results of database query - pretty noddy stuff. When you click on Edit I remove the standard...
2
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
7
by: DJ Dev | last post by:
Hi All, I have a complex problem. I have dropdownlists (usually 3-5) and the user selects some value from these and for each value selected, datagrids are shown to the user. I am creating the...
4
by: K Bryan | last post by:
Hi There, I have two datagrids on a webform both of which display DropDownLists when edit mode is invoked. That being said, does anyone have a generic or straightforward way in C# so that...
4
by: Vik | last post by:
How can I display the lookup values in a datagrid? E.g., the datagrid displays a table that contains a ProductID field. I want to display a Product description from a Product table in that...
0
by: Daniel Doyle | last post by:
Hello and apologies in advance for the amount of code in this post. I've also sent this message to the Sharepoint group, but thought that ASP.NET developers may also be able to help, even though...
3
by: Phillip N Rounds | last post by:
I have a DataGrid in a web form (ASP1.1, C#, VS 2003) that I'm trying to add some functionality to, and I can't figure out how to do int. The existing DataGrid has several databound columns and a...
1
by: marcbb | last post by:
Hi all, I have a really strange problem working with Dropdownlists in a DataGrid. I'm trying to preselect some values from the DropDownlist for each row in the DataGrid, but it seems that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.