VB.NET-保持下拉列表状态而不将代码放入“Not IsPostBack”中

发布于 2024-11-24 01:45:40 字数 1378 浏览 1 评论 0原文

我有 2 个下拉列表“国家”和“城市”,有 2 个数据源:

  1. 如果用户选择 ddl 国家中的第一个索引,他可以看到 ddl 城市中世界上的所有城市(使用 datasource1)。< /p>

  2. 如果用户选择一个国家/地区,他可以看到与所选国家/地区相对应的城市(使用数据源2)。 我将更改数据源的代码放在使用vb.net后面的代码中,在Page_Load中,但是当用户选择一个城市并单击提交按钮后,下拉列表城市无法保持状态,它会转到第一个索引这个ddl。

我尝试将此代码放入 If Not IdPostBack 中,但这样,它不会更改数据源,同时可以保留下拉列表的状态。

那么有人对这个问题有想法吗?

我将代码放在这里作为参考:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ddlCities As DropDownList
Dim ddlCountries As DropDownList
ddlCities = CType(PN_Search.FindControl("DropDownList_Cities"), System.Web.UI.WebControls.DropDownList)
ddlCountries = CType(PN_Search.FindControl("DropDownList_Countries"), System.Web.UI.WebControls.DropDownList)
Dim countrySelect As String
countrySelect = ddlCountries.SelectedValue
Dim rechercheCitiesNull As String = "SELECT * FROM Cities WHERE id_city=1"
Dim rechercheCitiesNotNull As String = "SELECT * FROM [View_Country_City] Where id_country=" & countrySelect 

If countrySelect = "" Then
Me.RechercheCitiesDS.SelectCommand = rechercheCitiesNull
ddlCities.EnableViewState = True
ddlCountries.EnableViewState = True
ddlCities.DataBind()
ElseIf countrySelect <> "" Then
Me.RechercheCitiesDS.SelectCommand = rechercheCitiesNotNull
ddlCities.DataBind()

End If
End Sub

提前致谢! 子流

I have 2 dropdownlists "countries" and "cities", with 2 datasources:

  1. If a user chooses the first index in the ddl countries, he can see all the cities in the world in the ddl cities (uses datasource1).

  2. If a user chooses a country, he can see the cities correspond to this country chosen (uses datasource2).
    I put the code for changing datasource in the code behind using vb.net, in Page_Load, but after the user chose a city, and click the submit button, the dropdownlist cities can't keep the status, it goes to the first index of this ddl.

I tried to put this code in the If Not IdPostBack, but like this, it doesn't change datasource while it can keep status of the dropdownlist.

So does anyone have an idea about this problem?

I put the code here as reference:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ddlCities As DropDownList
Dim ddlCountries As DropDownList
ddlCities = CType(PN_Search.FindControl("DropDownList_Cities"), System.Web.UI.WebControls.DropDownList)
ddlCountries = CType(PN_Search.FindControl("DropDownList_Countries"), System.Web.UI.WebControls.DropDownList)
Dim countrySelect As String
countrySelect = ddlCountries.SelectedValue
Dim rechercheCitiesNull As String = "SELECT * FROM Cities WHERE id_city=1"
Dim rechercheCitiesNotNull As String = "SELECT * FROM [View_Country_City] Where id_country=" & countrySelect 

If countrySelect = "" Then
Me.RechercheCitiesDS.SelectCommand = rechercheCitiesNull
ddlCities.EnableViewState = True
ddlCountries.EnableViewState = True
ddlCities.DataBind()
ElseIf countrySelect <> "" Then
Me.RechercheCitiesDS.SelectCommand = rechercheCitiesNotNull
ddlCities.DataBind()

End If
End Sub

Thanks in advance!
Ziliu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不…忘初心 2024-12-01 01:45:40

EnableViewState 它应该可以解决您的问题。使用 not page.ispostback 保留您的绑定。

EnableViewState and it should fix your issue. Keep your binding withing a not page.ispostback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文