Saturday, January 16, 2010

C#, Accessing page control properties from within a class?

Hello, I've got four different pages all repeating the same function of code, in-line, to query a database. I've moved this code into one method in a new class file, under the company namespace. This all works fine!





However, even though I have specified the class as such:





public class className : System.Web.UI.Page





I cannot access the controls, such a label from the class itself. Please could someone help me? I need to access these controls, inherit if you will, without using the codebehind option.





Thank you.





Note: Whole purpose of this is to allow the class to update the label.text property from within the webpage.C#, Accessing page control properties from within a class?
Hi





If you want to access Webcontrol elements from a Code project you have to add System.Web as reference and import some namespaces.





using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.HtmlControls;





public class MyPage : System.Web.UI.Page


{


////You class definition


}





Edit :





Recursive(System.Web.UI.Control a,string id) {


foreach (System.Web.UI.Control u in a.Controls)


{


if (u.GetType().ToString() == ';System.Web.UI.WebControls.Label';)


{


if (u.ID == id)


{


System.Web.UI.WebControls.Label lbl = (System.Web.UI.WebControls.Label)u;


return lbl.Text;


}


}


else


Recursive(u,id);


}


}

No comments:

Post a Comment