Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 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
Comments
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.
—SA

1 solution

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:

C#
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?

[EDIT]

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.

—SA
 
Share this answer
 
v4
Comments
Espen Harlinn 29-Dec-12 9:51am    
5'ed!
Sergey Alexandrovich Kryukov 29-Dec-12 20:08pm    
Thank you, Espen.
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900