Solved

Change Datasource for DropDownBox at runtime ASP.NET/Vb

Posted on 2004-08-26
3
741 Views
Last Modified: 2012-06-21
I have a dropdown box and three radio buttons on my webform- RadioButton A, B and C.  When radiobutton A or B is selected, then the dropdown box uses one datasource (with a default selection, so the empty string "" is not an option).  When radiobutton C is selected, I want to clear the values from the drop down box and disable it- so nothing can be selected.  

Current behavior:  The dropdown box is enabled/disabled properly, depending on which radiobutton is pressed.  When it is disabled, however, you can still view whatever choice was selected when it was enabled.  I want the dropdown box to be CLEARED and disabled... and can't figure out how to do it.

Current Technique (is there an easier way??): I am trying to accomplish this by changing the datasource of the dropdown box... but this does not appear to be workinig.  Each click of the radiobutton causes a postback to the page, so I check to see which radio button is checked and try and update the datasource of the dropdown box accordingly.  As I said before, the box itself is enabled/disabled correctly.... I just always see the previously selected text in the disabled box.  

Code below, Thanks much, Jessica

HTML Snippet-

<asp:tablerow>
   <asp:tablecell horizontalalign="right" verticalalign="Bottom" cssclass="label">Access Level</asp:tablecell>
   <asp:TableCell Width="10"></asp:TableCell>
   <asp:tablecell>
      <asp:DropDownList id="ddlAccess" Runat="server" CssClass="text_box"></asp:DropDownList>
   </asp:tablecell>
</asp:tablerow>
<asp:tablerow>
   <asp:tablecell horizontalalign="right" verticalalign="bottom" cssclass="label">Type</asp:tablecell>
   <asp:TableCell Width="10"></asp:TableCell>
   <asp:tablecell>
      <asp:RadioButton GroupName="type" Runat="server" Text="Advisor Company" ID= "rbAdvComp"></asp:RadioButton>
      &nbsp;&nbsp;
      <asp:RadioButton ID="rbRIAFirm" GroupName="type" Runat="server" Text="RIA Firm"></asp:RadioButton>
      &nbsp;&nbsp;
              <asp:RadioButton GroupName="type" Runat="server" Text="Both" ID="rbBoth"></asp:RadioButton>
   </asp:tablecell>
</asp:tablerow>


Codebehind (VB)

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        On Error GoTo handleerr
        Dim errhandle As New clsError
        Dim db As New clsRIADB
        Dim intcount As Integer

        Dim x As FirmType
        ria_id = Request.QueryString("id")
'function that retrieves all the company information based on the id from the db
        x = db.getRIAFirm(ria_id)
   
         If Not IsPostBack Then
            'whoooole bunch of processing that I omitted here for clarity

                'Initial set of the radiobuttons, based on what is stored in the db
                If x.Type = 1 Then
                    rbRIAFirm.Checked = True
                ElseIf x.Type = 2 Then
                    rbAdvComp.Checked = True
                Else
                    rbBoth.Checked = True
                End If

        End If

'Outside the postback I check for which radiobuttons are checked and try and set the datasource of the dropdownlist accordingly.
        If rbRIAFirm.Checked Or rbBoth.Checked Then
            ddlAccess.Enabled = True
           
'function below returns dataview from database of dropdownlist options
            Dim z As DVErrType
            z = db.getAccessLevel
            ddlAccess.DataSource = z.DV
            ddlAccess.DataTextField = "access_level_desc"
            ddlAccess.DataValueField = "access_level"
            ddlAccess.DataBind()
            For intcount = 0 To (ddlAccess.Items.Count - 1)
                If UCase(ddlAccess.Items(intcount).Text) = UCase(x.AccessDesc) Then
                    ddlAccess.Items(intcount).Selected = True
                End If

            Next
        Else

'THIS IS THE PART THAT DOESN'T WORK
            ddlAccess.SelectedValue = Nothing
            ddlAccess.DataSource = Nothing
            Dim dt2 As DataTable = New DataTable
            Dim dr2 As DataRow = dt2.NewRow()
            dt2.Columns.Add(New DataColumn("access_level", GetType(String)))
            dt2.Columns.Add(New DataColumn("access_level_desc", GetType(String)))
            dr2(0) = ""
            dr2(1) = ""
            dt2.Rows.Add(dr2)

            Dim dv2 As DataView = New DataView(dt2)
            ddlAccess.DataSource = dv2
            ddlAccess.Enabled = False
        End If

        errhandle = Nothing
        db = Nothing
 End Sub
0
Comment
Question by:jmamer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 11910542
jmamer ,
>>I want the dropdown box to be CLEARED and disabled... and can't figure out how to do it.
This part :
------------
ddlAccess.SelectedValue = Nothing
ddlAccess.DataSource = Nothing

Change to :
ddlAccess.Items.Clear() 'clear items
0
 
LVL 10

Expert Comment

by:EBatista
ID: 11911895
just call DataBind after setting up the DataSource to nothing:
...
ddlAccess.DataSource = Nothing
ddlAccess.DataBind()
...

regards
0
 
LVL 1

Expert Comment

by:janeburton
ID: 11913488
Dim dv2 As DataView = New DataView(dt2)
ddlAccess.DataSource = dv2
ddlAccess.Enabled = False
ddlAccess.DataTextField="something"
ddlAccess.DataValueField="somthing"
ddlAccess.DataBind()
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question