0

我正在尝试使用 ASP 和 VB 级联下拉菜单

1st drop down   --> Sex Type
                            > Male
                            > Female
2nd Drop Down   -->Status
                            > Orphan
                            > Married
                            > UnMarried
                            > Divorced
                            > Widow
                            > Special Need
                            > Normal

现在,
如果从第一个下拉列表中选择男性,则从第二个下拉列表中选择(孤儿、特殊需要、已婚、未婚、离婚、寡妇和正常)

如果从第一个下拉列表中选择女性,则从第二个下拉列表中选择(孤儿、特殊需要和正常)

我正在使用实体从数据库中获取 drop dwons 的数据;

VB代码

context As New EGrantsModel.Entities

ddlSexType.DataSource = context.SEXTYPE
ddlSexType.DataValueField = "SEXTYPE"
ddlSexType.DataTextField = "SEXTYPE"
ddlSexType.DataBind()

ddlCitizenStatus.DataSource = context.CITIZENSTATUS
ddlCitizenStatus.DataValueField = "CITIZENSTATUSID"
ddlCitizenStatus.DataTextField = "CITIZENSTATUS"
ddlCitizenStatus.DataBind()

ASP 代码

<div class="labelinput">
 <asp:Label ID="lblSexType" CssClass="lblwidth" runat="server" Text="">Sex Type</asp:Label>
 <asp:DropDownList ID="ddlSexType" runat="server"/>
   </div>

 <div class="labelinput">
   <asp:Label ID="lblCitizenStatus" CssClass="lblwidth" runat="server" Text="">Status</asp:Label>
    <asp:DropDownList ID="ddlCitizenStatus" runat="server"/>
     </div>

任何人都可以在这方面指导我。谢谢

4

3 回答 3

1

Check this:

Public Class WebForm4
    Inherits System.Web.UI.Page

    Private Sub CitizenBind(ByVal strSexType As String)

        Dim lstCitizenStatus As New List(Of CitizenStatus)

        With lstCitizenStatus
            .Add(New CitizenStatus("1", "Orphan"))
            .Add(New CitizenStatus("2", "Married"))
            .Add(New CitizenStatus("3", "UnMarried"))
            .Add(New CitizenStatus("4", "Divorced"))
            .Add(New CitizenStatus("5", "Widow"))
            .Add(New CitizenStatus("6", "Special Need"))
            .Add(New CitizenStatus("7", "Normal"))
        End With

        Me.ddlCitizenStatus.Items.Clear()

        With Me.ddlCitizenStatus
            .DataSource = lstCitizenStatus
            .DataValueField = "CitizenStatusId"
            .DataTextField = "CitizenStatusT"
            .DataBind()
        End With

        If strSexType.Contains("F") Then

            Dim lstCitizenStatusRemove As New List(Of CitizenStatus)
            With lstCitizenStatusRemove
                .Add(New CitizenStatus("2", "Married"))
                .Add(New CitizenStatus("3", "UnMarried"))
                .Add(New CitizenStatus("4", "Divorced"))
                .Add(New CitizenStatus("5", "Widow"))
            End With

            For Each lstCS As CitizenStatus In lstCitizenStatusRemove
                Me.ddlCitizenStatus.Items.Remove(New ListItem(lstCS.CitizenStatusT.ToString(), lstCS.CitizenStatusId.ToString()))
            Next

        End If

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            Dim lstSexType As New List(Of SexType)
            lstSexType.Add(New SexType("Male"))
            lstSexType.Add(New SexType("Female"))

            With Me.ddlSexType
                .DataSource = lstSexType
                .DataValueField = "SexTypeT"
                .DataTextField = "SexTypeT"
                .DataBind()
            End With

            CitizenBind("ALL")

        End If

    End Sub

    Protected Sub ddlSexType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlSexType.SelectedIndexChanged

        CitizenBind(Me.ddlSexType.SelectedValue)

    End Sub

End Class

If you have any questions about the code please let me know.

于 2012-07-29T19:51:58.870 回答
1

在 ddlSexType 的 Selected index change 事件上绑定第二个下拉列表。检查第一个下拉列表中的选定项目是否等于男性,然后绑定相应的项目。例子:

protected void ddlSexType_SelectedIndexChanged(object sender, EventArgs e)
{    
   if( ddlSexType.Text == "Male" )
   {
     // Bind 2nd drop down
     // you can add items manually as 
      ListItem li = new ListItem();
      li.Text = "Orphan";

      ddlCitizenStatus.Items.Add(li);
      // Add all the items in the same way

    // or get data from the database and bind it to the dropdown

    DataTable dt = new DataTable();
    ddlCitizenStatus.DataSource = dt;
    ddlCitizenStatus.DataTextFeild= "DataFeild";
    ddlCitizenStatus.DataValueFeild= "DataValueFeild";
    ddlCitizenStatus.DataBind();
   }
   else if( ddlSexType.Text == "Female" )
  {
       // Bind other items
   }
}
于 2012-07-29T13:05:44.227 回答
0

这就是我解决问题的方法

我只是在我的表中添加三个字段

1- ISMALE

2- ISFEMALE

3- COMMON

如果男性和女性身份相同,则设为True COMMON

并将男性身份设为ISMALE True和 ISFEMALE False

并为女性身份设置ISFEMALE True和 ISMALE False

VB代码

Protected Sub ddlSexType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSexType.SelectedIndexChanged
    Try

        Dim context As New EGrantsModel.Entities
        If (ddlSexType.SelectedValue = "Female") Then
            Dim femaleDataSource As Object
            femaleDataSource = (From femaleDS In context.CITIZENSTATUS _
                                Where femaleDS.ISFEMALE = "1" And femaleDS.ISMALE = "0" AndAlso femaleDS.COMMON = "1"
                                Select femaleDS)
            ddlCitizenStatus.DataSource = femaleDataSource
            ddlCitizenStatus.DataValueField = "CITIZENSTATUSID"
            ddlCitizenStatus.DataTextField = "CITIZENSTATUS"
            ddlCitizenStatus.DataBind()
        ElseIf (ddlSexType.SelectedValue = "Male") Then
            Dim maleDataSource As Object
            maleDataSource = (From femaleDS In context.CITIZENSTATUS _
                                Where femaleDS.ISFEMALE = "0" And femaleDS.ISMALE = "1" AndAlso femaleDS.COMMON = "1"
                                Select femaleDS)
            ddlCitizenStatus.DataSource = maleDataSource
            ddlCitizenStatus.DataValueField = "CITIZENSTATUSID"
            ddlCitizenStatus.DataTextField = "CITIZENSTATUS"
            ddlCitizenStatus.DataBind()
        End If
    Catch ex As Exception
        callErrorPage(ex.Message, ex.InnerException)
    End Try
End Sub

我对 VB 真的很陌生,所以如果有人认为这是一个冗长的方法,那么请更新我以获得更好的方法。

再次感谢Waqar JanjuaCoderRoller的宝贵时间和专业知识。

于 2012-07-29T22:13:28.703 回答