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("Pane lName").Ad dControl = *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="Horizonta l" Width="100%">
<ItemTemplate>
<asp:Panel ID="pnlMain" runat="server" />
<asp:Panel ID="pnlChild" runat="server" />
</ItemTemplate>
</asp:DataList>
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("Pane
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="Horizonta
<ItemTemplate>
<asp:Panel ID="pnlMain" runat="server" />
<asp:Panel ID="pnlChild" runat="server" />
</ItemTemplate>
</asp:DataList>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why don't you have a datasource?
Bob
Bob
ASKER
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
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
ASKER
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.
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
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
ASKER
Thanks for the help Bob, it's working now. :)
Br
Br
ASKER
Again, thanks for the answer above - sorry I didn't put everything in the question.
Stank