Add radio button dynamically at run-time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • castron
    New Member
    • Mar 2008
    • 5

    Add radio button dynamically at run-time

    Hi all,

    I'm reading user information from Active Directory. I'm allowing the users to search by username from a web form. There will be times when the result will be more than one record. What I'm trying to do is to generate radio buttons at run-time, so users can select the user account they are looking for, and then populate the information into a form.

    My question is how do I create a single event handler for more than one radio button, so I can write the code to populate the information in the form controls.

    here is the portion of the code where I want to create the radio buttons:
    Any help is greatly appreciated

    foreach (SearchResult result in search.FindAll( ))
    {
    if (result.Propert ies["samAccountName "].Count > 0 && result.Properti es["mail"].Count > 0 && result.Properti es["givenname"].Count > 0 && result.Properti es["sn"].Count > 0)
    {
    Response.Write( "<tr><td>" + result.Properti es["samAccountName "][0].ToString() + "</td><td>" + result.Properti es["mail"][0].ToString() + "</td><td>" + result.Properti es["givenname"][0].ToString() + "</td><td>" + result.Properti es["sn"][0].ToString() + "</td></tr>");
    }
    }
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    What you need to use is the RadioButtonList . That way, you tie the event handler to the list, and adding items is simple.

    Code:
    <!-- On the .aspx page -->
    <asp:RadioButtonList ID="rbl1" runat="server">
    	<asp:ListItem Text="One" Value="1"></asp:ListItem>
    </asp:RadioButtonList>
    <asp:Button ID="b1" runat="server" Text="Add Radio Button" OnClick="cmd" />
    Code:
    //on the .aspx.cs page
    protected void cmd(object sender, EventArgs e)
    {
    	rbl1.Items.Add(new ListItem("another", "2"));
    }
    So every time you click the button, you add a RadioButton to the RadioButtonList .

    Comment

    Working...