Home All Groups Group Topic Archive Search About

Dropdowlist within a datagrid, (VB.NET)

Author
1 Feb 2005 10:34 PM
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.

Author
1 Feb 2005 10:43 PM
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*@dana.ucc.nau.edu> wrote in message
Show quoteHide quote
news:1107297264.099797.281280@g14g2000cwa.googlegroups.com...
> 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.
>
Author
1 Feb 2005 11:12 PM
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
Author
2 Feb 2005 4:00 AM
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.


<g*@dana.ucc.nau.edu> wrote in message
news:1107299551.090403.120910@f14g2000cwb.googlegroups.com...
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