首页 > 解决方案 > 使用 SQL 数据库在 Gridview 编辑模式下级联下拉列表

问题描述

我尝试在 GridView 中以级联模式使用四个下拉列表进行数据编辑。我已经在三个列表中成功实现了级联功能,但第四个列表不起作用。我的 aspx 页面和 VB 代码是

<%@ Page Language="VB"  MaintainScrollPositionOnPostback="true" Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<style>
body
{ font-family:verdana;
font-size:10pt;
}
</style>

<script runat="server">

Protected Sub ddlclassification_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim ddlclassification, ddlbase, ddlLocoType, ddlEquipment As DropDownList
    Dim ShedBYClassification As SqlDataSource
    Dim LocoTypeBYShed As SqlDataSource
    Dim EquipBYLocoType As SqlDataSource
    Dim currentRowInEdit As Integer = GridView1.EditIndex
    ddlclassification = CType(sender, DropDownList)
    ddlbase = CType(GridView1.Rows(currentRowInEdit).FindControl("ddlbase"), DropDownList)
    ddlLocoType = CType(GridView1.Rows(currentRowInEdit).FindControl("ddlLocoType"), DropDownList)
    ddlEquipment = CType(GridView1.Rows(currentRowInEdit).FindControl("ddlEquipment"), DropDownList)
    ShedBYClassification = CType(GridView1.Rows(currentRowInEdit).FindControl("ShedBYClassification"), SqlDataSource)
    ShedBYClassification.SelectParameters("ClassificationName").DefaultValue = ddlclassification.SelectedValue
    LocoTypeBYShed = CType(GridView1.Rows(currentRowInEdit).FindControl("LocoTypeBYShed"), SqlDataSource)
    LocoTypeBYShed.SelectParameters("ClassificationName").DefaultValue = ddlclassification.SelectedValue
    EquipBYLocoType = CType(GridView1.Rows(currentRowInEdit).FindControl("EquipBYLocoType"), SqlDataSource)
    EquipBYLocoType.SelectParameters("LocoTypeName").DefaultValue = ddlLocoType.SelectedValue
    ddlbase.DataBind()
    ddlLocoType.DataBind()
    ddlEquipment.DataBind()
    
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e 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
        Dim ddlclassification As DropDownList = e.Row.FindControl("ddlclassification")
        ddlclassification.SelectedValue = dv("classificationName")
        Dim ddlbase As DropDownList = e.Row.FindControl("ddlbase")
        Dim ddlLocoType As DropDownList = e.Row.FindControl("ddlLocoType")
        Dim ddlEquipment As DropDownList = e.Row.FindControl("ddlEquipment")
        Dim dsc As SqlDataSource = e.Row.FindControl("ShedBYClassification")
        Dim dsclocotype As SqlDataSource = e.Row.FindControl("LocoTypeBYShed")
        Dim dscEquipment As SqlDataSource = e.Row.FindControl("EquipBYLocoType")
        dsc.SelectParameters("ClassificationName").DefaultValue = dv("classificationName")
        dsclocotype.SelectParameters("ClassificationName").DefaultValue = dv("ClassificationName")
        dscEquipment.SelectParameters("LocoTypeName").DefaultValue = dv("LocoTypeName")
        ddlbase.DataBind()
        ddlLocoType.DataBind()
        ddlEquipment.DataBind()
        ddlbase.SelectedValue = dv("Shed")
        ddlLocoType.SelectedValue = dv("LocoTypeName")
        ddlEquipment.SelectedValue = dv("EquipName")
    End If
End Sub

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
    Dim ddlclassification As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("ddlclassification"), DropDownList)
    Dim ddlbase As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("ddlbase"), DropDownList)
    Dim ddlLocoType As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("ddlLocoType"), DropDownList)
    Dim ddlEquipment As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("ddlEquipment"), DropDownList)
    e.NewValues("classification") = ddlclassification.SelectedValue
    e.NewValues("Shed") = ddlbase.SelectedValue
    e.NewValues("loco_type") = ddlLocoType.SelectedValue
    e.NewValues("Equipt") = ddlEquipment.SelectedValue
End Sub

</script>

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head id="Head1" runat="server">
<title>Cascading DropDownList Controls in a GridView Edit Mode</title>
 </head>
