Find Drop Down List Control in ItemTemplate

jeffreyjseaman
jeffreyjseaman used Ask the Experts™
on
I have a GridView with a Template Field that has a dropdownlist in the column.
I need to be able to communicate with the dropdownlist. But when I run the code to find the control the results I get is ControlValue = Nothing.


pagename.aspx

  <asp:TemplateField HeaderText="Column" SortExpression="Column">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlColumn"  runat="server" AutoPostBack="true"
                    ></asp:DropDownList>
                    </ItemTemplate>
                    </asp:TemplateField>  

pagename.aspx.vb
Dim ddlTypeOf As DropDownList = CType(FindControl("ddlColumn"), DropDownList)

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi jeffreyjseaman,

Under which event do you want to find the control.

Instead of giving just FindControl, give it as GridView1.FindControl like

Dim ddlTypeOf As DropDownList = CType(GridView1.FindControl("ddlColumn"), DropDownList)

Change the name of the gridview according to ur requirement
hi
try this code in OnRowDataBound Method.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdatabound.aspx

just put e.Row.FindControl(....
Hi jeffreyjseaman,

In the previous mail i forgot to add the row to gridview.
You will have to give the row also

like

Dim ddlTypeOf As DropDownList = CType(GridView1.Row(0).FindControl("ddlColumn"), DropDownList)

change the row position according to ur requirement.

If in a RowDataBound Event u can just give

Dim ddlTypeOf As DropDownList = CType(e.Row.FindControl("ddlColumn"), DropDownList)

If in a RowCommand event, u will have to find the rowindex from where the command was called.


Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Use GridView's SelectedRow object. Which will give you selected row object and you can find ddlColumn combo in that row.

Dim ddlTypeOf As DropDownList = CType(Use GridView.SelectedRow.FindControl("ddlColumn"), DropDownList)

Author

Commented:
I've used the following but it's coming up with NOTHING

Dim ddlTypeOf As DropDownList = CType(e.Row.FindControl("cboType"), DropDownList)

But if I do the following; it recognizes the control but it doesn't populate the dropdownlist.
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddlTypeOf As DropDownList = CType(e.Row.FindControl("cboType"), DropDownList)
End If
what do u want to do? do u want to populate data to the dropdown?

Author

Commented:
Rajeeshmca,

That's correct. I'm executing a stored procedure by sending it parameters. When it completes it's section I want to databhind to that dropdownlist.

If this DropDownList wasn't in the grid it works perfectly. But it needs to be in a grid.

Call a function within the dropdown control using the Datasource property like

<ItemTemplate>
 <asp:DropDownList ID="ddl_Region" CssClass="reqFields" runat="server" DataSource='<%# fillRegionDropDown() %>' DataTextField="Descr" DataValueField="RegionId" SelectedValue='<%# Bind("RegionId") %>' AppendDataBoundItems="True" >   
</asp:DropDownList>
</ItemTemplate>

Author

Commented:
Rajeeshmca,

What do you mean be call a function? where in SQL? You have a DataSource. Where is the Data Source coming from?
Hi jeffreyjseaman,

Call a user defined function in the code behind (cs Page).... this function will fetch the data from the database...
U will have to change the Datatext field and DataValuefield according to ur requirement

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial