Determining whether a DropDownList has changed...

  • Thread starter Thread starter Brennon Arnold
  • Start date Start date
B

Brennon Arnold

I have a problem that I figured would be relatively common, but have
been unable to find any information on it as of yet. I have a page
that contains two DropDownList controls, with the second being
dependent on the value of the first. My DropDownList control
definitions look like this:

<asp:dropdownlist id="ddlLocCty" runat="server" CssClass="SmlBox"
AutoPostback="True" CausesValidation="False"></asp:dropdownlist>

<asp:dropdownlist id="ddlLocOfce" runat="server"
CssClass="SmlBox"></asp:dropdownlist>

When the first list is changed, the second list is rebuilt based on
the selection of the first. My code-behind looks like this:

....
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
bind_ddlLocCty()
bind_ddlLocOfce()
bind_ddlReqType()
bind_ddlReqStat()
End If
End Sub

....

Private Sub bind_ddlLocCty()
ddlLocCty.DataSource = BindCty()
ddlLocCty.DataValueField = "cty_cd"
ddlLocCty.DataTextField = "cty_nme_txt"
ddlLocCty.DataBind()
ddlLocCty.Items.Insert(0, New ListItem(" ", ""))
End Sub

Private Sub bind_ddlLocOfce()
ddlLocOfce.DataSource = BindOfce()
ddlLocOfce.DataValueField = "ofce_nbr"
ddlLocOfce.DataTextField = "ofce_txt"
ddlLocOfce.DataBind()
ddlLocOfce.Items.Insert(0, New ListItem(" ", ""))
End Sub

....

Function BindCty() As OleDbDataReader
'Builds SQL for bind
End Function

....

Function BindOfce() As OleDbDataReader
'Builds SQL for bind based on value of ddlLocCty
End Function

....

Everything works as it should to a degree. The DDL's are only bound
on the initial load of the page. However, when a value is changed in
ddlLocCty, the ddlLocOfce remains unchanged due to the fact that it's
inside the "If Not Page.IsPostBack" block. If I move the bind of the
ddlLocOfce outside of this block, it works as it should. However, I
run into another problem at this point. There are several Validation
Controls on this page, and when the page is posted back by another
method besides the change event on ddlLocCty, the ddlLocOfce is
rebound, causing whatever selection the user made in that to be lost.

What I need to do is move the bind of ddlLocOfce outside of the
post-back check, and determine HOW the page is being posted back. I
need to know that the page is being posted back by virtue of the
autopostback property on ddlLocCty.

Now, I tell you all that (TMI, I know...) to ask this question: Is
there an easy way to tell if the value in ddlLocCty has changed? I
can store the original value in a Viewstate variable and compare it,
but I feel like I'm regressing back to Classic ASP at that point. Is
there a property for a DropDownList that checks to see if it's been
changed? Or is there a better way to go about doing this whole
process?

Any help would be greatly appreciated.

Thanks for your time!
 
Hello Brennon,

The SelectedIndexChanged event of a DropDownList
control will fire whenever the selection is changed. You
can write a handler to respond to a change of the value in
ddlLocCty, and only rebind ddlLocOfce under those
conditions. This should help solve your problem.

Chris
 
Back
Top