Solved

Change Datasource for DropDownBox at runtime ASP.NET/Vb

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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