2009-07-28 23 views
2

我有一個asp:GridView,並在那裏我有兩列,在一列中我想顯示標籤 ,但是當我點擊一個sdit按鈕時,我想顯示下拉列表中的那個特定列, 我創建像下面的表格視圖:asp:GridView控件的EditTemplate屬性

<bw:GridView ID="grdProducts" AllowPaging="True" PageSize="5" AllowSorting="True" 
    CssClass="DGTable" runat="server" AutoGenerateColumns="False" DataKeyNames="LinkedProductCode" 
    RowSelectingEnabled="True" RowStyle-CssClass="DGItem" SelectedRowStyle-CssClass="DGSelectedItem" 
    FooterStyle-CssClass="DGFooterTR" EditRowStyle-CssClass="DGEditItemValidator" > 
    <Columns> 
    <asp:BoundField DataField="LinkedProductCode" HeaderText="Product Code" ReadOnly="true" meta:resourcekey="BoundFieldResource4" />           
    <asp:TemplateField HeaderText="Product Type" ItemStyle-VerticalAlign="Top"> 
    <ItemTemplate> 
    <asp:Label ID="lbl1" runat="server" Text='<%# Bind("LinkedProductType")%>' /> 
    </ItemTemplate> 
    <EditItemTemplate > 
     <asp:DropDownList ID="linkedproductList" runat="server" DataSourceID="list"> 
     </asp:DropDownList> 
    </EditItemTemplate> 
    </asp:TemplateField>           
    </Columns> 
    <SelectedRowStyle CssClass="DGSelectedItem" /> 
    <PagerStyle CssClass="DGPagerTR" /> 
    <HeaderStyle CssClass="DGHeaderTR" /> 
</bw:GridView> 

是我應該做的去做呢?我應該在編輯按鈕的點擊事件中寫什麼? 請幫忙..

回答

3

這取決於你如何設置編輯按鈕。如果你有

<asp:Button ID="btnEdit" CommandName="Edit" runat="server" Text="Edit" /> 

在GridView的<ItemTemplate>內,然後在GridView將自動進入編輯模式,單擊編輯按鈕時。 CommandName Edit是一個特殊的CommandName,用於將GridView置於編輯模式。

如果你想在編輯模式下實現一些特定的行爲,那麼可以通過設置一個OnRowEditing事件處理程序並在這裏實現你的邏輯來實現。這將是這個樣子

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    // Set editing on the row that raised the event 
    GridView1.EditIndex = e.NewEditIndex; 

    /* Insert specific editing logic here */ 

    GridView1.DataBind(); 
} 
+1

用於顯示語法的+1,但略有更正:此行爲不需要OnRowEditing事件處理程序。只有在您打算修改編輯行爲時才需要(例如在取消編輯的情況下)。 – Cerebrus 2009-07-28 12:00:55

3

你只需要創建一個ButtonFieldCommandname設置爲「編輯」(或者,設置在GridView的爲True AutoGenerateEditButton屬性)。

GridView支持預先配置的命令,用於指定一組特定命令名(例如「編輯」,「刪除」,「取消」)的字段。

點擊此按鈕後,GridView將進入「編輯」模式,EditItemTemplate將自動顯示。