Click here to Skip to main content
11,483,983 members (68,283 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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
Posted 12-Dec-12 22:40pm
PBGuy1.8K
Edited 12-Dec-12 22:55pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
shabari7 at 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 at 13-Dec-12 3:56am
   
Your cs file should return the data and your page should put it in to the list.
shabari7 at 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 at 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.
shabari7 at 13-Dec-12 4:17am
   
since the content is null, so I again tried to access in the method.
Christian Graus at 13-Dec-12 4:21am
   
Get rid of the ugly code to look up controls, and strongly type your master page
shabari7 at 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)
{
FillCountry();

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"));


}

[WebMethod]
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 at 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.
shabari7 at 13-Dec-12 4:21am
   
But master page is also needed ,
shabari7 at 13-Dec-12 4:21am
   
But master page is also needed ,
Christian Graus at 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
shabari7 at 13-Dec-12 4:24am
   
Got it, Let me give a try..
Christian Graus at 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.
shabari7 at 13-Dec-12 4:28am
   
How to strongly type a master page?
Christian Graus at 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'.
shabari7 at 13-Dec-12 4:31am
   
thank you
shabari7 at 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 at 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 ?
shabari7 at 13-Dec-12 4:48am
   
That too with page methods, it's becoming little complex
shabari7 at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 210
1 Frankie-C 155
2 Raje_ 155
3 OriginalGriff 147
4 Maciej Los 110
0 Sergey Alexandrovich Kryukov 8,489
1 OriginalGriff 7,943
2 Sascha Lefèvre 3,114
3 Maciej Los 2,581
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100