注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

伯纳乌の夢

可以挽回么?我们按“ Ctrl+Z”撤销掉吧。对不起啦~~

 
 
 

日志

 
 
 
 

[Fw]GridView 编辑状态下下框架级联变动  

2011-05-29 16:28:31|  分类: Web |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Cascading DropDownList Controls in Edit Mode of a GridView Control
[TrackBack] http://www.mikepope.com/blog/fun/CascadingDropDowns_GridView.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
       
Protected Sub listManufacturers_SelectedIndexChanged(ByVal sender As ObjectByVal As System.EventArgs)
        
Dim listManufacturers, listModels As DropDownList
        
Dim dsModelsByManufacturer As AccessDataSource
        
Dim currentRowInEdit As Integer = GridView1.EditIndex
        listManufacturers 
= CType(sender, DropDownList)
        listModels 
= CType(GridView1.Rows(currentRowInEdit).FindControl("listModels"), DropDownList)
        dsModelsByManufacturer 
= CType(GridView1.Rows(currentRowInEdit).FindControl("dsModelsByManufacturer"), AccessDataSource)

        dsModelsByManufacturer.SelectParameters(
"manufacturerID").DefaultValue listManufacturers.SelectedValue
        listModels.DataBind()
    
End Sub

    Protected Sub 
GridView1_RowDataBound(ByVal sender As ObjectByVal As System.Web.UI.WebControls.GridViewRowEventArgs)
        
If (e.Row.RowState And DataControlRowState.Edit) DataControlRowState.Edit Then
            Dim 
dv As System.Data.DataRowView e.Row.DataItem
           
' Preselect correct value in Manufacturers list
            
Dim listManufacturers As DropDownList e.Row.FindControl("listManufacturers")
            listManufacturers.SelectedValue 
dv("ManufacturerID")
           
' Databind list of models in dependent drop-down list, preselect value
            
Dim listModels As DropDownList e.Row.FindControl("listModels")
            
Dim dsc As AccessDataSource e.Row.FindControl("dsModelsByManufacturer")
            dsc.SelectParameters(
"ManufacturerID").DefaultValue dv("ManufacturerID")
            listModels.DataBind()
            listModels.SelectedValue 
dv("ModelID")
        
End If
    End Sub
    
    Protected Sub 
GridView1_RowUpdating(ByVal sender As ObjectByVal As System.Web.UI.WebControls.GridViewUpdateEventArgs)
        
Dim listManufacturers As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("listManufacturers"), DropDownList)
        
Dim listModels As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("listModels"), DropDownList)
        e.NewValues(
"ManufacturerID"listManufacturers.SelectedValue
        e.NewValues(
"ModelID"listModels.SelectedValue
    
End Sub
</
script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Cascading DropDownList Controls in a GridView Control</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<br />
        <
h1>Cascading DropDownList Controls in Edit Mode of a GridView Control</h1>
        
<p>This page illustrates one way to create cascading (dependent) DropDownList controls 
           in edit mode of a GridView control. Blog post about this 
           
<a href="http://mikepope.com/blog/DisplayBlog.aspx?permalink=1708">here</a>.</p>
        
<asp:GridView ID="GridView1" 
            runat
="server" 
            AutoGenerateColumns
="False"
            DataKeyNames
="ItemID" 
            DataSourceID
="dsCarsForSale" 
            OnRowDataBound
="GridView1_RowDataBound" 
            OnRowUpdating
="GridView1_RowUpdating" 
             
>
            
            
<Columns>
                
<asp:CommandField ShowEditButton="True" />
                <
asp:BoundField 
                    DataField
="ItemID" 
                    HeaderText
="ItemID" 
                    InsertVisible
="False" 
                    ReadOnly
="True"
                    SortExpression
="ItemID" />

                <
asp:TemplateField HeaderText="Manufacturer" SortExpression="Manufacturer">
                    
<ItemTemplate>
                        
<asp:Label ID="Label1" 
                            runat
="server" 
                            Text
='<%# Bind("Manufacturer") %>'>
                        
</asp:Label>   
                    
</ItemTemplate>

                    
<EditItemTemplate>
                        
<asp:DropDownList 
                            ID
="listManufacturers" 
                            runat
="server" 
                            DataSourceID
="dsManufacturers" 
                            DataTextField
="Manufacturer" 
                            DataValueField
="ManufacturerID" 
                            autopostback
=true
                            OnSelectedIndexChanged
="listManufacturers_SelectedIndexChanged">
                        
</asp:DropDownList>
                         
<asp:AccessDataSource ID="dsManufacturers" 
                             runat
="server" 
                             DataFile
="~/App_Data/Cars.mdb"
                             SelectCommand
="SELECT [ManufacturerID], [Manufacturer] FROM [Manufacturers]">
                         
</asp:AccessDataSource>
                    
</EditItemTemplate>
                
</asp:TemplateField>
                
                
<asp:TemplateField HeaderText="Model" SortExpression="ModelName">
                    
<ItemTemplate>
                        
<asp:Label ID="Label2" 
                        runat
="server" 
                        Text
='<%# Bind("ModelName") %>'>
                        
</asp:Label>
                    
</ItemTemplate>

                    
<EditItemTemplate>
                       
<asp:DropDownList ID="listModels" 
                          runat
="server"
                          DataSourceID
="dsModelsByManufacturer"
                          DataTextField
="ModelName" 
                          DataValueField
="ModelID" />

                      <
asp:AccessDataSource 
                         ID
="dsModelsByManufacturer" 
                         runat
="server" 
                         DataFile
="~/App_Data/Cars.mdb"
                         SelectCommand
="SELECT [ModelID], [ModelName] FROM [Models] WHERE ([ManufacturerID] = ?)">
                         
<SelectParameters>
                             
<asp:Parameter Name="ManufacturerID" />
                         </
SelectParameters>
                      
</asp:AccessDataSource>
                    
</EditItemTemplate>
                
</asp:TemplateField>
            
</Columns>
        
</asp:GridView>
        
        
<asp:AccessDataSource ID="dsCarsForSale" 
            runat
="server" 
            DataFile
="~/App_Data/Cars.mdb"
            SelectCommand
="SELECT CarsForSale.ItemID, CarsForSale.ManufacturerID, 
                     CarsForSale.ModelID, Manufacturers.Manufacturer, Models.ModelName
                 FROM (CarsForSale 
                    INNER JOIN Manufacturers ON CarsForSale.ManufacturerID = Manufacturers.ManufacturerID) 
                 INNER JOIN Models ON CarsForSale.ModelID = Models.ModelID;"

            UpdateCommand
="UPDATE [CarsForSale] 
                SET [ManufacturerID] = ?, [ModelID] = ? 
                WHERE [ItemID] = ?"
 >
            
<UpdateParameters>
                
<asp:Parameter Name="ManufacturerID" Type="Int32" />
                <
asp:Parameter Name="ModelID" Type="Int32" />
                <
asp:Parameter Name="ItemID" Type="Int32" />
            </
UpdateParameters>
        
</asp:AccessDataSource>
        
</div>
    
</form>
</body>
</html>

Colorized by: CarlosAg.CodeColorizer
  评论这张
 
阅读(404)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017