cascadierende DropDownList an DataList übergeben

Lothar Rappsilber 1 Reputation point
2021-11-01T14:50:27.763+00:00

Hallo,

ich habe eine kaskadierende DropDownList, die funktioniert (Land, Bundesland, Region). Ich möchte das Ergebnis der Region in DataList ausgeben, aber es gelingt mir leider nicht. War mache ich falsch? Mein Code:

                     <asp:TemplateField>  
                     <ItemTemplate>  
                     <asp:DataList ID="DataListName" runat="server" Text='<%# Eval("Name") %>'>  
                     </asp:DataList>  
                     </ItemTemplate>  
                     </asp:TemplateField>  

und danach:

Protected Sub idRegion_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim strConnString As [String] = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim strQuery As [String] = "Select * from Campingplatz where idLand =@LandID and idBundesland = @BundeslandID and idRegion = @RegionID ORDER BY Name ASC"
Dim con As New MySqlConnection(strConnString)
Dim cmd As New MySqlCommand()
cmd.Parameters.AddWithValue("@LandID", MySqlDbType.Decimal).Value = DropDownLand.SelectedItem.Value
cmd.Parameters.AddWithValue("@BundeslandID", MySqlDbType.Decimal).Value = DropDownBundesland.SelectedItem.Value
cmd.Parameters.AddWithValue("@RegionID", MySqlDbType.Decimal).Value = DropDownRegion.SelectedItem.Value
cmd.CommandType = CommandType.Text
cmd.CommandText = strQuery
cmd.Connection = con
Try
con.Open()
DataListName.DataSource = cmd.ExecuteReader()
DataListName.DataBind()
Catch ex As Exception
'Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Sub

