Dependent DropDownList in DataGrid

G

Guest

Hello,

I have a datagrid with only one row and its having 2 dropdownlists, I need
to populate the secodn dropdownlist on the basis of the selection in the
first dropdown. but I am not able to populate the second dropdown, as its not
bale to find the specified control, even though the dropdownlist with the
specified name exists in the datagrid.

my .aspx page code goes like this

<asp:DataGrid id="Datagrid2" runat="server" Cssclass="txtTen" Width="600px"
Height="35px" Font-Size="9px" Font-Names="Verdana"
AutoGenerateColumns="False" Visible="True" BackColor="#FCFBEB"
BorderColor="#FFC0C0">

<HeaderStyle Font-Size="9px" Font-Names="Verdana"
Font-Bold="True"></HeaderStyle>

<Columns>
<asp:TemplateColumn HeaderText="Hotel Type">
<ItemTemplate>
<asp:DropDownList id="ddlhtype" runat="server" Font-Size="9px"
Font-Names="Verdana" Width="100px" AutoPostBack="true" >
<asp:ListItem Value="Hotel Type">Hotel Type</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Hotel name">
<ItemTemplate>
<asp:DropDownList id="ddlhname" runat="server" Font-Size="9px"
Font-Names="Verdana" Width="100px"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</asp:DataGrid>

my code behind for the grid goes like this
Private Sub Datagrid2_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles Datagrid2.ItemCreated

If Not e.Item.FindControl("ddlhtype") Is Nothing Then
AddHandler CType(e.Item.FindControl("ddlhtype"),
DropDownList).SelectedIndexChanged, AddressOf GetHotelNames
End If

End Sub

the handler definition goes like this


Public Sub GetHotelNames(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim quotemgr As New QuotationManager
Dim objcitystop As New CitystopServicesBE
objcitystop.ServiceCode = "CITYSTOP"
objcitystop.detailcode = "HTNM"

If Not CType(Datagrid2.FindControl("ddlhtype"), DropDownList) Is
Nothing Then
Dim strname As String
strname = CType(sender, DropDownList).SelectedValue.ToString()
objcitystop.Filter = strname

Dim col As New Collection
If Not CType(e.Item.FindControl("ddlhtype"),
DropDownList) Is Nothing Then

col = quotemgr.getcitystopdetails(objcitystop)
Dim objcity As CitystopServicesBE
For Each objcity In col
Dim strcode As New ListItem
strcode.Text = objcity.desc
strcode.Value = objcity.serviceid
Response.Write("before ddlname <br>")

If Not CType(Datagrid2.FindControl("ddlhname"),
DropDownList) Is Nothing Then
CType(Datagrid2.FindControl("ddlhname"),
DropDownList).Items.Add(strcode)
End If
Next
End If



Catch ex As Exception
Response.Write(ex.ToString)
End Try

but I am surprised to find that , it doesnt find both the mentioned
dropdowns, Can you pls tell me where I am going wrong.

Your expert advise would be highly appreciated.

Rgds
Shiju
 
K

Karl

Shiju,
Once you are in your GetHotelNames method you can get a reference to either
dropdownlist like so:

dim htype as DropDownList = ctype(sender, DropDownList)
dim hValue as DropDownList = ctype(ctype(htype.Parent.Parent,
DataGridITem).FindControl("ddlHValue"), DropdownList)

Those that help?

Karl
 

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

Latest Threads

Top