Solved

user control basics help

Posted on 2004-08-06
10
232 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Expert Comment

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

Expert Comment

by:Daeljan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

16 Experts available now in Live!

Get 1:1 Help Now