Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

Retrieve my usercontrol loaded with loadtemplate in my datagrid

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
noulouk
Asked:
noulouk
  • 4
  • 3
2 Solutions
 
AerosSagaCommented:
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
 
AerosSagaCommented:
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
 
nouloukAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
AerosSagaCommented:
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
 
jnhorstCommented:
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
 
jnhorstCommented:
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
 
jnhorstCommented:
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
 
jnhorstCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now