如何在detailview控件中添加下拉列表

时间:2012-10-04 21:29:06

标签: asp.net

如何在DetailView控件中放置下拉列表?我已将字段转换为模板但在添加下拉列表时遇到一些问题,基本上它没有将数据绑定到我的表中。我只是想在下拉列表中有一些静态数据,但是当我点击Update按钮时它应该保存它。我在编辑模式下有DetailView。感谢

<asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False" 
            BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" 
            CellPadding="4" CellSpacing="2" DataKeyNames="Post_ID" 
            DataSourceID="MyDataSource" ForeColor="Black" Height="50px" 
            Width="805px" DefaultMode="Edit">
            <EditRowStyle BackColor="#FFFFCC" Font-Bold="True" ForeColor="#003366" 
                BorderStyle="Groove" />
            <Fields>
                <asp:BoundField DataField="Post_ID" HeaderText="ID" ReadOnly="True" 
                    SortExpression="Post_ID" />
                <asp:TemplateField HeaderText="Category" 
                    SortExpression="CategoryList">



                     <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" 
                            Text='<%# Bind("CategoryList") %>'
                            Height="20px" Width="250px"></asp:TextBox>
                    </EditItemTemplate>




                      <InsertItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" 
                            Text='<%# Bind("CategoryList") %>'
                            Height="20px" TextMode="MultiLine" Width="300px"></asp:TextBox>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("CategoryList") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>


 <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="True" 
                            CommandName="Update" Text="Update" />
                        &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" 
                            CommandName="Cancel" Text="Cancel" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="False" 
                            CommandName="Edit" Text="Edit" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Fields>
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <InsertRowStyle BackColor="#FFFFCC" />
            <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
            <RowStyle BackColor="White" />
        </asp:DetailsView> 

2 个答案:

答案 0 :(得分:0)

您可以尝试使用

    <EditItemTemplate>
        <asp:DropDownList ID="DropDownList2" Runat="server"   >
           <asp:ListItem>1</asp:ListItem>
           <asp:ListItem>2</asp:ListItem>
        </asp:DropDownList>
     </EditItemTemplate>

背后的代码

protected void dvItem_DataBound(object sender, EventArgs e)
{
    if (this.dvItem.CurrentMode == DetailsViewMode.Edit)
    {
       DropDownList control= (DropDownList)this.dvItem.FindControl("IdDDL");
       ..          
    }
}  

选择

protected void dvItem_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
       DropDownList control = (DropDownList)this.dvItem.FindControl("IdDDL");
}  

答案 1 :(得分:0)

简单,只需将您的字段转换为模板字段,然后单击DetailsView的智能标记,然后选择编辑模板,然后在EditItem模板的字段中添加带有静态数据的DropDownList,并在EditDataBindings中设置BoundTo属性。

像这样:

<asp:DropDownList ID="DropDownList1" runat="server" 
                            SelectedValue='<%# Bind("yourField") %>'>
                            <asp:ListItem>One</asp:ListItem>
                            <asp:ListItem>Two</asp:ListItem>
                            <asp:ListItem>Three</asp:ListItem>
                        </asp:DropDownList>