DetailsView_ModeChanged 方法无法使用 FindCotrol 方法找到特定模式下的控件
我有一个关于商店产品的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您需要首先找到您的编辑容器。看看你的问题,如果我理解正确的话 - 我可能建议使用 Databound 事件并在那里绑定下拉列表。
查看此链接:
我还认为您应该将产品类别数据源:
移动到编辑模板之外(它可以存在于详细信息视图之外)。
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:
to outside of the edit template (it can exist outside of the detailsview).