Accessing stuff in web user control - Round 3

Alot of what I'm doing on this project is very exploratory for me, so forgive me if I ask the same question again.

I know that I can access members of a web user control if I expose them first:

public string SetLabelName
{
set{this.label1.text=value;}
get{return label1.text;}
}


I have also been shown that I can call a function on that control that sets the values locally by passing a long list of arguments or passing an array:

customer custControl = new customer();
               custControl.fillLabels(args);


Here is my new question.  I just did a bunch of reading and I was going over the static keyword.  When I make my members public static,  I can call them using a type name instead of an instance reference.  When I type the dot operator, it shows the members properly and everything compiles perfectly.

So now,  I have some static members in a class,  and when I try to run in debug mode it throws an error on a page thats not part of my code.  Here is the output:
Compiler Error Message: CS0176: Static member 'CMAlite.Controls.Customer.txtFirstName' cannot be accessed with an instance reference; qualify it with a type name instead.
Line 66:             #line default
Line 67:             #line hidden
Line 68:             this.txtFirstName = __ctrl;
Line 69:            
Line 70:             #line 8 "http://localhost/CMAlite/Controls/customer.ascx"

I don't have that anywhere in any file that I wrote, so I must be doing something that asp.net doesn't like.  

Any ideas would be greatly appreciated.
thanks
dave

LVL 2
KaiserSoseAsked:
Who is Participating?
 
praneethaConnect With a Mentor Commented:
if this is your first webusercontrol...try making one small webusercontrol..with just one textbox..and try using it..and if it works fine...

propably you have more idea about what u r doing...

and in codebehind while declaring your usercontrol...

CMAlite.Controls.Customer customerControl;//complete qualified namespace name...

i don't see any problem ...just make sure if u have declaration right in codebehind..

if u run your project in debug mode..set the debug point near set property...and see what is happening
0
 
praneethaCommented:
i think when u have a static method...which means you can;t have instance of class...

so change
set{this.label1.text=value;}


change that to with out this....

try with

set
{
label1.Text
}
0
 
KaiserSoseAuthor Commented:
I don't even have  set/get function to expose the members in there.  My other class just makes a call to Customer.txtFirstName.Text  and it compiles but errors at runtime with the above text.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
praneethaCommented:
is customer...user control name...

and is it static class?
0
 
KaiserSoseAuthor Commented:
Yes to both.

public class Customer : System.Web.UI.UserControl
{
public static System.Web.UI.WebControls.TextBox txtFirstName;
.
.
.
other stuff
.
.
.
}
0
 
praneethaCommented:
ok just create a new webform and drag a textbox and change it to

public static System.Web.UI.WebControls.TextBox txtFirstName;


and run it it throws the same error...

when u go to view->source..you can see the line it is talking about..

i guess it is some code which .net writes to set the textboxes using this...

Line 128:            #line default
Line 129:            #line hidden
it says it is hidden...so i guessi t is some internal code..

but good question...
0
 
praneethaCommented:
i am not sure if i am right..may be someone else can provide good explanation
0
 
KaiserSoseAuthor Commented:
I'm not sure either.  If I can't get this to work then I'm going to use the other method of exposing the members separately.

dave
0
 
KaiserSoseAuthor Commented:
Check this out...  

This class throws an error on the set function of the public string.  It says Object reference not set to an instance of an object.  Stepping through the code, the 'value'  is being passed correctly.


public class Customer : System.Web.UI.UserControl
{

protected internal System.Web.UI.WebControls.TextBox txtFirstName;


private void Page_Load(object sender, System.EventArgs e)
{
      //nothing
}


public string expTxtFirstName
{
      set
      {
            txtFirstName.Text = value;
      }
      get
      {
            return txtFirstName.Text;
      }
}
}
0
 
praneethaCommented:
isi t only for this property....do all the other properties work fine?
0
 
KaiserSoseAuthor Commented:
No, none of them work, same error.
0
 
praneethaCommented:
then i guess the problem is with how u r calling this property...

cope paste the code in aspx page..

the declaration of usercontrol
calling the property
0
 
