Click here to Skip to main content
13,594,789 members
Rate this:
Please Sign up or sign in to vote.
Hi All,

I have a gridview inside an UserControl. The Gridview has 3 fields. I have 3 textboxes on another page. I have registered the usercontrol to that page. I want to get value of Gridview field in the textboxes of another page , on firing the row command event of gridview.

Plz help...
Posted 27-Dec-12 23:50pm
Sergey Alexandrovich Kryukov 28-Dec-12 15:37pm
Is it a question? I see no problem in your situation. Do you see any? Why can't you get a value or add an event handler?
Besides, you always need to tag what UI library and/or application type do you use. If could be ASP.NET, WPF, Forms, etc.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question. I don't see a problem.

However, maybe your problem is the one typical for other members who asked question on related topics.

The problem is: the child controls of your controls are not directly visible to the code using your control. The only exposed property is Control.Controls, but it does not use strong typing for every members, so it's now so useful.

One option is to expose each child control instance you need to access as a property of your control, but I would not recommend it. This is certainly the easiest way, but it violates the isolation of implementation detail of your user control, that is, the encapsulation. This simple technique can be used in simple applications though.

More refined approach is to add some properties and events to your user control, only the functional ones. In the user control class implementations, they can be implemented using the child controls. For example, let's assume you have two child controls to access in a limited way: Label MyLabel should be accessible only to change its background color, and GridView GridView should respond to RowCommand event via the handler setup by the user of your user control. It could be something like this:

partial public class MyControl { // hopefully, inheritance list is left for other partial part, never repeat it

    GridView GridView = new GridView(); // keep private
    Label MyLabel = new Label(); // keep private


    public MyControl() {
        GridView.RowCommand += (sender, eventArgs) => { // add a permanent internal handler to it
           if (this.RowCommand != null)
               this.RowCommand.Invoke(this.GridView, new GridViewCommandEventArgs(/* ... */));
        }; // GridView.RowCommand
    } //MyControl

    public Color MyLabelBackColor { get { return MyLabel.BackColor; } set { MyLabel.BackColor = value; } } // very simple

    public event System.EventHandler<GridViewCommandEventArgs> RowCommand; // not as simple, see the constructor

} // class MyControl

Got the idea?


In many cases, you would need to make a step forward and re-package event argument of the event exposed to the user of the user control in a different event argument class which you can subclass from System.EventArgs or other most suitable event argument type. The problem is: if you assume that the access to the instance of the grid view is needed for a user to implement the even handler, the only reference is the sender parameter. This is not a string typing, will require type case, and, besides, it exposes a private user control member. Therefore, you can use the (generic) event type based on your own event args type, to provide the user with the information this user will really need. The code sample I've shown above will be nearly the same, only /* ... */ part will submit some really useful information and the event argument type will be different, a customized one.

Espen Harlinn 29-Dec-12 9:51am
Sergey Alexandrovich Kryukov 29-Dec-12 20:08pm
Thank you, Espen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.180621.3 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100