DetailsView_ModeChanged 方法无法使用 FindCotrol 方法找到特定模式下的控件

发布于 2024-10-06 04:47:18 字数 2240 浏览 5 评论 0原文

我有一个关于商店产品的DetailsView 控件。

当我点击DetailsView控件的“编辑”按钮时,我想绑定一个DropDownList来列出产品类别并选择其中的当前产品类别。

我使用方法“ModeChanged”来选择当前产品类别,如下所示:

编辑:标记:

<asp:DetailsView ID="dtlProduct" runat="server" 
        DataSourceID="ProductDetailsLinqDataSource" AutoGenerateRows="False" 
        DataKeyNames="ProductID">

        <Fields>
            <asp:BoundField DataField="ProductName"
                SortExpression="ProductName" />
            <asp:TemplateField>
            <ItemTemplate>
                <asp:Label Text='<%# Eval("ProductCategory.CategoryName") %>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlCategory" runat="server" DataSourceID="LDS_ProductsCategories" 
                    DataTextField="CategoryName" DataValueField="CategoryID" Width="200px">
                </asp:DropDownList>
                <asp:LinqDataSource ID="LDS_ProductsCategories" runat="server" 
                    ContextTypeName="ProductsDataClassesDataContext" 
                    Select="new (CategoryID, CategoryName)" TableName="ProductCategories">
                </asp:LinqDataSource>
            </EditItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView>

代码隐藏:

protected void dtlProduct_ModeChanged(object sender, EventArgs e)
{
    if (dtlProduct.CurrentMode == DetailsViewMode.Edit)
    {
        ProductsDataClassesDataContext dc = new ProductsDataClassesDataContext();
        var categoryID = (from c in dc.Products
                     where c.ProductID == (int)dtlProduct.DataKey.Value
                     select c.ProductCategoryID).FirstOrDefault();

        if (categoryID != null)
        {
            DropDownList ddl = dtlProduct.FindControl("ddlCategory") as DropDownList;
            ddl.Items.FindByValue(categoryID.ToString()).Selected = true;
        }
    }
}

FindControl 方法找不到“ddlCategory”(返回 null),尽管它存在于 EditTemplateField 中。

我不知道出了什么问题!

我正在考虑使用“DropDownList's PreRender”事件来实现我的目标,但我想知道出了什么问题!

非常感谢....

I have a DetailsView control about a store products.

When I hit the "Edit" button of the DetailsView control, I want to bind a DropDownList to list products categories and select the current product category in it.

I used the method "ModeChanged" to select the current product category like this:

Edit: Markup:

<asp:DetailsView ID="dtlProduct" runat="server" 
        DataSourceID="ProductDetailsLinqDataSource" AutoGenerateRows="False" 
        DataKeyNames="ProductID">

        <Fields>
            <asp:BoundField DataField="ProductName"
                SortExpression="ProductName" />
            <asp:TemplateField>
            <ItemTemplate>
                <asp:Label Text='<%# Eval("ProductCategory.CategoryName") %>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlCategory" runat="server" DataSourceID="LDS_ProductsCategories" 
                    DataTextField="CategoryName" DataValueField="CategoryID" Width="200px">
                </asp:DropDownList>
                <asp:LinqDataSource ID="LDS_ProductsCategories" runat="server" 
                    ContextTypeName="ProductsDataClassesDataContext" 
                    Select="new (CategoryID, CategoryName)" TableName="ProductCategories">
                </asp:LinqDataSource>
            </EditItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView>

Code Behind:

protected void dtlProduct_ModeChanged(object sender, EventArgs e)
{
    if (dtlProduct.CurrentMode == DetailsViewMode.Edit)
    {
        ProductsDataClassesDataContext dc = new ProductsDataClassesDataContext();
        var categoryID = (from c in dc.Products
                     where c.ProductID == (int)dtlProduct.DataKey.Value
                     select c.ProductCategoryID).FirstOrDefault();

        if (categoryID != null)
        {
            DropDownList ddl = dtlProduct.FindControl("ddlCategory") as DropDownList;
            ddl.Items.FindByValue(categoryID.ToString()).Selected = true;
        }
    }
}

the FindControl method DOES NOT find the "ddlCategory" (returns null) although it's present in the EditTemplateField.

I don't know what's going wrong!

I'm thinking to use "DropDownList's PreRender" event for doing the purpose I aim, but I want to know what is wrong!

Many thanks....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄傲 2024-10-13 04:47:18

看起来您需要首先找到您的编辑容器。看看你的问题,如果我理解正确的话 - 我可能建议使用 Databound 事件并在那里绑定下拉列表。

查看此链接:

我还认为您应该将产品类别数据源:

  <asp:LinqDataSource ID="LDS_ProductsCategories" runat="server" 
                    ContextTypeName="ProductsDataClassesDataContext" 
                    Select="new (CategoryID, CategoryName)" TableName="ProductCategories">
                </asp:LinqDataSource>

移动到编辑模板之外(它可以存在于详细信息视图之外)。

It looks like you need to find your edit container first. Looking at your question, if I understand correctly - I may suggest using the Databound event and bind the dropdown list there.

Check out this link:
http://weblogs.asp.net/sukumarraju/archive/2009/11/22/binding-drop-down-list-control-when-details-view-is-in-edit-mode.aspx

I'm also thinking you should move your productcategeories datasource:

  <asp:LinqDataSource ID="LDS_ProductsCategories" runat="server" 
                    ContextTypeName="ProductsDataClassesDataContext" 
                    Select="new (CategoryID, CategoryName)" TableName="ProductCategories">
                </asp:LinqDataSource>

to outside of the edit template (it can exist outside of the detailsview).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文