Hello <insert name here>,
It seems like you're doing something the hard way. All you need is a
reference to the selected row and from that you can use FindControl to
locate the ddl. With that you pull out the value. Below, I've inserted a
working sample that might get you going. Let us know if it helps?
Ken
Microsoft MVP [ASP.NET]
Toronto
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataS ource = CreateDataSourc e()
DataGrid1.DataB ind()
End If
End Sub
Private Sub DataGrid1_ItemC ommand _
(ByVal source As Object, ByVal _
e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) _
Handles DataGrid1.ItemC ommand
If e.CommandName = "Select" Then
Dim ddl As DropDownList
ddl = e.Item.FindCont rol("DDlUsers")
If Not IsNothing(ddl) Then
TextBox1.Text = "Selected " & _
ddl.SelectedIte m.Text & _
" from index # " & _
e.Item.ItemInde x.ToString
End If
End If
End Sub
Function CreateDataSourc e() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add( New DataColumn _
("SS", GetType(Int32)) )
dt.Columns.Add( New DataColumn _
("LastName", GetType(String) ))
dt.Columns.Add( New DataColumn _
("FirstName" , GetType(String) ))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i + i
dr(1) = "LastName " + i.ToString()
dr(2) = "FirstName " + i.ToString()
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSour ce
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateCol umns="False">
<Columns>
<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st
Name"></asp:BoundColumn >
<asp:BoundColum n DataField="Last Name" HeaderText="Las t
Name"></asp:BoundColumn >
<asp:TemplateCo lumn HeaderText="Use rs">
<ItemTemplate >
<asp:DropDownLi st id="DDlUsers" runat="server">
<asp:ListItem Value="Mr.">Mr. </asp:ListItem>
<asp:ListItem Value="Mrs.">Mr s.</asp:ListItem>
<asp:ListItem Value="Dr.">Dr. </asp:ListItem>
<asp:ListItem Value="Ms">Ms</asp:ListItem>
</asp:DropDownLis t>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:ButtonColu mn Text="Select" ButtonType="Pus hButton"
CommandName="Se lect"></asp:ButtonColum n>
</Columns>
</asp:DataGrid>
<P>
<asp:TextBox id="TextBox1" runat="server"> </asp:TextBox></P>
"m3ckon" <an*******@devd ex.com> wrote in message
news:%2******** **********@TK2M SFTNGP11.phx.gb l...
Hi there,
please help if you can, I'm having an issue with droponnlists in a
datagrid
I have a datagrid which is populated from a query .. all works fine
I've added two extra columns, one is a dropdownlist and the other is a
button which runs the command selectcode for this:
<Columns>
<asp:BoundColum n DataField="Salu tation"
HeaderText="Sal utation"></asp:BoundColumn >
<asp:BoundColum n DataField="Firs tName" HeaderText="Fir st
Name"></asp:BoundColumn >
<asp:BoundColum n DataField="Last Name" HeaderText="Las t
Name"></asp:BoundColumn >
<asp:BoundColum n DataField="Post Code" SortExpression= "Post Code"
HeaderText="Pos t Code"></asp:BoundColumn >
<asp:TemplateCo lumn HeaderText="Use rs">
<ItemTemplate >
<asp:DropDownLi st id=DDlUsers runat="server" DataSource="<%#
Itemtypes %>" >
</asp:DropDownLis t>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:ButtonColu mn Text="Select" ButtonType="Pus hButton"
CommandName="Se lect"></asp:ButtonColum n>
</Columns>
The datagrid renders correctly and when I click the button I've written
code to find the id of the dropdownlist on the same row as the button
which has been clicked ... however I can't access the dropdownlist value
and I NEED to do this in order to continue with my project.
My code is listed below:
Private Sub LeadsDG_Selecte dIndexChanged(B yVal sender As Object, ByVal e
As System.EventArg s) Handles LeadsDG.Selecte dIndexChanged
Dim ddstr As String
ddstr = LeadsDG.Selecte dItem.Cells(4). Controls(0).Cli entID
ddstr = Replace(ddstr, "__ctl1", "_DDlUsers" )
End Sub
Please someone tell me how to access the drop down, I've tried this
(with writing the vlue to a text box):
Dim DDlist1 As DropDownList = CType(LeadsDG.F indControl(ddst r),
DropDownList)
TextBox1.Text = DDlist1.Selecte dItem.Value
But I get the following error when I click a value:
Object reference not set to an instance of an object.
Please help me access the dropdownlist values, I know that the sub is
being run and that the id is the correct control, but I'm stuck getting
it working
Regrds,
m3ckon
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!