Second Try: Creating Dynamic Controls

Discussion in 'ASP .Net' started by Robert Bevington, Jul 16, 2007.

  1. Hi everyone,

    I've rewritten this one as I think I confused everyone last time :-(

    I'm trying to create dropdown lists that a user can select in order to
    filter the data in a gridview. My main problem is the number and names
    of the dropdown lists have to be created at runtime.

    Creating the dropdowns is easy via a simple VB loop round some ASP code.
    But how do I populate the dropdowns with the required data?

    When the loop starts the value of the dropdown's datatextfield is "". I
    then set it to the variable ATT_Name. Works fine if I only have one
    loop. However, on the second loop when I try to set the dropdown's
    datatextfield to the new value of ATT_Name, I get an error message that
    the datarowview does not contain the property "<value of ATT_Name>".

    Do I have to reset the property somehow?

    Here's my loop snippet:
    -- Start dynamic attribute field creation--%>
    <%
    For attCount = 1 To ATT_Columns.Count
    %>
    <tr>
    <%
    'Full name of attribute fields for SQL command
    ATT_Name = ATT_Columns(attCount)
    'Display name for label of drop-down list
    Me.Label1.Text = ATT_Name.Substring(4, (ATT_Name.Length - 4)) & ":"

    Me.cbo1.DataTextField = ATT_Name
    Me.cbo1.DataValueField = ATT_Name

    Me.SqlDataSource1.SelectCommand = "SELECT DISTINCT " & ATT_Name &
    " FROM " & Me.lblTableName.Text & " ORDER BY " & ATT_Name & ""
    %>
    <td>

    <asp:Label ID="Label1" runat="server" Text=""> </asp:Label>
    </td>
    <td>
    <asp:DropDownList ID="cbo1" runat="server"
    DataSourceID="SqlDataSource1" AppendDataBoundItems="True">
    </asp:DropDownList>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:Search4TextConnectionString %>">
    </asp:SqlDataSource>
    </td>
    </tr>
    <%
    Next
    %>

    Best regards,

    Robert
    Robert Bevington, Jul 16, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weston Fryatt
    Replies:
    2
    Views:
    947
    Weston Fryatt
    Jan 1, 2005
  2. John Saunders
    Replies:
    0
    Views:
    419
    John Saunders
    Jul 23, 2003
  3. Steven
    Replies:
    1
    Views:
    1,458
    Victor Garcia Aprea [MVP]
    Oct 23, 2004
  4. bienwell
    Replies:
    4
    Views:
    3,709
    bienwell
    May 27, 2005
  5. yelipolok
    Replies:
    4
    Views:
    239
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page