2012-07-29 147 views
0

我想級聯使用ASP和VB下拉級聯問題:如何級聯兩個下拉

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

現在,
如果男性從第一個下拉選擇菜單,然後選擇下拉菜單(孤兒,特別極品,已婚,未婚,離婚,從第2下拉

從第一個下拉選擇向下寡婦和普通)

如果女,然後從第二個下拉

選擇(孤兒,特殊需要和正常)

我得到來自數據庫的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> 

任何一個可以指導我在這方面。謝謝

回答

1

檢查:

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 

如果您對代碼有任何疑問,請讓我知道。

+0

非常感謝CoderRoller的熱情和慷慨的回覆,它確實幫助我,只是告訴我如何解決我的問題,我的答案在下面,但我接受你的答案。 – AbdulAziz 2012-07-29 22:02:38

1

在ddlSexType的選定索引更改事件綁定第二個下拉列表。 檢查第一個下拉菜單中選定的項目,如果它等於男性,則綁定相應的項目。 例子:

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 
    } 
} 
+0

謝謝你的回答,但這不是確切的情況。我正在嘗試篩選數據,以及選擇男性時。 – AbdulAziz 2012-07-29 13:20:46

+0

@AbdulAziz好的。您將爲第二個下拉菜單綁定的數據存在於單個表格中,並且您想過濾它們? – 2012-07-29 13:33:54

+0

是的,這是我想要完成的。我的意思是,如果我選擇「女性」,那麼可以選擇表中的所有項目,但如果我選擇「男性」,它只會過濾一些項目。我真的很感謝你的幫助。 – AbdulAziz 2012-07-29 18:53:29

0

這是我如何解決我的問題

我只是添加三個字段在我的表

1- ISMALE 

2- ISFEMALE 

3- COMMON 

而且使COMMON,如果男性和女性的狀態是共同的。

而且使ISMALE和ISFEMALE 男性狀態。

而且使ISFEMALE和ISMALE 女性狀態。

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,所以如果有人認爲這一個漫長的方法,然後親切地更新我更好的辦法。再次

由於瓦卡揚尤亞CoderRoller的寶貴時間和專業知識。

+0

不用客氣 – 2012-07-29 22:31:44

+0

隨時歡迎阿卜杜勒! – CoderRoller 2012-07-29 22:35:14