Solved

user control basics help

Posted on 2004-08-06
10
237 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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