1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / listbox in createusercontrol - how to

listbox in createusercontrol - how to

2 posts, 1 answered
  1. gus
    gus avatar
    4 posts
    Registered:
    10 Aug 2009
    18 Aug 2009
    Link to this post
    How can I add a listbox, and populate with 3 items in the createusercontrolwizard?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    19 Aug 2009
    Link to this post
    Hello gus,

    You can add the control declaratively

            <asp:ListBox ID="lb1" runat="server" > 
            <asp:ListItem Text="test1"></asp:ListItem> 
            <asp:ListItem Text="test2"></asp:ListItem> 
            <asp:ListItem Text="test3"></asp:ListItem> 
            </asp:ListBox> 

    or bind the control to a datasource

      private string ConnectionString() 
        { 
            return System.Configuration.ConfigurationManager.ConnectionStrings["Sitefinity"].ConnectionString; 
        } 
     
        private void BindListBox() 
        { 
            DataTable table = new DataTable(); 
            SqlConnection connection = new SqlConnection(ConnectionString()); 
            try 
            { 
                connection.Open(); 
                string sqlStatement = "SELECT Top(5)* FROM telerik_Users"
                SqlCommand command = new SqlCommand(sqlStatement, connection); 
                SqlDataAdapter sqlData = new SqlDataAdapter(command); 
                sqlData.Fill(table); 
                if (table.Rows.Count > 0) 
                { 
                    lb1.DataSource = table; 
                    lb1.DataTextField = "UserName"
                    lb1.DataValueField = "UserId"
                    lb1.DataBind(); 
                } 
            } 
     
            catch (System.Data.SqlClient.SqlException ex) 
            { 
                string msg = "Error"
                msg += ex.Message; 
                throw new Exception(msg); 
            } 
     
            finally 
            { 
                connection.Close(); 
            } 
     
        } 


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
    Answered
2 posts, 1 answered