?
Solved

Change Datasource for DropDownBox at runtime ASP.NET/Vb

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

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 article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 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