Reputation: 271
I have the following problem and i dont know why this is happening. I am declaring a dropdownlist in a grid view:
<asp:GridView ID="grdMappingList" runat="server" OnPageIndexChanging="grdMappingList_PageIndexChanging" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" ShowHeaderWhenEmpty="true" UseAccessibleHeader="true" CssClass="table table-hover table-striped segment_list_tbl" >
<PagerStyle CssClass="grid_pagination" HorizontalAlign="Right" VerticalAlign="Middle" BackColor="#DFDFDF" />
<Columns>
<asp:BoundField HeaderText="Mapping Id" DataField="MAPPING_ID"></asp:BoundField>
<asp:TemplateField HeaderText="Mapping Status" HeaderStyle-CssClass="width_120 aligned_center" ItemStyle-CssClass="width_120 aligned_center" Visible="true">
<ItemTemplate>
<asp:DropDownList id="ddgvOpp" runat="server" >
<asp:ListItem Text="DONE" Value="True"></asp:ListItem>
<asp:ListItem Text="NOT DONE" Value="False"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
The above is my aspx code. The dropdownlist with the id="ddgvOpp" is not recognized in my cs code.
private void InitializeDepartmentsDDL()
{
List<ReplaceParameter> param = new List<ReplaceParameter>();
//new- project selection
param.Add(new ReplaceParameter("PROJECT_ID", Convert.ToInt32(User_Data["SelectedProj"].Rows[0]["ProjectID"].ToString())));
DataTable AvailableDepartments = (DataTable)DQManager.Execute("LoadDepartments", null, param);
ddgvOpp.DataSource = AvailableDepartments;
ddgvOpp.DataTextField = "Text";
ddgvOpp.DataValueField = "Value";
ddgvOpp.DataBind();
ddgvOpp.Items.Insert(0, new ListItem("Please Select...", "-1"));
}
The error that i am getting is that the ename ddgvOpp does not exist in the current context. Please bear in mind that if get the dropdown list outside the the gridview everything is working.
Any help please?
The above problem is solved. Now i am facing the following problem. So here is my code :
<asp:DropDownList id="ddlMappingStatus" runat="server" SelectedValue='<%# Bind("MappingCompleted") %>' OnSelectedIndexChanged="ddlMappingStatus_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Done" Value="DONE"></asp:ListItem>
<asp:ListItem Text="Not Done" Value="NOT DONE"></asp:ListItem>
</asp:DropDownList>
When i call ddlMappingStatus_SelectedIndexChanged
in order to get all the attributes of the specific row nothing is returned. I can't get the values of the specific row and also the DropDownList ddgvOpp = (DropDownList)grdMappingList.SelectedRow.FindControl("ddlMappingStatus");
is giving an exception of null
object
Upvotes: 0
Views: 1444
Reputation: 460340
You have to use GridViewRow.FindControl("ControlID")
if you want to get the reference to the control in a TemplateField
of a GridView
since the row is the NamingContainer
not the page.
if(grdMappingList.SelectedRow != null)
{
DropDownList ddgvOpp = (DropDownList) grdMappingList.SelectedRow.FindControl("ddgvOpp");
// ...
}
This presumes that you want to find the DropDownList
of the selected row of the GridView
. If you want to find all you have to loop the rows:
foreach(GridViewRow row in grdMappingList.Rows)
{
DropDownList ddgvOpp = (DropDownList) row.FindControl("ddgvOpp");
// ...
}
Upvotes: 3