Solved

user control basics help

Posted on 2004-08-06
10
233 Views
Last Modified: 2010-04-15
i have my user control named edit_version_datagrid that i am placing on my WebForm1.aspx page like so...

<%@ Register TagPrefix="uc1" TagName="edit_version_datagrid" Src="edit_version_datagrid.ascx" %>

<uc1:edit_version_datagrid id=Edit_version_datagrid1 runat="server"></uc1:edit_version_datagrid>


on my user control edit_version_datagrid.ascx, i have a public string...

public string CtypeNumber;

Can someone show me how, in my code behind of WebForm1.aspx, i can set the value of the public string of my user control?

I thought it would be:

Edit_version_datagrid1.CtypeNumber = "5"

but i am getting an error saying that type or namespace 'Edit_version_datagrid1' could not be found.
0
Comment
Question by:trevoray
  • 5
  • 5
10 Comments
 
LVL 2

Expert Comment

by:Daeljan
ID: 11736272
Ok, I think that the problem may be the following:

When you use a user control on your form, it isn't automatically declared in the pages server code, so this is what you need to do. For example, something like this:

protected AppNameSpace.OtherNameSpace.Edit_version_datagrid Edit_version_datagrid1;

I am guessing that you control class name is Edit_version_datagrid, which explains the name Edit_version_datagrid1. The part AppNameSpace.OtherNameSpace represent the namespace path to your class.

You must also have the variable that you want to access declared as public. However, a more elegant way of doing this is as follows:

private string _CtypeNumber;

public string CtypeNumber
{
get {return _CtypeNumber;}
set {_CtypeNumber = value;}
}

You may even want to make this variable persitant, in which case you can use the viewstate as follows:

public string CtypeNumber
{
get {return ViewState["CtypeNumber"]==null?string.Empty:ViewState["CtypeNumber"].ToString();}
set {ViewState["CtypeNumber"] = value;}
}
0
 

Author Comment

by:trevoray
ID: 11737145
is this function on the .ascx page or the .aspx page? and if it's on the .ascx page, how do i call it from my .aspx page?

private string _CtypeNumber;

public string CtypeNumber
{
get {return _CtypeNumber;}
set {_CtypeNumber = value;}
}



thanks!
0
 
LVL 2

Expert Comment

by:Daeljan
ID: 11737612
Hi, the code snippet goes in the Edit_version_datagrid.ascx.cs file, and it is called from the aspx page as follows:

declaration of control is aspx:

protected AppNameSpace.OtherNameSpace.Edit_version_datagrid Edit_version_datagrid1;


calling of method / attribute from aspx:

Edit_version_datagrid1.CtypeNumber = "value";

Remember that if this value does not appear to be being set, it is because you need to make it persist in the viewstate.
0
 

Author Comment

by:trevoray
ID: 11737628
so, even though i drag and drop this control onto my .aspx page in the designer, it doesn't create the code needed to declare it?
0
 

Author Comment

by:trevoray
ID: 11737720
also, i'm sorry, but i'm not too knowledgable on this...

how do i declare the control in my namespace using your example below? i cannot figure out where to replace appnamespace and othernamespace, etc.

my namespace is as so:

namespace table_editor_toolkit
{
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.DropDownList SelectCtype;
            protected System.Data.SqlClient.SqlConnection sqlConnection1;
            protected System.Data.SqlClient.SqlCommand sqlCommand1;

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:Daeljan
ID: 11737742
Well... no!! It doesn't. I think it ought to, personally.
0
 
LVL 2

Expert Comment

by:Daeljan
ID: 11737808
The complete refence for your page would be:
table_editor_toolkit.WebForm1

In a similar way, look at the code for your user control to see the complete reference for that.


If it is something like this.....
namespace table_editor_toolkit
{
     /// <summary>
     /// Summary description for Edit_version_datagrid .
     /// </summary>
     public class Edit_version_datagrid  : System.Web.UI.UserControl
     {


... the complete reference would be:

table_editor_toolkit.Edit_version_datagrid

and your declaration in the aspx.cs would be:
protected table_editor_toolkit.Edit_version_datagrid Edit_version_datagrid1




0
 

Author Comment

by:trevoray
ID: 11737832
oh, ok, i thought you were saying to declare it on the .aspx page, but you mean that i need to declare it on itself? and once i do that i can call it from the .aspx page? lemme try...
0
 

Author Comment

by:trevoray
ID: 11737876
or do i have to declare it on both the .aspx and the .ascx pages?

0
 
LVL 2

Accepted Solution

by:
Daeljan earned 500 total points
ID: 11740136
Hi,

(1) The aspx is the container.
(2) It contains your ascx.
(3) In order for the aspx server code to be able to access the ascx methods, it needs a formal explicit declaration of the ascx object in the aspx.cs server code.

So all you need to do is declare the presence of the ascx object in the aspx server code, for example, in the aspx.cs code, your declaration would look something like this:

protected table_editor_toolkit.Edit_version_datagrid Edit_version_datagrid1

...and you would call it's methods like this:

Edit_version_datagrid1.CtypeNumber = "value";
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
XAML: Layout 8 25
c# LinkButton OnClientClick 2 34
Finally almost got it 5 20
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now