By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

Dependent DropDownList in DataGrid

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Shiju Poyilil" <Sh**********@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
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

Nov 18 '05 #2

P: n/a
Thanks Karl , It really worked for me.

Rgds
Shiju

"Karl" wrote:
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

"Shiju Poyilil" <Sh**********@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
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


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.