13

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.

Tyler Mortensen
  • 451
  • 3
  • 8
  • 19
  • Check if [this](http://geekswithblogs.net/AzamSharp/archive/2006/08/27/89475.aspx) helps. Is not an ideal solution, but it might works for you. – Claudio Redi May 21 '12 at 15:21

6 Answers6

19

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

Rodrigo Reis
  • 1,097
  • 12
  • 21
4

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);
    }
}
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
  • If you don't have access to server-side code, this falls down. How would one go about this client-side? – Code Maverick Aug 10 '17 at 15:09
  • Hey, @CodeMaverick - the [top-voted answer](https://stackoverflow.com/a/11653516/861565) on this question has a good solution that I know works if you have access to the markup. If you mean client-side as in JavaScript, you may have to be a bit more creative. The server generated ID for the dropdown lists will contain the server side ID, so you could use the ["attribute contains selector"](https://api.jquery.com/attribute-contains-selector/) to get all of the dropdowns, and then iterate through them, setting the correct selected value based on context (sibling controls, etc). – Josh Darnell Aug 10 '17 at 16:06
3

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.

Glenn
  • 31
  • 1
  • Worked best in my situation: A DetailsView(dv) with a DropDownList(ddl) that is bound to a second datasource(s2) that requires ddl.SelectedValue as a select-parameter. If s2 was placed outside of dv and dv is NOT visible (happens in my code), s2 would still try to access ddl and strange things happen. – Tobias81 Feb 16 '16 at 16:50
2

Another option is, set your dropdownlist client id mode to be static. Then your dropdownlist id will not be modified.

 ClientIDMode="Static"

Thanks,

Esen.

Esen
  • 973
  • 1
  • 21
  • 47
0

Make sure your control of interest has runat="server". Argh.

CindyH
  • 2,986
  • 2
  • 24
  • 38
0

if you talking about gridview (add new record button) use

    <asp:Parameter Name="Type" Type="String"/> 

instead of

    <asp:ControlParameter    Name="Type"    ControlID="rddType"   PropertyName="SelectedValue" />