Solved

asp.net c# placeholder

Posted on 2010-11-22
13
725 Views
Last Modified: 2012-05-10
Q1: I have a list of placeholder name (.ascx) saved in database. like
hello1.ascx
hello2.ascx
hello3.ascx

I plan to use query to get list of placeholder into frontend placeholder object in aspx.
I have no problem to query them, but i don't know how to parse to the frontend placeholder.

Can you show me how? I would like to see in a function call because I will put it into App_Code folder.

Thanks,


0
Comment
Question by:Webboy2008
13 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 34190485
please explain in detail. can't make out anything out of your question
0
 

Author Comment

by:Webboy2008
ID: 34190515
Table Name TblPlaceHolder

Column Name: PlaceHolder
hello1.ascx
hello2.ascx
hello3.ascx

Query Select PlaceHolder from TblPlaceHolder

Using Ado to get resultset from the query
Put all of the ascx to a single placeholder.
shown on the aspx

All languages using c#/asp.net

Hope this helps.


0
 
LVL 1

Expert Comment

by:MLSmudde
ID: 34193322
I am assuming the user controls are files within your website.   To add a user control to an existing placeholder you add the control to the placeholder's contols collection.

UserControl myControl = Page.LoadControl("hello1.ascx");
TblPlaceHolder.Controls.Add(myControl);

To avoid viewstate issues,  you should add the controls in the page's OnInit method.
0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34194305
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 34195204
Hi,

I think you are after something like this:
    protected void Page_Load(object sender, EventArgs e)
    {
        //Get all the placeholder names from the database
        SqlConnection dbConn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
        SqlDataAdapter da = new SqlDataAdapter("SELECT PlaceHolder FROM tblPlaceHolder", dbConn);
        DataTable dtPlaceHolders = new DataTable();
        da.Fill(dtPlaceHolders);
        //Find the master placeholder in the page
        PlaceHolder phMain = (PlaceHolder)Page.FindControl("phMain");
        //Iterate over the database results to instantiate and load the user controls into the main placeholder
        foreach(DataRow row in dtPlaceHolders.Rows) {
            UserControl dbPlaceHolder = (UserControl)Page.LoadControl("~/" + row[0].ToString());
            phMain.Controls.Add(dbPlaceHolder);
        }
    }

Open in new window


Above example assumes you have a placeholder in your page with ID="phMain" and the three user controls in the root of your app.

/Carl.
0
 

Author Comment

by:Webboy2008
ID: 34202019
error pointing to this code: phMain.Controls.Add(dbPlaceHolder);
Error Message: System.NullReferenceException: Object reference not set to an instance of an object.
When I use response.write to see the string.
It shows correct directory path.



using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Web.Configuration;



public partial class Default2 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        SqlConnection DBSuretyCon = new SqlConnection(WebConfigurationManager.ConnectionStrings["SuretyDb"].ToString());

        SqlDataAdapter da = new SqlDataAdapter("SELECT AscxName FROM AdditionalInfoUI WHERE ClassCodeId =7 AND StatusId = 1",DBSuretyCon);



        DataTable dtPlaceHolders = new DataTable();

        da.Fill(dtPlaceHolders);

        //Find the master placeholder in the page

        PlaceHolder phMain = (PlaceHolder)Page.FindControl("phMain");

         //Iterate over the database results to instantiate and load the user controls into the main placeholder

        foreach (DataRow row in dtPlaceHolders.Rows)

        {

            UserControl dbPlaceHolder = (UserControl)Page.LoadControl("~/" + row[0].ToString());

            Response.Write("~/" + row[0].ToString() + "<br>");

            phMain.Controls.Add(dbPlaceHolder);



            

          }

        

    }   

}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:carlnorrbom
ID: 34202928
Hi,

And you are sure you have the specific user control on that path? To me it sounds as if it can't find the usercontrol in question on the given path. The code works just fine for me on my end.

/Carl.
0
 

Author Comment

by:Webboy2008
ID: 34207703
it is a correct path
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 34208228
Hi,

For Your reference and a proof of concept and validation of provided code, please see:

 carlnorrbom-373377.flv
0
 

Author Comment

by:Webboy2008
ID: 34208355
When I comment the following line of code. it is working.
PlaceHolder phMain = (PlaceHolder)Page.FindControl("phMain");
0
 

Author Comment

by:Webboy2008
ID: 34208360
And I put PlaceHolder1.Controls.Add(dbPlaceHolder) instead in the ForEach Loop
0
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 34208492
Hi,

Well naturally you need to replace the phMain ID with whatever is relevant in your solution, this is just a working sample.

/Carl.
0
 

Author Closing Comment

by:Webboy2008
ID: 34208506
It is working great. Thank you for your helps and efforts.

I have another post for 500pts if you want to try.

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26637414.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now