468,161 Members | 1,952 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

Determining whether a DropDownList has changed...

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!
Jul 21 '05 #1
2 3069
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
-----Original Message-----
I have a problem that I figured would be relatively common, but havebeen unable to find any information on it as of yet. I have a pagethat contains two DropDownList controls, with the second beingdependent on the value of the first. My DropDownList controldefinitions 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 onthe selection of the first. My code-behind looks like this:
....
Private Sub Page_Load(ByVal sender As System.Object, ByVal e AsSystem.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 boundon the initial load of the page. However, when a value is changed inddlLocCty, the ddlLocOfce remains unchanged due to the fact that it'sinside the "If Not Page.IsPostBack" block. If I move the bind of theddlLocOfce outside of this block, it works as it should. However, Irun into another problem at this point. There are several ValidationControls on this page, and when the page is posted back by anothermethod besides the change event on ddlLocCty, the ddlLocOfce isrebound, causing whatever selection the user made in that to be lost.
What I need to do is move the bind of ddlLocOfce outside of thepost-back check, and determine HOW the page is being posted back. Ineed to know that the page is being posted back by virtue of theautopostback property on ddlLocCty.

Now, I tell you all that (TMI, I know...) to ask this question: Isthere an easy way to tell if the value in ddlLocCty has changed? Ican 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. Isthere a property for a DropDownList that checks to see if it's beenchanged? Or is there a better way to go about doing this wholeprocess?

Any help would be greatly appreciated.

Thanks for your time!
.

Jul 21 '05 #2
Chris,

I tried that it worked like a champ...

Thanks so much!

Brennon
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jason | last post: by
2 posts views Thread by Brennon Arnold | last post: by
2 posts views Thread by Mike Collins | last post: by
11 posts views Thread by Santosh | last post: by
11 posts views Thread by harold.gimenez | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.