<body>
<form id="form1" runat="server">
    <div>
        <div>TEST</div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="ID" DataSourceID="SqlDataSource1"
            HorizontalAlign="Center" AllowSorting="True" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating"> 

            <Columns>
                <asp:CommandField ShowDeleteButton="False" ShowEditButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
                    ReadOnly="True" SortExpression="ID" Visible="false" />
                
                <asp:TemplateField HeaderText="Date" SortExpression="Date">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server"
                            Text='<%# Bind("Date", "{0:dd-MMM-yy}") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Label ID="TextBox3" runat="server" Text='<%# Bind("Date", "{0:MM/dd/yyyy}") %>' ></asp:Label>
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
                </asp:TemplateField>
                
                <asp:TemplateField HeaderText="Classification of Case" SortExpression="classification">
                    <ItemTemplate>
                        <asp:Label ID="Label9" runat="server" Text='<%# Bind("classification") %>'></asp:Label>
                    </ItemTemplate>
                     <EditItemTemplate>
                           <asp:DropDownList ID="ddlclassification" runat="server" DataSourceID="ClassificationDDL" DataTextField="classificationName"
                            DataValueField="classificationName" AutoPostBack="true" OnSelectedIndexChanged="ddlclassification_SelectedIndexChanged">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="ClassificationDDL" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString  %>" 
                    SelectCommand="SELECT [ClassificationName] FROM [ClassificationDDL] ORDER BY [ClassificationName]" 
                    EnableCaching="True">
                </asp:SqlDataSource>
                        </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Loco no." SortExpression="Loco">
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("Loco") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Loco") %>' ></asp:TextBox>
                        
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" 
                        Wrap="True" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Home Rly" SortExpression="Homerly">
                    <ItemTemplate>
                        <asp:Label ID="Label7" runat="server" Text='<%# Bind("Homerly") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Label ID="ddlRly" runat="server" Text='<%# Bind("Homerly") %>'></asp:Label>
                        
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Shed" SortExpression="Shed">
                    <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("Shed") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlbase" runat="server" DataSourceID="ShedBYClassification" DataTextField="ShedName"  DataValueField="ShedName" AutoPostBack="true" />
                        <asp:SqlDataSource ID="ShedBYClassification" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString %>" 
                    SelectCommand="SELECT [ShedID], [ShedName] FROM [ShedDDL] WHERE ([ClassificationID] = @ClassificationName) ORDER BY ShedName" EnableCaching="True">
                    <SelectParameters>
                        <asp:Parameter Name="ClassificationName" />
                    </SelectParameters>
                            </asp:SqlDataSource>             
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Loco Type" SortExpression="loco_type">
                    <ItemTemplate>
                        <asp:Label ID="Label11" runat="server" Text='<%# Bind("loco_type") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlLocoType" runat="server" DataSourceID="LocoTypeBYShed" DataTextField="LocoTypeName"  DataValueField="LocoTypeName"
                             AutoPostBack="true" />
                        <asp:SqlDataSource ID="LocoTypeBYShed" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString %>" 
                    SelectCommand="SELECT [LocoTypeID], [LocoTypeName] FROM [LocoTypeDDL] WHERE ([ClassificationID] = @ClassificationName) ORDER BY LocoTypeName" EnableCaching="True">
                    <SelectParameters>
                        <asp:Parameter Name="ClassificationName" />
                    </SelectParameters>
                            </asp:SqlDataSource>
                    </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Equipment" SortExpression="Equipt">
                    <ItemTemplate>
                        <asp:Label ID="Label8" runat="server" Text='<%# Bind("Equipt") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlEquipment" runat="server" DataSourceID="EquipBYLocoType" DataTextField="EquipName"  DataValueField="EquipName"
                             AutoPostBack="true"  />
                        <asp:SqlDataSource ID="EquipBYLocoType" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString %>" 
                    SelectCommand="SELECT [EquipID], [EquipName] FROM [EquipmentDDL] WHERE ([LocoTypeID] = @LocoTypeName) ORDER BY EquipName" EnableCaching="True">
                    <SelectParameters>
                        <asp:Parameter Name="LocoTypeName" />
                    </SelectParameters>
                            </asp:SqlDataSource>

                        </EditItemTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
                </asp:TemplateField>
                </Columns>
            <FooterStyle Font-Bold="True" Font-Names="Tahoma"
                Font-Size="Medium" HorizontalAlign="Center" VerticalAlign="Middle" />
            <HeaderStyle Font-Names="Tahoma"
                Font-Size="Small" />
            <RowStyle Font-Names="Tahoma" Font-Size="Small" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:EquipfailuresERConnectionString %>"
            SelectCommand="TESTINNER" SelectCommandType="StoredProcedure" UpdateCommand="EnRRlyupdate" UpdateCommandType="StoredProcedure">
            
        </asp:SqlDataSource>
                    <br />
    </div>
</form>
</body>
</html>

下拉列表的 SQL 数据如下:

第一个下拉列表 - 'ClassificationDDL' SQL 表

  +------------------------+------------------------+
  |    ClassificationID    |   ClassificationName   |
  +:----------------------:+:----------------------:+
  | Loco                   | Loco                   |
  | Crew/Loco Operation    | Crew/Loco Operation    |
  | Traffic/wrongly logged | Traffic/wrongly logged |
  | Stalling               | Stalling               |
  +------------------------+------------------------+

