Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find Control in ControlParameter inside EditItemTemplate

I am working on a Dynamic Data website and I have run into a wall. I have a Details page where the details for each employee can be seen, and then I have a separate page to edit each employee. I did this because I need to use DropDownList boxes for Department and Job in each department. Nevertheless, I am having trouble accessing the department ddl and I think it is because it is inside an EditItemTemplate. Here is what I have:

<asp:DetailsView ID="dvEmployee" 
                    DataSourceID="EmpDVds" 
                    AutoGenerateRows="false" 
                    DataKeyNames="Id" 
                    GridLines="None" 
                    CellSpacing="10" 
                    runat="server" DefaultMode="Edit">
                    <Fields>
                        <asp:TemplateField HeaderStyle-Font-Bold="true" HeaderText="Department: ">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddlDept" DataSourceID="DeptDDLds" DataTextField = "DepartmentName" DataValueField = "Id" runat="server" SelectedValue='<%#Bind("DeptID") %>' />
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderStyle-Font-Bold="true" HeaderText="Job Code: ">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddlJob" DataSourceID="JobDDLds" DataTextField = "JobName" DataValueField = "Id" runat="server" />
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Fields>

Then I am trying to use the ddlDept SelectedValue to populate the ddlJob. Here is the DataSource I am trying to use.

<asp:SqlDataSource ID="JobDDLds"
                    SelectCommand="
                        SELECT 
                        Id, 
                        Code+' - '+[Desc] AS JobName,
                        Department_Id 
                        FROM 
                        JobCodes 
                        WHERE
                        JobCodes.Department_Id = @DeptID"
                    ConnectionString="<%$ConnectionStrings:TrainingDatabaseConnection %>" runat="server" >
                        <SelectParameters>
                            <asp:ControlParameter ControlID="ddlDept" PropertyName="SelectedValue"
                                    Name="DeptID" Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>

I know that the format of the Select parameter is correct because I am using another ddl to populate the DetailsView and I know the relationship between Departments and JobCodes is correct because I am using it successfully in and AddEmployee page.

Here is the error I get:

Could not find control 'ddlDept' in ControlParameter 'DeptID'.

I am I correct in assuming that it cannot access the ddlDept by it's ID because it is in the EditItemTemplate? How can I fix this? Other suggestions on how to achieve this? Any and all help is greatly appreciated.

like image 602
Tyler Mortensen Avatar asked May 21 '12 15:05

Tyler Mortensen


3 Answers

Your assumption is correct; the <ControlParameter> doesn't recognize your ddlDept because it's in a different ContentTemplate.

One way to work around this is to remove the <ControlParameter> from your markup and add it programatically at runtime, so that you can use ddlDept's actual UniqueID property.

Something like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // Create your ControlParameter
        ControlParameter deptParam = new ControlParameter();
        deptParam.ControlID = ddlDept.UniqueID;
        deptParam.PropertyName = "SelectedValue";
        deptParam.Name = "DeptID";
        deptParam.Type = TypeCode.Int32;
        // Add it to your SelectParameters collection
        JobDDLds.SelectParameters.Add(deptParam);
    }
}
like image 176
Josh Darnell Avatar answered Nov 14 '22 12:11

Josh Darnell


I found this link helps to solve without server side: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."

the author says that you can use the dollar char ($) to access the inner control.

Ex:

ControlID="dvEmployee$ddlDept"

will get the value of ddlDept that is a inner control of dvEmployee

like image 35
Rodrigo Reis Avatar answered Nov 14 '22 13:11

Rodrigo Reis


One way I've found to get around this issue with data source objects looking for controls inside the context of a DetailsView or GridView control is to actually place the data source control inside the item/edit item template that has the controls you wish to reference. This might not be ideal for all situations, but it certainly works.

like image 34
Glenn Avatar answered Nov 14 '22 12:11

Glenn