Link to home
Start Free TrialLog in
Avatar of stankstank
stankstank

asked on

Dynamically adding controls to a DataList

Hey experts,

I am trying to add controls to my datalist at runtime, but I can't get it to work.  I don't know the name of the control UNTIL the program is run by the user...  Below is what I am trying to do, all I need to figure out is how to add the control to the datalist control.  I added two panel controls to the datalist's <ItemTemplate> section so I could do something like dlMyList.FindControl("PanelName").AddControl = *mycontrolhere*


Pseudocode of what I am trying to do:
-------------------------------------------------------------------------------------------------------------
for(each row in datatable)
{
  if(the row == "something here")
  {
    create a control called row[i]
    add a new record in the datalist so we can add the control
    add the control to the datalist control
     
  }
}

Here is the datalist which was added at design time:
--------------------------------------------------------------------------------------------------------------
<asp:DataList ID="dlRoles" runat="server" RepeatColumns="6" RepeatDirection="Horizontal" Width="100%">
  <ItemTemplate>
     <asp:Panel ID="pnlMain" runat="server" />
     <asp:Panel ID="pnlChild" runat="server" />
  </ItemTemplate>
</asp:DataList>
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stankstank
stankstank

ASKER

Hey bob, thanks for the quick reply.  I am unable to run the code above because I don't have a datasource for my datalist control.  I am terribly sorry - I forgot to mention this in the question.  I guess I also need some way in my foreach loop to say MyDataList.AddNewRecord or something.  Any ideas on that?

Again, thanks for the answer above - sorry I didn't put everything in the question.

Stank
Why don't you have a datasource?

Bob
Well, I have a datatable which contains about 30 records from the database.  Depending on which user gets to the page they may or may not see all of them.  I have to loop through the datatable and only make controls for the rows which match a certain criteria.  Is this proper?  It seems as if I am not doing something right...
1) You have a DataTable

2) You only want to display certain rows

3) I would bind the DataList to a DataView constructed from the DataTable, with a RowFilter set to the criteria for which rows to display.

Bob
Well, I am not really sure what to do to be honest... Should I just set the DataSource property of the datalist to my dataview which has filtered out all of the fields which I don't need?  If I do that, how do I get the controls created for each record in the dataview, then put them in the datalist?  

You are leading me into good thinking, but I just don't know where to go with it yet...  I think I get the general idea, though.
1) Are you have problems with both showing particular columns and rows?

2) The approach of using a DataView with the RowFilter property set is for filtering rows.

3) The process of creating controls for the DataList would be the same for either the DataTable or the DataView as the data source.

Bob
Thanks for the help Bob, it's working now.  :)

Br