• DropDownList在GridView中(vb.net)


    曾有用户给Insus.NET留言,下拉式菜单DropDownList在GridView编辑选择。 http://www.cnblogs.com/insus/articles/1411016.html#2053183 ,需要vb.net版本,现在Insus.NET把主要部分的改写了一些,请参考:

    xxx.aspx HTML程序: 

    View Code
    <asp:GridView ID="GridView1" runat="server" SkinID="gridviewSkin" DataKeyNames="MediaId"
                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代码,代码有中文注释: 

    View Code
    Partial Class Management_DropDownListInGridViewDemo
        
    Inherits System.Web.UI.Page

        
    '数据源的对象
        Dim objMedia As New Media()
        
    'DropDownList数据源的对象
        Dim objMediaType As New MediaType()

        
    Protected Sub Page_Load(ByVal sender As ObjectByVal 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 ObjectByVal 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 ObjectByVal e As GridViewEditEventArgs)
            
    '编辑记录,让程式知道是编辑哪一行。
            GridView1.EditIndex = e.NewEditIndex
            
    '绑定数据
            Data_Binding()
        
    End Sub

        
    Protected Sub GridView1_OnCancelCommand(ByVal sender As ObjectByVal e As GridViewCancelEditEventArgs)
            
    'Cancel编辑,回到数据绑定状态
            GridView1.EditIndex = -1
            
    '绑定数据
            Data_Binding()
        
    End Sub

        
    Protected Sub GridView1_OnUpdateCommand(ByVal sender As ObjectByVal 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

     更多相关DropDownList与Gridview参考:

    http://www.cnblogs.com/insus/articles/1399645.html

    http://www.cnblogs.com/insus/articles/1654911.html

    http://www.cnblogs.com/insus/articles/1411016.html

  • 相关阅读:
    Flutter移动电商实战 --(40)路由_Fluro的全局注入和使用方法
    Flutter移动电商实战 --(39)路由_Fluro的路由配置和静态化
    Flutter移动电商实战 --(38)路由_Fluro中Handler编写方法
    Flutter移动电商实战 --(37)路由_Fluro引入和商品详细页建立
    网络基础:NetBIOS
    linux 定时任务
    linux useradd 命令基本用法
    Git undo 操作
    Git 查看文件的历史
    Ubuntu14.04安装postgresql9.4
  • 原文地址:https://www.cnblogs.com/insus/p/1997458.html
Copyright © 2020-2023  润新知