Drop Down List inside a Datagrid

C

Chris Kettenbach

Good Morning,
Quick question, I have an EditItemTemplate inside a datagrid. It should
contain a drop down list of possible choices. I have an EditCommandColumn.
The drop dow list column should initially contain the value of the field.
This works fine with
<ItemTemplate><%# Container.DataItem("NameType")%></ItemTemplate>

The desired effect is for the drop down list to databind to a query to show
possible choices when the user clicks the edit link. I can not get a
reference to the drop down list when the user clicks edit. Does anyone know
how to do this?

I tried page.findcontrol("controlname") and it does not work. Any ideas? I
get a NullReference Error when I do this, because I guess the control does
not exist on the page.

Thanks to everyone,
Chris
 
G

Guest

Within the method that handles the datagrid EditCommand event you can get a
reference to the dropdown list by using the passed DataGridCommandEventArgs
as follows:
CType(e.Item.FindControl("controlname"), DropDownList)
 
C

Chris Kettenbach

No, that still didn't work. Any other suggestions?
I ended up with this

Sub DataGrid_Edit(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)

' turn on editing for the selected row

Dim ddl As DropDownList = CType(E.Item.FindControl("drpNameType"),
DropDownList)

If Not isEdit Then

dg1.EditItemIndex = E.Item.ItemIndex

BindGrid()

End If


End Sub



Using a breakpoint, I see ddl = Nothing. What am I doing wrong here? Thanks!
 
C

Charlie@NISH

In the edit command handler try... DropDownList ddl = (DropDownList)
e.items.findcontrol("controlname")

Charlie
 
Joined
Aug 7, 2007
Messages
1
Reaction score
0
Row[x].Cell[y].FindControl()

Patrick.O.Ige said:
Did that did the trick
Try going through this article at:-
http://www.4guysfromrolla.com/webtech/050801-1.shtml
Hope that helps
Patrick

I was experiencing the same trouble while trying to use FindControl to get a DropDownList that was inside a DetailsView EditTemplate.
I found Patrick's link to be helpful, but I was still having trouble get the SelectedValue from the DropDownMenu. Finally, I found I was able to locate and work with the DropDownList by placing the following code in my PageLoad:
Code:
        if (DetailsView1.CurrentMode != DetailsViewMode.ReadOnly)
        {
            DropDownList myDropDownList = (DropDownList)DetailsView1.Rows[8].Cells[1].FindControl("myDropDownInASPX");
            Label1.Text += myDropDownList.SelectedValue.ToString();
        }

-of course the only trick was figuring out what Row and what Cell to reference.
 
Joined
Mar 16, 2009
Messages
1
Reaction score
0
selected index problem in editable Dg with drop down list

<asp:datagrid id=DataGrid1 style="Z-INDEX: 104; LEFT: 304px; POSITION: absolute; TOP: 104px" runat="server" CssClass="grid" AutoGenerateColumns="False">
<Columns>

<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
<asp:BoundColumn DataField="Message Id" ReadOnly="False" HeaderText="Message Id" />
<asp:TemplateColumn HeaderText="Opcode">
<ItemTemplate>
<%#Container.DataItem("Opcode")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList AutoPostBack = false id="ddl_Opcode" runat="server" DataValueField="Opcode" DataTextField ="Opcode" DataSource ="<%#TempDataView%>" >

</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

<asp:BoundColumn DataField="Message Status" HeaderText="Message Status" ReadOnly="True" />
<asp:BoundColumn DataField="Error Descp" HeaderText="Error Descp" ReadOnly="True" />

</Columns>
</asp:datagrid>



Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
populateDropDownList()
DataGrid1.EditItemIndex = e.Item.ItemIndex
End Sub


Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
Dim myDDL As New DropDownList
myDDL = CType(e.Item.FindControl("ddl_Opcode"), DropDownList)
Response.Write(myDDL.SelectedItem.Value)
End Sub


Private Function populateDropDownList()
Dim mydatatable As New DataTable
mydatatable.Columns.Add("Opcode", Type.GetType("System.String"))
' Declare row
Dim myrow As DataRow
' create new row
myrow = mydatatable.NewRow
myrow("Opcode") = "CREATE"
mydatatable.Rows.Add(myrow)
myrow = mydatatable.NewRow
myrow("Opcode") = "UPDATE"
mydatatable.Rows.Add(myrow)
myrow = mydatatable.NewRow
myrow("Opcode") = "DELETE"
mydatatable.Rows.Add(myrow)
TempDataView = mydatatable.DefaultView


End Function




obj ref not set error
drop down list selected item remains nothing
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top