Dropdowlist within a datagrid, (VB.NET)

G

gn

Hi have a dropdown list within a datagrid, based on what is selected in
the list I want to populate another dropdownlist, the code to populate
the dropdownlists is not the problem, my problem is getting the code
behind to recognise that something has been selected.

I have tried using the selectedindexchanged event and that never fires.

I also added onselectchange="dataCOM_SelectedIndexChanged" attribute to
the dropdownlist and on the code behind page:

Public Sub dataCOM_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs)
'populate second dropdownlist code
End Sub

This didn't fire either.

Any ideas or suggestions would be appreciated.
 
S

Scott M.

To capture the event for a control placed into a Template column of a
DataGrid, you need to add the following code to your code-behind (this
assumes there is a checkbox named "chkDynamic" that you want to handle the
CheckChanged event of):

Protected Sub chkDynamic_CheckedChanged(ByVal Sender As Object, ByVal e As
System.EventArgs)
'This sub will be run for every row that has had its value changed
Dim chk As CheckBox = CType(Sender, CheckBox)
Dim item As DataGridItem = CType(chk.NamingContainer, DataGridItem)
' "item" is now a reference to the DataGrid selected row
If chk.Checked Then item.BackColor = Color.Gray
'From here, you could grab some data from this row with:
item.FindControl("ControlName")
'and then you would know which record was selected.
End Sub

Private Sub dg_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemCreated
'This sub runs anytime the DataGrid needs to dynamically create a cotrol
'but we only are interested in rows that may have controls (not header,
footer or pager rows)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim chk As CheckBox = CType(e.Item.FindControl("chkDynamic"),
CheckBox)
AddHandler chk.CheckedChanged, AddressOf chkDynamic_CheckedChanged
'This adds a client-side JavaScript event handler as well:
'You must also have added a client-side JavaScript function called
chkShow(sender)
chk.Attributes.Add("onClick", "chkShow(this," & rowNum.ToString &
")")
End If
End Sub
 
G

gn

Thanks for your fast response!

I have adapted the first sub to suit my application but it still
doesn't fire?

Protected Sub DDLAddCommYear_CheckedChanged(ByVal Sender As Object,
ByVal e As System.EventArgs)
'This sub will be run for every row that has had its value
changed
Dim DDLAddCommYear As DropDownList = CType(Sender,
DropDownList)
Dim item As DataGridItem =
CType(DDLAddCommYear.NamingContainer, DataGridItem)
' "item" is now a reference to the DataGrid selected row
If DDLAddCommYear.SelectedValue = "2004" Then item.BackColor =
Color.Gray
'From here, you could grab some data from this row with:­
item.FindControl("ControlName")
'and then you would know which record was selected.
End Sub
 
S

Scott M.

You need to add both subs. The first sub is a user-defined method, the
second (dg_ItemCreated) is called by the datagrid as it is building itself.
The dg_ItemCreated sub calls the first sub.


Thanks for your fast response!

I have adapted the first sub to suit my application but it still
doesn't fire?

Protected Sub DDLAddCommYear_CheckedChanged(ByVal Sender As Object,
ByVal e As System.EventArgs)
'This sub will be run for every row that has had its value
changed
Dim DDLAddCommYear As DropDownList = CType(Sender,
DropDownList)
Dim item As DataGridItem =
CType(DDLAddCommYear.NamingContainer, DataGridItem)
' "item" is now a reference to the DataGrid selected row
If DDLAddCommYear.SelectedValue = "2004" Then item.BackColor =
Color.Gray
'From here, you could grab some data from this row with:­
item.FindControl("ControlName")
'and then you would know which record was selected.
End Sub
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top