第二个下拉列表 - 'ShedDDL' SQL 表

  +------------------------+----------+--------+
  |    ClassificationID    | ShedName | ShedID |
  +:----------------------:+:--------:+:------:+
  | Loco                   | BSL      | BSL    |
  | Loco                   | AQ       | AQ     |
  | Loco                   | KYN      | KYN    |
  | Stalling               | Other    | Other  |
  | Stalling               | BSL      | BSL    |
  | Stalling               | AQ       | AQ     |
  | Stalling               | KYN      | KYN    |
  | Crew/Loco Operation    | Other    | Other  |
  | Crew/Loco Operation    | BSL      | BSL    |
  | Crew/Loco Operation    | AQ       | AQ     |
  | Crew/Loco Operation    | KYN      | KYN    |
  | Traffic/wrongly logged | BSL      | BSL    |
  | Traffic/wrongly logged | AQ       | AQ     |
  | Traffic/wrongly logged | KYN      | KYN    |
  | Traffic/wrongly logged | Other    | Other  |
  +------------------------+----------+--------+

第三个下拉列表 - 'LocoTypeDDL' SQL 表

  +------------------------+--------------+--------------+
  |    ClassificationID    |  LocoTypeID  | LocoTypeName |
  +:----------------------:+:------------:+:------------:+
  | Loco                   | Conventional | Conventional |
  | Loco                   | 3-Phase      | 3-Phase      |
  | Crew/Loco Operation    | Conventional | Conventional |
  | Crew/Loco Operation    | 3-Phase      | 3-Phase      |
  | Crew/Loco Operation    | Other        | Other        |
  | Traffic/wrongly logged | Conventional | Conventional |
  | Traffic/wrongly logged | 3-Phase      | 3-Phase      |
  | Traffic/wrongly logged | Other        | Other        |
  | Stalling               | Conventional | Conventional |
  | Stalling               | 3-Phase      | 3-Phase      |
  | Stalling               | Other        | Other        |
  +------------------------+--------------+--------------+

第 4 个下拉列表 - 'EquipmentDDL' SQL 表

  +--------------+---------------------------+---------------------------+
  |  LocoTypeID  |         EquipName         |          EquipID          |
  +:------------:+:-------------------------:+:-------------------------:+
  | Other        | Other                     | Other                     |
  | Conventional | Air Blast Circuit Breaker | Air Blast Circuit Breaker |
  | Conventional | Air Dryer                 | Air Dryer                 |
  | Conventional | Arno                      | Arno                      |
  | Conventional | Aux. compressor           | Aux. compressor           |
  | Conventional | Auxiliary Motor           | Auxiliary Motor           |
  | Conventional | Axle Box                  | Axle Box                  |
  | Conventional | Battery                   | Battery                   |
  | 3-Phase      | 110V MCB                  | 110V MCB                  |
  | 3-Phase      | 415V  MCB                 | 415V  MCB                 |
  | 3-Phase      | Air Dryer                 | Air Dryer                 |
  | 3-Phase      | Angle transmitter         | Angle transmitter         |
  | 3-Phase      | Aux. compressor           | Aux. compressor           |
  | 3-Phase      | Aux. converter GTO (BUR)  | Aux. converter GTO (BUR)  |
  +--------------+---------------------------+---------------------------+

选择命令(存储过程“TESTINNER”)是

  Select ERfailures.ID, ERfailures.Date, ERfailures.Loco, ERfailures.Shed, ERfailures.Homerly, ERfailures.Equipt, ERfailures.classification, ERfailures.Sub_head, ERfailures.loco_type, ClassificationDDL.ClassificationName, ShedDDL.ShedName, LocoTypeDDL.LocoTypeName, EquipmentDDL.EquipName from ERfailures 
  INNER JOIN ClassificationDDL ON ERfailures.classification = ClassificationDDL.ClassificationName
  INNER JOIN ShedDDL ON ERfailures.Shed = ShedDDL.ShedName AND ERfailures.classification = ShedDDL.ClassificationID
  INNER JOIN LocoTypeDDL ON ERfailures.loco_type = LocoTypeDDL.LocoTypeName AND ERfailures.classification =  LocoTypeDDL.ClassificationID
  INNER JOIN EquipmentDDL ON ERfailures.Equipt = EquipmentDDL.EquipName AND ERfailures.loco_type = EquipmentDDL.LocoTypeID

更新命令(存储过程)“EnRRlyupdate”是

  UPDATE ERfailures SET Date = @Date, Loco = @Loco, Shed = @Shed, Homerly = (CASE WHEN @Shed ='BSL' THEN 'CR' WHEN @Shed ='AQ' THEN 'ER' WHEN @Shed ='KYN' THEN 'ECR' ELSE 'xyz' END), Equipt = @Equipt, classification = @classification, loco_type = @loco_type WHERE ID = @ID

第二个下拉列表(ddlbase)和第三个下拉列表(ddlLocoType)基于第一个下拉列表(ddlclassification)中的选定值。第 4 个下拉列表 (ddlEquipment) 基于第 3 个下拉列表中的选定值。第 1、第 2 和第 3 下拉菜单工作正常,但第 4 下拉菜单不起作用(表示值未根据第 3 下拉列表中选择的值显示。请帮助使该项目可行。

标签: asp.netvb.netgridviewcascadingdropdowneditmode

解决方案


推荐阅读