下拉列表数据绑定,并在详细信息视图中插入新项目

时间:2014-01-31 05:51:27

标签: asp.net vb.net

在我的页面上,我有两个对象数据源;

第一个具有绑定下拉列表,包含项目编号和描述。  第二个具有绑定详细信息视图,显示项目编号的所有内容。

选择下拉列表中的项目会导致显示详细信息,如 意。当我从详细信息视图中插入新记录时,我想添加它 到下拉列表并选择它。

我试着做一个:

Private Sub dvRecipeItem_ItemInserted(sender As Object, e As DetailsViewInsertedEventArgs) Handles dvRecipeItem.ItemInserted
    ddRecipeItemNumber.DataBind()
End Sub

即使Databind确实将新添加的项目加载到下拉列表中,它也不会使其成为选定的值,因此详细信息视图上显示的记录只是项目编号的第一个,因为它按项目编号排序

有人可以解释一下如何让它适用于我在第二段描述的场景吗?

非常感谢你。

<asp:DropDownList 
ID="ddRecipeItemNumber" 
runat="server" 
width="600px" 
DataSourceID="EntityDataSource_RecipeItemNumber" 
DataTextField="DisplayName" 
DataValueField="Id" 
AutoPostBack="True"
OnDataBinding="Page_Load"
CssClass="cssRecipeItemNumber">
</asp:DropDownList>
<asp:EntityDataSource 
ID="EntityDataSource_RecipeItemNumber" 
runat="server" 
ConnectionString="name=OLTPEntities" 
DefaultContainerName="OLTPEntities" 
EnableFlattening="False" 
EntitySetName="vStd_RefineRecipe" 
EnableDelete="True" 
EnableInsert="True" 
EnableUpdate="True" 
EntityTypeFilter="vStd_RefineRecipe" 
Where="It.Id > 0"
OrderBy="It.ItemNumber">
</asp:EntityDataSource>

<asp:DetailsView 
ID="dvRecipeItem" 
runat="server" 
AutoGenerateRows="False"
DataKeyNames="Id" 
DataSourceID="EntityDataSource_RecipeItemDetail" 
OnItemCommand="dvRecipeItem_ItemCommandEventHandler"
GridLines="None" 
HeaderText="Recipe Detail"
CssClass="cssDetailsView"
HeaderStyle-CssClass="header"
Font-Names="Arial,Sans-Serif"
Font-Size="11px"
AlternatingRowStyle-CssClass="alternating"
CommandRowStyle-CssClass="command"
FieldHeaderStyle-CssClass="fieldheader">
<Fields>
    <asp:TemplateField HeaderText="Id" SortExpression="Id" InsertVisible="False">
    <EditItemTemplate>
        <asp:Label ID="Label_Id" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:TextBox ID="TextBox_Id" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
    </InsertItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label_Id" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Item Number" SortExpression="ItemNumber">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox_ItemNumber" runat="server" Text='<%# Bind("ItemNumber") %>'></asp:TextBox>
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:TextBox ID="TextBox_ItemNumber" runat="server" Text='<%# Bind("ItemNumber") %>'></asp:TextBox>
    </InsertItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label_ItemNumber" runat="server" Text='<%# Bind("ItemNumber") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" InsertText="Insert" UpdateText="Update" DeleteText="Delete" />                                
    </Fields>                            
    </asp:DetailsView>
    <asp:EntityDataSource 
        ID="EntityDataSource_RecipeItemDetail" 
        runat="server" 
        ConnectionString="name=OLTPEntities" 
        DefaultContainerName="OLTPEntities" 
        EnableFlattening="False" 
        EntitySetName="RefineRecipe" 
        Where="it.Id = @RecipeId" 
        EnableDelete="True" 
        EnableInsert="True" 
        EnableUpdate="True" 
        EntityTypeFilter="RefineRecipe">
        <WhereParameters>
        <asp:ControlParameter 
        ControlID="ddRecipeItemNumber" 
        DbType="Int32" 
        DefaultValue="1" 
        Name="RecipeId" 
        PropertyName="SelectedValue" />
        </WhereParameters>
        </asp:EntityDataSource>

Private Sub dvRecipeItem_ItemInserted(sender As Object, e As      DetailsViewInsertedEventArgs) Handles dvRecipeItem.ItemInserted

ddRecipeItemNumber.DataBind()
ddRecipeItemNumber.Items.FindByValue(DirectCast(dvRecipeItem.FindControl("TextBox_ItemNumber"), TextBox).Text).Selected = True

End Sub

1 个答案:

答案 0 :(得分:0)

Wirte Code This Way ::

Private Sub dvRecipeItem_ItemInserted(sender As Object, e As DetailsViewInsertedEventArgs) Handles dvRecipeItem.ItemInserted

    ddRecipeItemNumber.DataBind()
    ddRecipeItemNumber.Items.FindByValue(e.Values["ItemNumber"].ToString()).Selected = true;

End Sub

* String val是一个新插入的字符串