Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

user control basics help

Posted on 2004-08-06
10
Medium Priority
?
250 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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 aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

715 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