Wer kann mir helfen?145347-dropdownlist1.jpg

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,306 questions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,071 Reputation points
    2021-11-02T04:03:36.333+00:00

    Hi @Lothar Rappsilber ,
    According to your description, I'm guessing that you couldn't get the datasource of the datalist. If your problem is this, I suggest you could do like this:

    1. Create a method A to bind the data.
    2. You must use autopostback on the dropdownlist and you must call the method A to bind the data to the datalist in the page load event.
    3. If you want to change the data of the datalist, you could rebound the data in the selectindexchanged of the dropdownlist recalling the method A.

    Note: Please convert your language in English.

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Lothar Rappsilber 1 Reputation point
    2021-11-02T10:01:07.157+00:00

    Das verstehe ich leider nicht. Bitte Code zum Verstehen senden. Danke!


  3. Lothar Rappsilber 1 Reputation point
    2021-11-03T09:51:54.82+00:00

    Ich habe es auf MySQL und vb angepasst, aber wo muss es hin?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            test()
        End If
    End Sub
    
    Protected Sub test()
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
        Dim strQuery As String = "select * from Name where Id=@ID"
        Dim con As MySqlConnection = New SqlConnection(strConnString)
        Dim cmd As MySqlCommand = New MySqlCommand()
        cmd.Parameters.AddWithValue("@ID", MySqlDbType.Decimal).Value = Region.SelectedItem.Value
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strQuery
        cmd.Connection = con
        Try
            con.Open()
            DataListName.DataSource = cmd.ExecuteReader()
            DataListName.DataBind()
            ' Throw ex
        Catch ex As Exception
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Sub
    
    Protected Sub Region_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        test()
    End Sub
    

    ganzer Code:

    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
    Imports MySql.Data
    Imports MySql.Data.MySqlClient

    Public Class hauptseite
    Inherits System.Web.UI.Page

    Public Property MyVariablen As New Variablen()
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            DropDownLand.AppendDataBoundItems = True
            Dim strConnString As [String] = ConfigurationManager _
             .ConnectionStrings("conString").ConnectionString
            Dim strQuery As [String] = "Select LandDE, idLand from Campingplatz Group BY LandDE ORDER BY LandDE ASC"
            Dim con As New MySqlConnection(strConnString)
            Dim cmd As New MySqlCommand()
            cmd.CommandType = CommandType.Text
            cmd.CommandText = strQuery
            cmd.Connection = con
            Try
                con.Open()
                DropDownLand.DataSource = cmd.ExecuteReader()
                DropDownLand.DataTextField = "LandDE"
                DropDownLand.DataValueField = "idLand"
                DropDownLand.DataBind()
            Catch ex As Exception
                'Throw ex
            Finally
                con.Close()
                con.Dispose()
            End Try
        End If
    End Sub
    Protected Sub idLand_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        DropDownBundesland.Items.Clear()
        DropDownBundesland.Items.Add(New ListItem("--Select Bundesland--", ""))
        DropDownRegion.Items.Clear()
        DropDownRegion.Items.Add(New ListItem("--Select Region--", ""))
        DropDownBundesland.AppendDataBoundItems = True
        Dim strConnString As [String] = ConfigurationManager _
                   .ConnectionStrings("conString").ConnectionString
        Dim strQuery As [String] = "Select Bundesland, idBundesland from Campingplatz where idLand = @LandID Group BY Bundesland ORDER BY Bundesland ASC"
        Dim con As New MySqlConnection(strConnString)
        Dim cmd As New MySqlCommand()
        cmd.Parameters.AddWithValue("@LandID", MySqlDbType.Decimal).Value = DropDownLand.SelectedItem.Value
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strQuery
        cmd.Connection = con
        Try
            con.Open()
            DropDownBundesland.DataSource = cmd.ExecuteReader()
            DropDownBundesland.DataTextField = "Bundesland"
            DropDownBundesland.DataValueField = "idBundesland"
            DropDownBundesland.DataBind()
            If DropDownBundesland.Items.Count > 1 Then
                DropDownBundesland.Enabled = True
            Else
                DropDownBundesland.Enabled = False
                DropDownRegion.Enabled = False
            End If
        Catch ex As Exception
            'Throw ex
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Sub
    Protected Sub idBundesland_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        DropDownRegion.Items.Clear()
        DropDownRegion.Items.Add(New ListItem("--Select Region--", ""))
        DropDownRegion.AppendDataBoundItems = True
        Dim strConnString As [String] = ConfigurationManager _
                   .ConnectionStrings("conString").ConnectionString
        Dim strQuery As [String] = "Select Region, idRegion, idBundesland, idLand from Campingplatz where idLand =@LandID and idBundesland = @BundeslandID Group BY Region ORDER BY Region ASC"
        Dim con As New MySqlConnection(strConnString)
        Dim cmd As New MySqlCommand()
        cmd.Parameters.AddWithValue("@LandID", MySqlDbType.Decimal).Value = DropDownLand.SelectedItem.Value
        cmd.Parameters.AddWithValue("@BundeslandID", MySqlDbType.Decimal).Value = DropDownBundesland.SelectedItem.Value
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strQuery
        cmd.Connection = con
        Try
            con.Open()
            DropDownRegion.DataSource = cmd.ExecuteReader()
            DropDownRegion.DataTextField = "Region"
            DropDownRegion.DataValueField = "idRegion"
            DropDownRegion.DataBind()
            If DropDownRegion.Items.Count > 1 Then
                DropDownRegion.Enabled = True
            Else
                DropDownRegion.Enabled = False
    
            End If
        Catch ex As Exception
            'Throw ex
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Sub
    Protected Sub idRegion_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim strConnString As [String] = ConfigurationManager.ConnectionStrings("conString").ConnectionString
        Dim strQuery As [String] = "Select * from Campingplatz where idLand =@LandID and idBundesland = @BundeslandID and idRegion = @RegionID ORDER BY Name ASC"
        Dim con As New MySqlConnection(strConnString)
        Dim cmd As New MySqlCommand()
        cmd.Parameters.AddWithValue("@LandID", MySqlDbType.Decimal).Value = DropDownLand.SelectedItem.Value
        cmd.Parameters.AddWithValue("@BundeslandID", MySqlDbType.Decimal).Value = DropDownBundesland.SelectedItem.Value
        cmd.Parameters.AddWithValue("@RegionID", MySqlDbType.Decimal).Value = DropDownRegion.SelectedItem.Value
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strQuery
        cmd.Connection = con
        Try
            con.Open()
            DataListName.DataSource = cmd.ExecuteReader()
            DataListName.DataBind()
        Catch ex As Exception
            'Throw ex
        Finally
                con.Close()
                con.Dispose()
            End Try
    End Sub
    

    End Class

    Gruss Lothar


  4. Lothar Rappsilber 1 Reputation point
    2021-11-04T11:52:36.427+00:00

    Ich habe es hinbekommen! Danke noch mal für die qualifizierte Hilfe!!!

    0 comments No comments

  5. Lothar Rappsilber 1 Reputation point
    2021-11-04T16:40:37.013+00:00

    Wie kann man den Output zB <%# Eval("Name") %> auf eine Variable setzen?

    If <%# Eval("Name") %> geht ja nicht!

    Gruss Lothar

    0 comments No comments