Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Change Datasource for DropDownBox at runtime ASP.NET/Vb

Posted on 2004-08-26
3
Medium Priority
?
755 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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