KaiserSoseAuthor Commented:
protected void loadUserDetails(object sender, CommandEventArgs e)
            {
                  CustomerFunctions objCF = new CustomerFunctions();
                  CustomerInfo objCust = new CustomerInfo();
                  objCust = objCF.loadUserDetails(e.CommandArgument.ToString());

                  //Account Tab Data
                  Customer custObj = new Customer();
                  custObj.expTxtFirstName = objCust.firstName;     <-----------------------It makes it to this line just fine and dies.
0
 
praneethaCommented:
did u drag and drop the usercontrol onthe web form?
0
 
KaiserSoseAuthor Commented:
No I did not.  I am using a tab control that only shows tab 1 in design view.   I tried to drag and drop, but it either ends up on tab 1 or in limbo.  I manually entered the control registration and then put the user control tag where I wanted it.  Could I have missed something?  Here is my 2 lines:

<%@ Register TagPrefix="s2f" TagName="customerControl" src="Controls/customer.ascx" %>

<s2f:customerControl id="customerControl" runat="server" />
0
 
praneethaCommented:
in the code behind...

don't do that....assuming Customer is usercontrol class...
Customer custObj = new Customer();
               custObj.expTxtFirstName = objCust.firstName;

in the decalaration region that would be before Page_load...

declare..

Customer customerControl;//same as id in HTML

and then

custometControl.expTxtFirstName="whatever"  
0
 
KaiserSoseAuthor Commented:
Same deal, as soon as the code steps into the Set function on the ascx codebehind it can't find txtFirstName.   Do you think I could save alot of this headache by making Web Custom Controls instead of Web User Controls?
0
 
praneethaCommented:
is it doing for all properties...or just txtFirstName...

try with out interbal in your textbox declaration...(it should not make a diff though)
change your set property to that
  set
     {
          txtFirstName.Text = "testing";
     }

with out setting any properties if you run the application can u see the user control on your webform?

public class Customer : System.Web.UI.UserControl
{

protected internal System.Web.UI.WebControls.TextBox txtFirstName;


private void Page_Load(object sender, System.EventArgs e)
{
     //nothing
}


public string expTxtFirstName
{
     set
     {
          txtFirstName.Text = value;
     }
     get
     {
          return txtFirstName.Text;
     }
}
}
0
 
KaiserSoseAuthor Commented:
Ok, I tried it with this and it still couldn't find txtFirstName:

 set
     {
          txtFirstName.Text = "working";
     }


I can see the user control when the page loads because I don't set those properties until you search for a user.  That part works fine, until you try to load the users data into the textboxes.

This is straight from that class:


namespace CMAlite.Controls
{
      using System;
      using System.Configuration;
      using System.Data;
      using System.Data.SqlClient;
      using System.Drawing;
      using System.Web;
      using System.Web.UI.WebControls;
      using System.Web.UI.HtmlControls;

      /// <summary>
      ///            Summary description for customer.
      /// </summary>
      public class Customer : System.Web.UI.UserControl
      {
            protected internal System.Web.UI.WebControls.TextBox txtFirstName;
            protected internal System.Web.UI.WebControls.TextBox txtLastName;
            //The rest taken out for readability but all are declared the same way

      

            private void Page_Load(object sender, System.EventArgs e)
            {
                  if(!IsPostBack)
                  {                        
                        bindDropDownLists();
                  }
            }


            /// <summary>
            ///            Loads all data for the items in the dropdown lists.
            /// </summary>
            private void bindDropDownLists()
            {
                  //Working function that binds dropdown lists from a sql db                  
            }


            #region Exposed Members
            public string expTxtFirstName
            {
                  set
                  {
                        txtFirstName.Text = "working";//value;
                  }
                  get
                  {
                        return txtFirstName.Text;
                  }
            }
        }
}
0
 
KaiserSoseAuthor Commented:
This project is making me go gray....    >8^]
0
 
praneethaCommented:
hmm could you make it work with small usercontrol..

believe me once u learn how to do it...it makes your life lot easier...

0
 
KaiserSoseAuthor Commented:
Yeah, I'll probably end up doing that.

Thanks so much for your help

dave
0
 
praneethaCommented:
ok good luck...u rwelcome..

let me know if you could fix it...

and thanks for the points :)
0
All Courses

From novice to tech pro — start learning today.