VB.NET—保持dropdownlist状态,而不将代码放入";而不是IsPostBack“;
我有两个下拉列表“国家”和“城市”,有两个数据源:VB.NET—保持dropdownlist状态,而不将代码放入";而不是IsPostBack“;,vb.net,drop-down-menu,postback,status,Vb.net,Drop Down Menu,Postback,Status,我有两个下拉列表“国家”和“城市”,有两个数据源: 如果用户选择ddl国家中的第一个索引,他可以在ddl城市中看到世界上所有的城市(使用数据源1) 如果用户选择一个国家,他可以看到与所选国家相对应的城市(使用数据源2)。 我使用vb.net将更改数据源的代码放在代码后面,在页面加载中,但在用户选择一个城市并单击提交按钮后,dropdownlist城市无法保持状态,它将转到此ddl的第一个索引 我试图将这段代码放在If Not IdPostBack中,但就像这样,它不会更改数据源,同时可以保持dr
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
Protected Sub Page_Load(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
将城市设置为下拉列表
将国家/地区设置为下拉列表
ddlCities=CType(PN_Search.FindControl(“DropDownList_Cities”),System.Web.UI.WebControls.DropDownList)
ddlcontries=CType(PN_Search.FindControl(“DropDownList_Countries”),System.Web.UI.WebControls.DropDownList)
选择为字符串
countrySelect=ddlCountries.SelectedValue
Dim rechercheCitiesNull As String=“选择*来自id为1的城市”
Dim rechercheCitiesNotNull As String=“从[View\u Country\u City]中选择*,其中id\u Country=“&countrySelect
如果countrySelect=“”,则
Me.RechercheCitiesDS.SelectCommand=rechercheCitiesNull
ddlCities.EnableViewState=True
ddlCountries.EnableViewState=True
ddlicities.DataBind()
否则,请选择“”然后
Me.RechercheCitiesDS.SelectCommand=rechercheCitiesNotNull
ddlicities.DataBind()
如果结束
端接头
提前谢谢!
Ziliu启用ViewState,它应该可以解决您的问题。使用not页面保留绑定。ispostback。您好,David,我已尝试将数据绑定设置为not ispostback,并使enableViewState等于True。但是当我把它放到ispostback中时,它不会改变数据源。表示无论我选择哪个国家,它总是使用数据源1。@Zillu您需要使用第一个下拉列表的SelectedExedChanged事件更改第二个下拉列表的数据源。请确保在下拉列表中指定autopostback=true。非常感谢您,David。我是按照你的建议做的:)@ziliu: