Click here to Skip to main content
15,301,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to access content page controls in a class file?

I invoked a method of .cs class file through page methods[static method] in code behind of contentpage.
Master is null,
content place holder is null,
How to get the controls of this content place holder into .cs class file>>>>?

Actually I want to fill the drop down list through a method in .cs class file, so just to hinder page post, 'm using page methods, hence I need those controls, any suggestion is appreciated
Updated 12-Dec-12 21:55pm

1 solution

You should NEVER do this, because it breaks the separation of logic. You should pass data from your controls in to cs files, but not the controls themselves.
[no name] 13-Dec-12 3:54am
Actually I want to fill the drop down list through a method in .cs class file, so just to hinder page post, 'm using page methods, hence I need those controls,
any suggestion is appreciated
Christian Graus 13-Dec-12 3:56am
Your cs file should return the data and your page should put it in to the list.
[no name] 13-Dec-12 4:01am
Thank you, but, I knew it, but my case is different, as I am using static method , I can't get UI controls of the page class in static method, so I am trying to fill the items from another class,
Christian Graus 13-Dec-12 4:08am
Your page class has the UI controls. It can call a static method and pass in data. What you're saying makes no sense. The instance of a page class is the start and end of where your UI elements exist. It's not static. A non static class can call static methods on other classes.
[no name] 13-Dec-12 4:17am
since the content is null, so I again tried to access in the method.
Christian Graus 13-Dec-12 4:21am
Get rid of the ugly code to look up controls, and strongly type your master page
[no name] 13-Dec-12 4:15am
My Code
public partial class ContentMaster : System.Web.UI.Page
protected void Page_PreInit(object sender, EventArgs e)
// this.MasterPageFile = "Master2.Master";

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)

ddlCountry.Attributes.Add("onchange", "javascript:return fnFill()");


private void FillCountry()

foreach (string[] str in new string[][] { new string[] { "India", "1" }, new string[] { "Sri Lanka", "2" }, new string[] { "SavyaSachi", "3" } })
ddlCountry.Items.Add(new ListItem(str[0], str[1]));
ddlCountry.Items.Insert(0,new ListItem("Select","-1"));


public static bool FillCountry2()

return (new SetControls()).FillDrodDownList(1,(ContentPlaceHolder)new Master1().FindControl("ContentPlaceHolder1"));
SetControls Class

public bool FillDrodDownList(int i, ContentPlaceHolder content)

ContentPlaceHolder cph = (new Master1()).FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
if (cph != null)
DropDownList ddl = cph.FindControl("ddlCountry2") as DropDownList;

foreach (string[] str in new string[][] { new string[] { "a", "1" }, new string[] { "b", "2" }, new string[] { "c", "3" } })
ddl.Items.Add(new ListItem(str[0], str[1]));

ddl.Items.Insert(0, new ListItem("Select", "-1"));
return true;
Christian Graus 13-Dec-12 4:17am
So, none of this makes sense. Don't pass in a contentplaceholder. That's presentation layer code. Pass in a dropdownlist if you must, but even that is retarded. This whole method belongs in the presentation layer. I find if I must use ASP.NET and not MVC, I create a base class for my pages, and in it, I create a strongly typed MasterPage property, so I can access the actual Master page instead of the base class, and access controls, if I need to, in my pages.
[no name] 13-Dec-12 4:21am
But master page is also needed ,
[no name] 13-Dec-12 4:21am
But master page is also needed ,
Christian Graus 13-Dec-12 4:22am
What ? That makes zero sense. I told you to improve your access to your master page class, that would not do anything if you were not using a master page
[no name] 13-Dec-12 4:24am
Got it, Let me give a try..
Christian Graus 13-Dec-12 4:26am
Good luck. You will find that to make ASP.NET workable, you need to extend the page base class, first thing is to strongly type your master page, but you'll find as you go there will be lots of helper methods you can write to make your life easier.
[no name] 13-Dec-12 4:28am
How to strongly type a master page?
Christian Graus 13-Dec-12 4:29am
Just cast it. If your master page class is called MyMasterPage, create a property that returns 'this.Master as MyMasterPage'.
[no name] 13-Dec-12 4:31am
thank you
[no name] 13-Dec-12 4:41am
content place holder is null, even the get property returns null
I inherited base class in master class, in base class, inherited, web.ui.masterpage,no luck
Christian Graus 13-Dec-12 4:43am
I guess you need to look at your aspx to work out why it's null then. It will stay null, no matter where you look for it, until you fix it. Why do you need this place holder ? Why not just grab the drop down list inside it ?
[no name] 13-Dec-12 4:48am
That too with page methods, it's becoming little complex
[no name] 13-Dec-12 4:47am
Actually I have 4 content pages,each with same drop down list in it, so I am trying to make a common method to serve the requests from these pages,
Christian Graus 13-Dec-12 14:11pm
Create a method to get the list of values, for sure. But put it in to the UI in the page.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900