Webboy2008
asked on
asp.net c# placeholder
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,
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,
please explain in detail. can't make out anything out of your question
ASKER
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.
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.
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.a scx");
TblPlaceHolder.Controls.Ad d(myContro l);
To avoid viewstate issues, you should add the controls in the page's OnInit method.
UserControl myControl = Page.LoadControl("hello1.a
TblPlaceHolder.Controls.Ad
To avoid viewstate issues, you should add the controls in the page's OnInit method.
Hi,
I think you are after something like this:
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.
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);
}
}
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.
ASKER
error pointing to this code: phMain.Controls.Add(dbPlac eHolder);
Error Message: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
When I use response.write to see the string.
It shows correct directory path.
Error Message: System.NullReferenceExcept
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);
}
}
}
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.
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.
ASKER
it is a correct path
Hi,
For Your reference and a proof of concept and validation of provided code, please see:
carlnorrbom-373377.flv
For Your reference and a proof of concept and validation of provided code, please see:
carlnorrbom-373377.flv
ASKER
When I comment the following line of code. it is working.
PlaceHolder phMain = (PlaceHolder)Page.FindCont rol("phMai n");
PlaceHolder phMain = (PlaceHolder)Page.FindCont
ASKER
And I put PlaceHolder1.Controls.Add( dbPlaceHol der) instead in the ForEach Loop
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It is working great. Thank you for your helps and efforts.
I have another post for 500pts if you want to try.
https://www.experts-exchange.com/questions/26637414/asp-net-placeholder.html
I have another post for 500pts if you want to try.
https://www.experts-exchange.com/questions/26637414/asp-net-placeholder.html