Results 1 to 9 of 9

Thread: How do I populate a combobox and then select a certain item on it

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    How do I populate a combobox and then select a certain item on it

    Hi guys,

    how can i npopulate a combobox and then autoselect an item that corresponds to its primary key. say i populate a combo with a list of occupations from my occupation table. and then for a specific person who has an occupationid as a foreign key in the people table I want to select the related combo value

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How do I populate a combobox and then select a certain item on it

    How are you retrieving the data? The dropdownlist control can be bound, just like you'd bind a gridview or repeater; when binding the DDL, specify the DataTextField and DataValueField.

    To select a certain item, if you have the user's "pre-chosen" value, then you can find it using the FindByText or FindByValue methods and then selecting Selected to true.

  3. #3

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: How do I populate a combobox and then select a certain item on it

    hi mendhak,

    thanks for the reply. sorry but i forgot to mention, the dropdownlist is in a datarepeater. Please help me. I am retrieving data from a mysql database

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How do I populate a combobox and then select a certain item on it

    Hey,

    I think it might help if you were to show some code.

    What are you currently doing?

    Gary

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How do I populate a combobox and then select a certain item on it

    Yes, it would help to mention that. The same rules apply, but do an e.Item.FindControl to get the dropdownlist first, cast it to type DropDownList and then bind it to your 'source', then use FindByText or FindByValue.

  6. #6

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: How do I populate a combobox and then select a certain item on it

    Hi gep13,

    I have this code:

    Code:
    <asp:Repeater ID="GuardianRepeater"  
                runat="server">
                <HeaderTemplate>
                <table>
                </HeaderTemplate> 
                <ItemTemplate>
                    <asp:TextBox ID="Gid" CssClass="hidden" runat="server" Text='<%#Container.DataItem("GuardianID")%>'></asp:TextBox>
                    <tr>
                    <td>Surname:</td><td><asp:TextBox ID="Surname" CssClass="surnametextbox" runat="server" Text='<%#Container.DataItem("Surname")%>' ></asp:TextBox></td><td></td>
                    <td>First Names:</td><td><asp:TextBox ID="Firstnames" CssClass="surnametextbox" runat="server" Text='<%#Container.DataItem("firstnames")%>' ></asp:TextBox></td> <td></td>
                    </tr>
                    
                    <tr>
                    <td>ID Number:</td><td><asp:TextBox ID="idnumber" runat="server" CssClass="surnametextbox"  Text='<%#Container.DataItem("IDNumber")%>' ></asp:TextBox></td> <td></td>
                    <td>Cell:</td><td><asp:TextBox ID="cellphone" runat="server" CssClass="surnametextbox"  Text='<%#Container.DataItem("cell")%>' ></asp:TextBox></td> <td></td>
                    </tr>
                    
                    <tr>
                    <td>Tel(Work):</td><td><asp:TextBox ID="telwork" CssClass="surnametextbox" runat="server" Text='<%#Container.DataItem("Tel_Work")%>' ></asp:TextBox></td><td></td>
                    <td>Tel(Home):</td><td><asp:TextBox ID="telhome" CssClass="surnametextbox" runat="server" Text='<%#Container.DataItem("Tel_Home")%>' ></asp:TextBox></td> <td></td>
                    </tr>
                    
                    <tr>
                    <td>Fax(Work):</td><td><asp:TextBox ID="faxwork" CssClass="surnametextbox" runat="server" Text='<%#Container.DataItem("Fax_Work")%>' ></asp:TextBox></td><td></td>
                    <td>Fax(Home):</td><td><asp:TextBox ID="faxhome" CssClass="surnametextbox" runat="server" Text='<%#Container.DataItem("Fax_Home")%>' ></asp:TextBox></td> <td></td>
                    </tr>
                     
                    <tr>
                    <td>Occupation:</td><td></td>
                    </tr>
                    
                    <tr>
                    <td>Work E-Mail:</td><td><asp:TextBox ID="email" runat="server" CssClass="emailtextbox" Text='<%#Container.DataItem("email")%>' ></asp:TextBox></td><td><asp:CheckBox ID="chkCanEmail" Checked='<%#Container.DataItem("CanEmail")%>' runat="server" />Receive E-mail Correspondence on this Address</td> 
                    </tr>
                    
                    <td>Home E-Mail:</td><td><asp:TextBox ID="HomeEmail" runat="server" CssClass="emailtextbox" Text='<%#Container.DataItem("HomeEmail")%>'></asp:TextBox></td><td><asp:CheckBox ID="chkCanEmailHome" Checked='<%#Container.DataItem("CanEmailHome")%>' runat="server" />Receive E-mail Correspondence on this Address</td><tr>
                    
                    </tr>
                    
                   
                     <cc1:MaskedEditExtender ID="MaskedEditExtender1" TargetControlID="telwork" Mask="(999)9999999" MessageValidatorTip="true" MaskType="Number"   ErrorTooltipEnabled="true" runat="server"  ></cc1:MaskedEditExtender> 
                     <cc1:MaskedEditExtender ID="MaskedEditExtender2" TargetControlID="telhome" Mask="(999)9999999" MessageValidatorTip="true" MaskType="Number" ErrorTooltipEnabled="true" runat="server"></cc1:MaskedEditExtender>         
                    
                    <cc1:MaskedEditValidator ID="MaskedEditValidator1" ControlExtender="MaskedEditExtender1" ControlToValidate="telwork" TooltipMessage="Enter a Number in this format: (999)9999999"   runat="server"></cc1:MaskedEditValidator>
                   <%-- <asp:RequiredFieldValidator ID="RfEmail" ControlToValidate="email" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>--%>
                    <asp:RegularExpressionValidator ID="RevEmail" ControlToValidate="email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" runat="server" ErrorMessage="*Email must be in the format of [email protected]" Display="Dynamic" ></asp:RegularExpressionValidator>
                </ItemTemplate> 
                <SeparatorTemplate>
                <tr>
                <td colspan="6" >
                <hr />
                </td>
                </tr>
                </SeparatorTemplate> 
                
                <FooterTemplate>
                </table> 
                </FooterTemplate> 
            </asp:Repeater>
    as you can see all the fields are bound. I populate a dataadapter in my codebehind.

    I need to add a dropdownlist inside the datarepeater and bind it as well. please help

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How do I populate a combobox and then select a certain item on it

    Did you do what I suggested?

  8. #8

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: How do I populate a combobox and then select a certain item on it

    thanks mendhak,

    will try on tuesday. Im on leave from today

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How do I populate a combobox and then select a certain item on it

    Happy Holidays!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width