Databind Dropdown Asp.net


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Databind Dropdown Asp.net

Hybrid View

  1. #1
    Join Date
    Nov 2006
    Posts
    29

    Question Databind Dropdown Asp.net

    I have a user control that uses a lookup table to populate. When the user clicks the loopup icon for building, a list of items they can choose from appears. How could I populate the same control below based on what they choose. So once they select a building, I want them to only see those rooms associated with that building. I have a building table and a room table with an associated bldg id. Here's how the controls read:

    Code:
    <tr>
    <td bgcolor="gainsboro" style="width: 191px; height: 19px" valign="top">
    Building Location:</td>
    <td bgcolor="gainsboro" style="width: 415px; height: 26px" valign="top">
    <asp:TextBox ID="txtBldgLoc" runat="server" BackColor="LightYellow" CssClass="textbox" Text='<%# Bind("bldg_loc")%>' Width="222px"></asp:TextBox>* <asp:ImageButton ID="ibtnBldgLoc" runat="server" Height="20px" ImageUrl="~\images\zoom.gif" Width="20px" OnClick="ibtnLookup_Click" /><br />
    <asp:GridView ID="gridBldgLoc" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="dataBldgLoc" 
    Width="222px" OnRowCommand="gridLookup_RowComman* d" DataKeyNames="item" Visible="False"><Columns>* 
    <asp:ButtonField CommandName="select" DataTextField="item" SortExpression="item" />
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="dataBldgLoc" runat="server" ConnectionString="<%$ ConnectionStrings:WQ_INVENTORYConn* ectionString %>"
    SelectCommand="select item from vBldgs where lookup='bldg' order by item"></asp:SqlDataSource>* 
    </td>
    </tr>
    <tr>
    <td bgcolor="gainsboro" style="width: 191px; height: 19px" valign="top">
    Room Location:</td>
    <td bgcolor="gainsboro" style="width: 415px; height: 26px" valign="top">
    <asp:TextBox ID="txtRoomLoc" runat="server" BackColor="LightYellow" CssClass="textbox"
    Text='<%# Bind("room_loc")%>' Width="222px"></asp:TextBox>* <asp:ImageButton ID="ibtnRoomLoc" runat="server" Height="20px" ImageUrl="~\images\zoom.gif"
    Width="20px" OnClick="ibtnLookup_Click" /><br />
    <asp:GridView ID="gridRoomLoc" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" DataSourceID="dataRoomLoc" 
    Width="222px" OnRowCommand="gridLookup_RowComman* d" DataKeyNames="item" Visible="False">
    <Columns>
    <asp:ButtonField CommandName="select" DataTextField="item" SortExpression="item" />
    </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="dataRoomLoc" runat="server" ConnectionString="<%$ ConnectionStrings:WQ_INVENTORYConn* ectionString %>"
    SelectCommand="select item from vRooms where lookup='room' order by item"></asp:SqlDataSource>* 
    </td>
    </tr>

  2. #2
    Join Date
    Mar 2005
    Location
    Los Angeles, Calif. AKA: Gangsta Yoda™
    Posts
    455
    Thread Moved
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer code questions via PMs.
    Microsoft MVP 2006-2009
    Office Development FAQ (VBA, VB6, VB.NET, C#)
    Software Engineer MCP (VB6 & .NET)
    Reps & Rating PostsVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6Outlook Global Address List

  3. #3
    Join Date
    Jan 2007
    Posts
    41
    Well,
    to be honest I don't want to figure all of that code out...but i think i get the gist of what you are trying to do.

    So, first things first is that you want to set the "Value" property of each item to the propertyID (or whatever the ID was). When the dropdownlist selectedindex is changed, make sure to point the event to a subroutine, like so:

    Code:
    <script runat="server">
      Sub fill_table(sender as object, e as eventargs)
         'Fill code will go here
      End Sub
    </script>
    <asp:dropdownlist runat="Server" id="dl_propertyList" OnSelectedIndexChanged="fill_table"  Autopostback="True" />
    It is important that if you want the form to fill on selected index changed, which I'm assuming you do...set autopostback="true".

    Next you can construct the data.datatable that you will bind to your data display table in the client end. So here goes:
    Code:
    <Script runat="server">
      Sub fill_table(sender as object, e as eventargs)
         'Do whatever you do to connect to yer DB but use a querystring similar to this
        'Select only the columns that I want from both tables and join the two based on matching IDs
        Dim sqlCommand as string = "Select Table1.Column1,Table1.Column2,Table2.Column1 FROM Table1 INNER JOIN Table2 ON Table1.ID=Table2.ID WHERE Table1.ID='" & sender.SelectedItem.Value & "'"
    
        'Fill the datatable & databind it to your control
      End Sub
    
    </script>

  4. #4
    Join Date
    Nov 2006
    Posts
    29

    Question

    I sort of get what you're saying, however still confused. I've inherited this application and have minimal asp.net knowledge (learning as I go). Basically this app is using txt boxes with gridviews to lookup tables. So my user controls from the codes in this post refer to case statements in code behind.

    Code:
    Protected Sub ibtnLookup_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
            'Routine: displays gridview based on button clicked
            '           sets focus to gridview
            Dim strGrid As String = ""
            Select Case CType(sender, ImageButton).ID.ToString
                Case "ibtnType"
                    strGrid = "gridType"
                Case "ibtnMfg"
                    strGrid = "gridMfg"
                Case "ibtnModel"
                    strGrid = "gridModel"
                Case "ibtnMother"
                    strGrid = "gridMother"
                Case "ibtnVideo"
                    strGrid = "gridVideo"
                Case "ibtnOS"
                    strGrid = "gridOS"
                Case "ibtnProcessor"
                    strGrid = "gridProcessor"
                Case "ibtnSpeed"
                    strGrid = "gridSpeed"
                Case "ibtnHD1"
                    strGrid = "gridHD1"
                Case "ibtnHD2"
                    strGrid = "gridHD2"
                Case "ibtnMemory"
                    strGrid = "gridMemory"
                Case "ibtnOD1"
                    strGrid = "gridOD1"
                Case "ibtnOD2"
                    strGrid = "gridOD2"
                Case "ibtnStatus"
                    strGrid = "gridStatus"
                Case "ibtnOwner"
                    strGrid = "gridOwner"
                Case "ibtnBldgLoc"
                    strGrid = "gridBldgLoc"
                Case "ibtnRoomLoc"
                    strGrid = "gridRoomLoc"
            End Select
            CType(formPC.FindControl(strGrid), GridView).Visible = True
            CType(formPC.FindControl(strGrid), GridView).Focus()
        End Sub
        Protected Sub gridLookup_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
            'Routine: if a dataview item is selected for a lookup,
            '           calls Lookup to process selected item
            If e.CommandName = "select" Then
                Select Case CType(e.CommandSource, GridView).ID
                    Case "gridType"
                        Lookup(e, "gridType", "txtType", "txtSerial")
                    Case "gridMfg"
                        Lookup(e, "gridMfg", "txtMfg", "txtModel")
                    Case "gridModel"
                        Lookup(e, "gridModel", "txtModel", "txtMother")
                    Case "gridMother"
                        Lookup(e, "gridMother", "txtMother", "txtVideo")
                    Case "gridVideo"
                        Lookup(e, "gridVideo", "txtVideo", "txtOS")
                    Case "gridOS"
                        Lookup(e, "gridOS", "txtOS", "txtProcessor")
                    Case "gridProcessor"
                        Lookup(e, "gridProcessor", "txtProcessor", "txtSpeed")
                    Case "gridSpeed"
                        Lookup(e, "gridSpeed", "txtSpeed", "txtHardDrive1")
                    Case "gridHD1"
                        Lookup(e, "gridHD1", "txtHardDrive1", "txtHardDrive2")
                    Case "gridHD2"
                        Lookup(e, "gridHD2", "txtHardDrive2", "txtMemory")
                    Case "gridMemory"
                        Lookup(e, "gridMemory", "txtMemory", "txtOD1")
                    Case "gridOD1"
                        Lookup(e, "gridOD1", "txtOD1", "txtOD2")
                    Case "gridOD2"
                        Lookup(e, "gridOD2", "txtOD2", "txtEMR")
                    Case "gridStatus"
                        Lookup(e, "gridStatus", "txtStatus", "txtStatusDate")
                    Case "gridOwner"
                        Lookup(e, "gridOwner", "txtOwner", "txtBldgLoc")
                    Case "gridBldgLoc"
                        Lookup(e, "gridBldgLoc", "txtBldgLoc", "txtRoomLoc")
                    Case "gridRoomLoc"
                        Lookup(e, "gridRoomLoc", "txtRoomLoc", "txtComments")
                End Select
            End If
        End Sub
    It doesn't appear that it is using any tablefills or table adapters so I'm trying to take ideas from online and figure out how I can accomplish getting one gridview to populate based on the selection from another gridview.

Similar Threads

  1. asp.net 1.1 button dropdown menu
    By munchy_cool in forum ASP.NET
    Replies: 0
    Last Post: 02-14-2007, 04:19 AM
  2. Replies: 2
    Last Post: 12-30-2006, 08:12 AM
  3. dropdown calendar control in asp.net
    By flitt2001 in forum ASP.NET
    Replies: 1
    Last Post: 04-21-2006, 03:55 PM
  4. Replies: 1
    Last Post: 08-18-2005, 02:19 PM
  5. Free ASP.NET Web Matrix Design/Editor Tool Released
    By ASPSmith Training in forum dotnet.announcements
    Replies: 0
    Last Post: 06-18-2002, 04:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center