0

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

2 回答 2

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");
}  
于 2012-10-04T21:52:19.090 回答
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>
于 2012-10-04T21:52:59.883 回答