AS Web Forms : Design Patterns : Limit a dropdown on the Web Forms based on a value in another dropdown.
Sometimes, you may have a set of drop downs on a single AS Web Form that rely on one another. For an example, consider State and County.
The following pattern may be used on the page hosting the ASWebForms ASDataEditor control:
In your code behind, add an object to server as a reference to your data editor:
Protected WithEvents ctlASDataEditor As ASWebForms.AlgonquinStudios.Controls.ASDataEditor
Then, set a reference to this in the Page_Load event:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadctlASDataEditor = Me.FindControl("ctlASDataEditor")End Sub
Finally, add code to the ctlASDataEditor_PreRender event which will perform the appropriate look-up changes and reset the drop down:
Private Sub ctlASDataEditor_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctlASDataEditor.PreRenderDim cboState As System.Web.UI.HtmlControls.HtmlSelect
Dim htParameters As Hashtable
Dim intStateIdent As Int32
Dim dsResults As System.Data.DataSet
Dim dv As System.Data.DataView
Dim intCountyIdent As Int32cboState = Helper.GetControl(ctlASDataEditor, "StateIdent")
cboState.Attributes.Add("onchange", "forms[0].submit();")intStateIdent = CType(Helper.GetControl(ctlASDataEditor.pnlDataControls, ("State")), System.Web.UI.HtmlControls.HtmlSelect).Value
intCountyIdent = CType(Helper.GetControl(ctlASDataEditor.pnlDataControls, ("CountyIdent")), System.Web.UI.HtmlControls.HtmlSelect).ValueWith CType(Helper.GetControl(ctlASDataEditor.pnlDataControls, ("CountyIdent")), System.Web.UI.HtmlControls.HtmlSelect)htParameters = New Hashtable
htParameters.Add("@intStateIdent", intStateIdent)If Me.Helper.CallStoredProcedure("uspGetActiveCountyByStateIdent", htParameters, 0, 0, dsResults, dsMessage) Then.DataSource = dsResults.Tables(0).DefaultViewEnd If.DataBind()
.Items.FindByValue(intCountyIdent.ToString).Selected = TrueEnd WithEnd Sub