Solved

Retrieve my usercontrol loaded with loadtemplate in my datagrid

Posted on 2004-08-25
8
518 Views
Last Modified: 2012-06-27
Hi Experts,

I have created my own usercontrol.
I use it in my datagrid with loadtemplate("myusercontrol.ascx") to create a template column.

Now I want to retrieve my usercontrol in ItemCreated event of my datagrid, but don't know how to do ?
(e.Item.FindControl doesn't work because the name of my usercontrol is not generated)

Thanks in advance for your help.
0
Comment
Question by:noulouk
  • 4
  • 3
8 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11897566
I believe you will need to use FindControl.  Here is an abudance of information on manipulating User Controls, very helpful.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/usercontrols.asp

Regards,

Aeros
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 400 total points
ID: 11897572
At this point we have created a user control with a CategoryID property whose value dictates what subset of products are displayed in the DataGrid. All that remains is to set this property programmatically in an ASP.NET Web page. To demonstrate this, let's create a new Web page, Condiments.aspx, that will use the DisplayProductsByCategory.ascx user control to display just the condiment products (those whose CategoryID equals 2.)

When adding the user control in the HTML portion of Condiments.aspx, it is important that we provide an ID attribute for the user control. We'll use this ID to reference the user control in the code-behind class. That is, use the following declarative syntax when adding the user control in the HTML portion:

<skm:ShowProducts runat="server" id="beverages"></skm:ShowProducts>

In the ASP.NET Web page's code-behind class we must reference the user control programmatically and set its CategoryID property to the value 2. This can be accomplished in either one of two ways:

    * Use the FindControl(ID) method to locate the user control in the Web page's control hierarchy.
    * Create a protected member variable in the Web page's code-behind class having the same type as the user control and the same name as the user control's ID.

Let's examine each of these two approaches. The first approach can be accomplished using the following code:

private void Page_Load(object sender, System.EventArgs e)
{
   if (!Page.IsPostBack)
   {
      DisplayProductsByCategory myUC =
        (DisplayProductsByCategory) FindControl("beverages");
      myUC.CategoryID = 2;
   }
}

The FindControl(ID) method takes in a single string input and searches the page's control hierarchy for a control with an ID property value equal to the input parameter. If such a control is found, it is returned; otherwise null (or Nothing, in Microsoft® Visual Basic® .NET) is returned. FindControl(ID) returns an object of type Control, so we need to cast the return value from Control to the user control type, DisplayProductsByCategory.

The second method is to add a protected member variable with the appropriate type and name to the Web page's code-behind class. Once this has been done, the user control can be referenced directly by this member variable, as shown in the following example:

public class Condiments : System.Web.UI.Page
{
   protected DisplayProductsByCategory beverages;

   private void Page_Load(object sender, System.EventArgs e)
   {
      if (!Page.IsPostBack)
         beverages.CategoryID = 2;
   }
}

Notice that the protected member variable is of the correct type (DisplayProductsByCategory) and has the same name as the ID property of the user control in the Web page's HTML portion. Once this variable has been declared, it can be referenced directly, as shown in the Page_Load event handler. (Realize that if the name of the member variable and the user control's ID property value do not match up, you'll get a NullReferenceException exception.)

    Note   When creating user controls with Visual Studio .NET, the class name of the user control is the name of the user control's code-behind class, DisplayProductsByCategory in this instance. If you are not using a code-behind class for the user control, and are instead having the user control's source code in a server-side <script> block, you can specify the user control's class name in the @Control directive's ClassName attribute. (For more information on the @Control directive refer to the technical documentation.)

The following screenshot shows the Condiments.aspx page when visited through a browser. Note that the products displayed belong to the condiments product line, and that the reason this subset of products is displayed is because the ASP.NET Web page sets the user control's CategoryID property to 2, which is the CategoryID for condiments.
0
 
LVL 9

Author Comment

by:noulouk
ID: 11897716
I forgot to set the ID property of my usercontrol which appears to be a big mistake.
But I can't find my usercontrol in itemcreated event because it's a "template column" in fact.

Any idea.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11897736
you can open the actual .ascx usercontrol file and do it there, or the tutorial I linked to will show you how to do it in the code.  Just let me know if you need anymore info.

Regards,

Aeros
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 10

Expert Comment

by:jnhorst
ID: 11897785
e.Item.FindControl() will not work because you are trying to resolve the control against the Item's Controls collection rather than the Item.Cells(x).Controls collection.  Put a break point in your ItemCreated event and then as you break on the creation of each item, in the Imeediate Pane, test for controls in the cell where your templated column is, like this (x is the templated column index):

? e.Item.Cells(x).Controls.Count <hit enter>

If you have more than one, then check the type of each:

? e.Item.Cells(x).Controls(0).GetType().ToString()

Do this with Cells(0), Cells(1), etc... until you see which index in the collection is the one you are expecting.  You can then cast e.Item.Cells(x).Controls(y) to the type of your user control and access the properties.

John

0
 
LVL 10

Assisted Solution

by:jnhorst
jnhorst earned 100 total points
ID: 11897824
If you are able to set the user control's id, then FindControl() will work, but make sure you are calling it from the resolved Cell, not the Item:

variable = e.Item.Cells(x).FindControl("controlID")

John
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 11897847
Oops: Make sure to cast the result of the FindControl() to the proper datatype

VB.NET:
CType(e.Item.Cells(x).FindControl("controlID"), ControlsDataType)

C#

(ControlDataType)e.Item.Cells(x).FindControl("controlID");

"ControlsDataType" is the actual type name of your control.

John
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 11897886
My first post is wrong in saying that you test in the immediate pane for "Cells(0), Cells(1), etc..."

That should be "Controls(0), Controls(1), etc..." when you have more than 1 in the Controls.Count.

Sorry about that... ;-)
John
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

13 Experts available now in Live!

Get 1:1 Help Now