Solved

Change Datasource for DropDownBox at runtime ASP.NET/Vb

Posted on 2004-08-26
3
713 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
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now