question

LotharRappsilber-3215 avatar image
0 Votes"
LotharRappsilber-3215 asked LotharRappsilber-3215 answered

cascadierende DropDownList an DataList übergeben

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-supported
dropdownlist1.jpg (85.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @LotharRappsilber-3215 ,
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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LotharRappsilber-3215 avatar image
0 Votes"
LotharRappsilber-3215 answered YijingSun-MSFT edited

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @LotharRappsilber-3215 ,
Please use English!
I have created a simple demo,you could do like this:

    Class SurroundingClass
     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("dbconnection").ConnectionString
         Dim strQuery As String = "select * from Name where Id=@ID"
         Dim con As SqlConnection = New SqlConnection(strConnString)
         Dim cmd As SqlCommand = New SqlCommand()
         cmd.Parameters.AddWithValue("@ID", Region.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
         Finally
             con.Close()
             con.Dispose()
         End Try
     End Sub
    
     Protected Sub Region_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
         test()
     End Sub
 End Class

Best regards,
Yijing Sun

0 Votes 0 ·
LotharRappsilber-3215 avatar image
0 Votes"
LotharRappsilber-3215 answered YijingSun-MSFT commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @LotharRappsilber-3215 ,
Please use English! I don't understand what's your meanings.
Best regards,
Yijing Sun

0 Votes 0 ·
LotharRappsilber-3215 avatar image
0 Votes"
LotharRappsilber-3215 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LotharRappsilber-3215 avatar image
0 Votes"
LotharRappsilber-3215 answered

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

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

Gruss Lothar

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LotharRappsilber-3215 avatar image
0 Votes"
LotharRappsilber-3215 answered

Wenn ich DataListName.DataBind() eingebe:

 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

macht er mir die Tabelle kaputt. Wie verhindere ich das?


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.