Solved

Accessing stuff in web user control  - Round 3

Posted on 2004-09-27
23
354 Views
Last Modified: 2010-04-15
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

0
Comment
Question by:KaiserSose
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 11
23 Comments
 
LVL 15

Expert Comment

by:praneetha
ID: 12162195
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
 
LVL 2

Author Comment

by:KaiserSose
ID: 12162244
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
 
LVL 15

Expert Comment

by:praneetha
ID: 12162272
is customer...user control name...

and is it static class?
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 2

Author Comment

by:KaiserSose
ID: 12162295
Yes to both.

public class Customer : System.Web.UI.UserControl
{
public static System.Web.UI.WebControls.TextBox txtFirstName;
.
.
.
other stuff
.
.
.
}
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12162412
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
 
LVL 15

Expert Comment

by:praneetha
ID: 12162419
i am not sure if i am right..may be someone else can provide good explanation
0
 
LVL 2

Author Comment

by:KaiserSose
ID: 12162526
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
 
LVL 2

Author Comment

by:KaiserSose
ID: 12164134
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
 
LVL 15

Expert Comment

by:praneetha
ID: 12164224
isi t only for this property....do all the other properties work fine?
0
 
LVL 2

Author Comment

by:KaiserSose
ID: 12164231
No, none of them work, same error.
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12164277
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
 
LVL 2

Author Comment

by:KaiserSose
ID: 12164335
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
 
LVL 15

Expert Comment

by:praneetha
ID: 12164349
did u drag and drop the usercontrol onthe web form?
0
 
LVL 2

Author Comment

by:KaiserSose
ID: 12169067
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
 
LVL 15

Expert Comment

by:praneetha
ID: 12169493
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
 
LVL 2

Author Comment

by:KaiserSose
ID: 12169536
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
 
LVL 15

Expert Comment

by:praneetha
ID: 12169674
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
 
LVL 2

Author Comment

by:KaiserSose
ID: 12169837
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
 
LVL 15

Accepted Solution

by:
praneetha earned 500 total points
ID: 12169998
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
 
LVL 2

Author Comment

by:KaiserSose
ID: 12170459
This project is making me go gray....    >8^]
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12170583
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
 
LVL 2

Author Comment

by:KaiserSose
ID: 12170660
Yeah, I'll probably end up doing that.

Thanks so much for your help

dave
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12170797
ok good luck...u rwelcome..

let me know if you could fix it...

and thanks for the points :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question