Solved

user control basics help

Posted on 2004-08-06
10
244 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

759 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