2012-10-04 32 views
0

如何在DetailView控件中放置下拉列表?我已將字段轉換爲模板,但在添加下拉列表時遇到了一些問題,基本上它不會將數據綁定到我的表中。我只是想在下拉列表中輸入一些靜態數據,但當我點擊更新按鈕時它應該保存它。我有一個編輯模式下的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> 

回答

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"); 
} 
+0

感謝,但這個代碼是給我所有樣的錯誤專門的CURRENTMODE和我不知道idDDL總的來說,我不能跟着代碼,因爲我是新手。如果你能簡化,我將不勝感激。謝謝 – moe

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> 
+0

謝謝我的確如你所說,但問題是選定值的綁定是灰色的,只有選定的自定義綁定是活動的 – moe

+0

任何人都可以在這裏幫忙嗎? – moe

+0

如何將您的DetailsView與Code或Wirzard綁定?當你使用代碼apporach只是自定義綁定是活動的。 –