问题
I have a asp:GridView and in there i have two columns , in one column i want to show label but when i click an sdit button i want to show a drop down list in that particular column, i have created the grid view like following:
<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>
what should i do to do it? What should i write in edit button's click event? Please help..
回答1:
It depends on how you are setting up the Edit button. If you have
<asp:Button ID="btnEdit" CommandName="Edit" runat="server" Text="Edit" />
within an <ItemTemplate> in the GridView, then the Gridview will automatically go into Edit mode when the Edit button is clicked. The CommandName Edit is a special CommandName to put a GridView into edit mode.
If you wanted to implement some specific behaviour in edit mode, then this can be achieved by setting up an OnRowEditing event handler and implement your logic here. This would look something like this
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();
}
回答2:
You only need to create a ButtonField with Commandname set to "Edit" (alternatively, set the AutoGenerateEditButton property of the GridView to True).
The GridView supports preconfigured commands for fields that specify a specific set of CommandNames (such as "Edit", "Delete", "Cancel").
When this button is clicked, your GridView will go into "Edit" mode and the EditItemTemplate will automatically be displayed.
来源:https://stackoverflow.com/questions/1193585/edittemplate-property-of-aspgridview-control