gpt4 book ai didi

asp.net - 如何在DetailsView内的UpdatePanel中将数据绑定(bind)到DropDownList

转载 作者:行者123 更新时间:2023-12-01 04:17:03 24 4
gpt4 key购买 nike

我使用 DetailsView 在数据库中插入行。 Row 有字段 id、subcategory_id 等。我想动态填充在 TemplateField 中使用的下拉列表 ddl_subcategories。第一个下拉列表 ddl_categories 的选定项目值用作为 ddl_subcategories 生成集合的参数。我尝试使用 UpdatePanel,但 DataBind 方法返回错误“Eval()、XPath() 和 Bind() 等数据绑定(bind)方法只能在数据绑定(bind)控件的上下文中使用。”。

有网页表单的代码

<asp:DetailsView ID="dvw" runat="server" Height="50px" Width="125px" 
AutoGenerateRows="False" DataSourceID="ods"
DefaultMode="Insert" DataKeyNames="Section_id"
OnDataBound="dvw_DataBound" OnItemUpdated="dvw_ItemUpdated"
OnItemCommand="dvw_ItemCommand">
<Fields>
<asp:TemplateField HeaderText="Category" >
<ItemTemplate>
<asp:DropDownList ID="ddl_categories" runat="server" AutoPostBack="true" DataSourceID="ods_categories"
DataTextField="Name" DataValueField="Category_id" OnSelectedIndexChanged="category_select_index_changed"/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Subcategory" >
<ItemTemplate>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddl_subcategories" runat="server"
SelectedValue='<%# Bind("Subcategory_id") %>' />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl_categories" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
...
</Fields>
</asp:DetailsView>

有一部分背后的代码:
protected void category_select_index_changed(object sender, EventArgs e)
{
DropDownList ddl_categories = (DropDownList)dvw.FindControl("ddl_categories");
List<SUBCATEGORY> sections = SUBCATEGORY.Select_all_by_parameters(Int32.Parse(ddl_categories.SelectedValue));//Select all subcategories by id of category
DropDownList ddl_subcategories= (DropDownList)dvw.FindControl("ddl_subcategories");

ddl_subcategories.DataSource = sections;
ddl_subcategories.DataTextField = "Name";
ddl_subcategories.DataValueField = "Subcategory_id";
ddl_subcategories.DataBind();
}

我的错误是什么?
谢谢。

最佳答案

问题是当您对子类别下拉列表进行数据绑定(bind)时,没有 DataBinding 上下文(参见 here 了解更多信息,请注意 David Fowler 的评论)。我不认为 UpdatePanel 是问题,因为无论有没有它,行为都是相同的(如果您将整个 DetailsView 包装在 UpdatePanel 中)。

一种解决方案是单独加载项目,而不是对控件进行数据绑定(bind)。我承认,这似乎有点笨拙,但它确实有效。

protected void category_select_index_changed(object sender, EventArgs e)
{
DropDownList ddl_categories = (DropDownList)dvw.FindControl("ddl_categories");
IEnumerable<SUBCATEGORY> sections = new SUBCATEGORY[] { new SUBCATEGORY() { Name = "First " + ddl_categories.SelectedValue, Subcategory_id = "1" }, new SUBCATEGORY() { Name = "Second", Subcategory_id = "2" } };
DropDownList ddl_subcategories = (DropDownList)dvw.FindControl("ddl_subcategories");

ddl_subcategories.Items.Clear();
foreach (SUBCATEGORY cat in sections)
{
ddl_subcategories.Items.Add(new ListItem(cat.Name, cat.Subcategory_id));
}
}

更好的选择是不在控件中使用 Bind,而只在插入/更新时设置 ObjectDataSource 参数中的值。

更新:您可以设置 ObjectDataSource直接取值。为 OnInserting 添加事件处理程序(或 OnUpdating 根据需要)如下:
protected void ods_OnInserting(object sender, ObjectDataSourceMethodEventArgs e)
{
DropDownList ddl_subcategories = (DropDownList)dvw.FindControl("ddl_subcategories");
e.InputParameters["Subcategory_id"] = ddl_subcategories.SelectedValue;
}

我现在实际上无法测试这个,但它应该很接近。

关于asp.net - 如何在DetailsView内的UpdatePanel中将数据绑定(bind)到DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3426299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com