?
Solved

Dynamically adding controls to a DataList

Posted on 2007-08-12
8
Medium Priority
?
428 Views
Last Modified: 2008-02-01
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>
0
Comment
Question by:stankstank
  • 4
  • 4
8 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 19680761
1) Get a reference to the panel using FindControl:

   Panel pnlMain = (Panel)row.FindControl("pnlMain");

2) Add the control to the panel:

   TextBox txt = new TextBox();
   txt.ID = "textBox1";
   txt.Text = "Name";
   pnlMain.Controls.Add(txt);

Bob
0
 

Author Comment

by:stankstank
ID: 19680785
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19680795
Why don't you have a datasource?

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:stankstank
ID: 19680829
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...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19680842
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
0
 

Author Comment

by:stankstank
ID: 19681102
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19682631
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
0
 

Author Comment

by:stankstank
ID: 19714516
Thanks for the help Bob, it's working now.  :)

Br
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question