在EditItemTemplate中找不到ControlParameter中的控件吗

时间:2021-12-08 02:16:07

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:

我正在一个动态数据网站上工作,我遇到了一堵墙。我有一个详细的页面,每个员工的详细信息可以被看到,然后我有一个单独的页面来编辑每个员工。我这样做是因为我需要在每个部门使用下拉列表框。不过,我在访问部门ddl时遇到了麻烦,我认为这是因为它位于EditItemTemplate中。以下是我的资料:

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

然后,我尝试使用ddlDept SelectedValue填充ddlJob。这是我要使用的数据源。

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

我知道Select参数的格式是正确的,因为我正在使用另一个ddl来填充DetailsView,而且我知道部门和jobcode之间的关系是正确的,因为我正在成功地使用它和AddEmployee页面。

Here is the error I get:

我得到的错误是:

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

在ControlParameter 'DeptID'中无法找到control 'ddlDept'。

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.

假设它不能通过ID访问ddlDept因为它在EditItemTemplate中?我该怎么解决这个问题呢?关于如何实现这一点的其他建议?非常感谢您的帮助。

6 个解决方案

#1


17  

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

我发现这个链接可以帮助解决没有服务器端的问题:解决控制参数‘xxx’中找不到控件‘xxx’的错误。

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

作者说,您可以使用美元char($)来访问内部控制。

Ex:

例:

ControlID="dvEmployee$ddlDept"

ControlID = " dvEmployee ddlDept美元"

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

会得到dvEmployee内部控制的ddlDept的值吗

#2


3  

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

你的假设是正确的; 无法识别您的ddlDept,因为它位于不同的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.

解决这个问题的一种方法是从标记中删除 ,并在运行时以编程方式添加它,这样您就可以使用ddlDept的实际的UniqueID属性。

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);
    }
}

#3


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.

我发现在DetailsView或GridView控件的上下文中寻找控件的数据源对象的一个方法是将数据源控件放置在具有您希望引用的控件的项/编辑项模板中。这可能并不适合所有的情况,但它确实有效。

#4


2  

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

另一个选项是,将dropdownlist客户端id模式设置为静态。那么您的下拉列表id将不会被修改。

 ClientIDMode="Static"

Thanks,

谢谢,

Esen.

女猎手。

#5


0  

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

确保您的感兴趣的控件具有runat="server"。啊。

#6


0  

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

如果您正在讨论gridview(添加新记录按钮),请使用

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

instead of

而不是

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

#1


17  

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

我发现这个链接可以帮助解决没有服务器端的问题:解决控制参数‘xxx’中找不到控件‘xxx’的错误。

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

作者说,您可以使用美元char($)来访问内部控制。

Ex:

例:

ControlID="dvEmployee$ddlDept"

ControlID = " dvEmployee ddlDept美元"

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

会得到dvEmployee内部控制的ddlDept的值吗

#2


3  

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

你的假设是正确的; 无法识别您的ddlDept,因为它位于不同的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.

解决这个问题的一种方法是从标记中删除 ,并在运行时以编程方式添加它,这样您就可以使用ddlDept的实际的UniqueID属性。

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);
    }
}

#3


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.

我发现在DetailsView或GridView控件的上下文中寻找控件的数据源对象的一个方法是将数据源控件放置在具有您希望引用的控件的项/编辑项模板中。这可能并不适合所有的情况,但它确实有效。

#4


2  

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

另一个选项是,将dropdownlist客户端id模式设置为静态。那么您的下拉列表id将不会被修改。

 ClientIDMode="Static"

Thanks,

谢谢,

Esen.

女猎手。

#5


0  

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

确保您的感兴趣的控件具有runat="server"。啊。

#6


0  

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

如果您正在讨论gridview(添加新记录按钮),请使用

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

instead of

而不是

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