如何将数据绑定到DetailsView内UpdatePanel中的DropDownList
我使用 DetailsView 在数据库中插入行。行有字段 id、subcategory_id 等。我想动态填充下拉列表 ddl_subcategories,它在 TemplateField 中使用。第一个下拉列表 ddl_categories 的所选项目值用作生成 ddl_subcategories 集合的参数。我尝试使用 UpdatePanel,但方法 DataBind 返回错误“数据绑定方法,例如 Eval()、XPath() 和 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();
}
我的错误是什么? 谢谢。
I use DetailsView to insert rows in database. Row has fields id, subcategory_id etc. I want to fill dynamically dropdownlist ddl_subcategories, which is used in TemplateField. Selected item value of first dropdownlist ddl_categories is used as parameter for generating collection for ddl_subcategories. I try it with using UpdatePanel, but method DataBind returns error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.".
There's code of web form
<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>
There is part of behind-code:
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();
}
What is my error?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是,当您对子类别下拉列表进行数据绑定时,没有数据绑定上下文(请参阅此处< /a> 了解更多信息,请关注 David Fowler 的评论)。我不认为 UpdatePanel 是问题,因为无论有没有它,行为都是相同的(如果您将整个 DetailsView 包装在 UpdatePanel 中)。
解决方案是单独加载项目,而不是绑定控件的数据。我承认这看起来有点笨拙,但它确实有效。
更好的选择是不在控件中使用 Bind,而只在插入/更新时设置 ObjectDataSource 参数中的值。
更新:您可以设置 ObjectDataSource 直接值。为 OnInserting< 添加事件处理程序/a> (或 OnUpdating 根据需要)如下:
我现在无法实际测试它,但它应该接近。
The problem is that when you are data binding the subcategories dropdown, there is no DataBinding Context (see here for more info, pay attention to David Fowler's comments). I don't think the UpdatePanel is the issue as the behavior is the same with or without it (and if you wrap the entire DetailsView in an UpdatePanel).
A solution is to load the items individually instead of data binding the control. It seems a little clumsy, I admit, but it does work.
A better option would be to not use Bind in the control, and just set the value in the ObjectDataSource's parameters on Insert/Update.
Update: You can set the ObjectDataSource value directly. Add an event handler for the OnInserting (or OnUpdating as needed) as follows:
I can't actually test this right now, but it should be close.
删除
Bind("DBFieldName")
并将以下行添加到YourDetailsView_ItemInserting
中,如下所示:注意:不要忘记将 Rows[1] 替换为下拉列表模板字段索引。
Remove the
Bind("DBFieldName")
and add the following line to theYourDetailsView_ItemInserting
like the following:Note: Don't forget to replace the Rows[1] to your Dropdown templete field index.
尝试用更新面板包裹整个DetailsView,看看这是否有所不同......
Try wrapping the entire DetailsView with the update panel to see if that makes a difference...