Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey all,
let say i have a panel or text control in form, normally it is possible to access controls by id but how can i access control from class library.

What I have tried:

C#
public class FormDesign
{
public void fun()
{
textbox1.text="some text";   //notworking
foreach(control ct in panel)    //not working
{
//some code
}
}
}
Posted
Updated 9-Sep-20 0:17am
Comments
Dave Kreskowiak 8-Sep-20 22:15pm    
Is this an ASP.NET app or Windows Forms?
Aftab Iqbal Clips 8-Sep-20 23:44pm    
its an ASP.NET application

Check that the TextBox has runat="server" in it's HTML:
ASP.NET
<asp:TextBox ID="textbox1" Columns="2" MaxLength="3" Text="1" runat="server"/>
The ID will need to match the name you use in the C# code.
The Panel will need the same, but you will also need to access it's Controls collection, and remember that C# is case sensitive: "control" is not the same as "Control":
C#
foreach (Control ct in panel.Controls)


And by the way: "It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Help us to help you!
 
Share this answer
 
Comments
Aftab Iqbal Clips 9-Sep-20 2:11am    
as i mentioned in question i want to access asp page control from class
there is textbox inside panel
panel is added with markup
textbox is added programmatically
You can't access controls from a class library as the controls are defined on the code-behind of the aspx file and populated for you by asp.net. If your class really must run from asp.net controls then set up properties for those controls

C#
public class FormDesign
{
    public Panel MyPanel {get; set;}

    public void Fun()
    {
        foreach(Control ct in this.MyPanel)
        {


When you create FormDesign in your code-behind class populate the relevant properties

C#
var formDesign = new FormDesign();
formDesign.MyPanel = panel1;
formDesign.Fun();


You can also pass the controls in as params to Fun, it all depends on what your code needs to do.
 
Share this answer
 
Comments
Aftab Iqbal Clips 13-Sep-20 2:20am    
getting error on
sf.pnl = panel1;

Error CS0103 The name 'panel1' does not exist in the current context
F-ES Sitecore 13-Sep-20 8:19am    
Change "panel1" to whatever the name of your panel is.
Aftab Iqbal Clips 14-Sep-20 1:39am    
yes it is working now but findcontrol not appearing in intellsense of class
any solution?

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