曾有用户给Insus.NET留言,下拉式菜单DropDownList在GridView编辑选择。 http://www.cnblogs.com/insus/articles/1411016.html#2053183 ,需要vb.net版本,现在Insus.NET把主要部分的改写了一些,请参考:
xxx.aspx HTML程序:
AllowPaging="true" OnRowDataBound="GridView1_RowDataBound" OnRowCancelingEdit="GridView1_OnCancelCommand"
OnRowEditing="GridView1_OnEditCommand" OnRowUpdating="GridView1_OnUpdateCommand">
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="WhiteSmoke" />
<Columns>
<asp:TemplateField HeaderText="类别">
<HeaderStyle BorderColor="#c0c0c0" BorderWidth="1" Width="50px" />
<ItemStyle BorderColor="#c0c0c0" BorderWidth="1" Width="50px" />
<ItemTemplate>
<%# Eval("TypeName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlMediaType" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑">
<HeaderStyle BorderWidth="1" BorderColor="#c0c0c0" Width="30" />
<ItemStyle BorderWidth="1" BorderColor="#c0c0c0" />
<ItemTemplate>
<asp:ImageButton ID="imgBtnEdit" runat="server" CommandName="Edit" ImageAlign="absmiddle"
ImageUrl="~/Image/edit.gif" CausesValidation="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgBtnUpdate" runat="server" CommandName="Update" ImageAlign="absmiddle"
ImageUrl="~/Image/update.gif" ValidationGroup="vg_Edit" />
<asp:ImageButton ID="imgBtnCancel" runat="server" CommandName="Cancel" ImageAlign="absmiddle"
ImageUrl="~/Image/cancel.gif" CausesValidation="false" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
xxx.aspx.vb代码,代码有中文注释:
Inherits System.Web.UI.Page
'数据源的对象
Dim objMedia As New Media()
'DropDownList数据源的对象
Dim objMediaType As New MediaType()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Data_Binding()
End If
End Sub
'数据绑定,建议不要直接写在page_Load事件中
Private Sub Data_Binding()
'数据源绑定到GriwView
Me.GridView1.DataSource = objMedia.GetMediaAllByIsActive(True)
Me.GridView1.DataBind()
End Sub
'绑定GridView控件中的DropDownlist
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
'下面这个类别,请参考Insus.NET的博客http://www.cnblogs.com/insus/articles/1399645.html
Dim objListControlUtility As New ListControlUtility()
If e.Row.RowType = DataControlRowType.DataRow Then
'记得要写下面这个判断,如果找不到GridView中的DropDownList会发生程序运行异常
If e.Row.FindControl("ddlMediaType") IsNot Nothing Then
'找到GridView的DorpDownList的对象并转DorpDownList为控件
Dim ddl As DropDownList = CType(e.Row.FindControl("ddlMediaType"), DropDownList)
'对DropDownList数据源绑定
objListControlUtility.DropDownListParse(ddl, objMediaType.GetMediaTypeByIsActive(), "TypeName", "MediaTypeId", DataBinder.Eval(e.Row.DataItem, "MediaTypeId").ToString(), "--请选择--")
End If
End If
End Sub
Protected Sub GridView1_OnEditCommand(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
'编辑记录,让程式知道是编辑哪一行。
GridView1.EditIndex = e.NewEditIndex
'绑定数据
Data_Binding()
End Sub
Protected Sub GridView1_OnCancelCommand(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
'Cancel编辑,回到数据绑定状态
GridView1.EditIndex = -1
'绑定数据
Data_Binding()
End Sub
Protected Sub GridView1_OnUpdateCommand(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
'找到记录的主键
Dim pk As String = GridView1.DataKeys(e.RowIndex).Value.ToString()
'找到DropDownList的选择的值
Dim mediaTypeId As String = CType(GridView1.Rows(e.RowIndex).FindControl("ddlMediaType"), DropDownList).SelectedItem.Value
'下面是其它字段或更新处理事件。
End Sub
End Class