VB.NET-保持下拉列表状态而不将代码放入“Not IsPostBack”中
我有 2 个下拉列表“国家”和“城市”,有 2 个数据源:
如果用户选择 ddl 国家中的第一个索引,他可以看到 ddl 城市中世界上的所有城市(使用 datasource1)。< /p>
如果用户选择一个国家/地区,他可以看到与所选国家/地区相对应的城市(使用数据源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:
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).
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EnableViewState 它应该可以解决您的问题。使用 not page.ispostback 保留您的绑定。
EnableViewState and it should fix your issue. Keep your binding withing a not page.ispostback.