Link to home
Start Free TrialLog in
Avatar of jeffreyjseaman
jeffreyjseaman

asked on

Find Drop Down List Control in ItemTemplate

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

Avatar of rajeeshmca
rajeeshmca
Flag of India image

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.


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)
Avatar of jeffreyjseaman
jeffreyjseaman

ASKER

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?
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>

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...
ASKER CERTIFIED SOLUTION
Avatar of rajeeshmca
rajeeshmca
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial