UserControl里面的GridView [英] GridView inside UserControl

查看:116
本文介绍了UserControl里面的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在UserControl中有一个gridview。 Gridview有3个字段。我在另一页上有3个文本框。我已将usercontrol注册到该页面。我希望在触发另一个页面的文本框中获取Gridview字段的值,触发gridview的row命令事件。





Plz帮助...

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...

推荐答案

请参阅我对该问题的评论。我没有看到问题。



但是,也许你的问题是其他成员就相关主题提出问题的典型问题。



问题是:使用您的控件,代码无法直接看到控件的子控件。唯一暴露的属性是 Control.Controls ,但它并没有对每个成员使用强类型,所以它现在非常有用。



一个选项是公开你需要作为控件属性访问的每个子控件实例,但我不推荐它。这当然是最简单的方法,但它违反了用户控件的实现细节的隔离,即封装。这种简单的技术可用于简单的应用程序。



更精细的方法是向用户控件添加一些属性和事件,仅添加功能属性和事件。在用户控件类实现中,可以使用子控件实现它们。例如,让我们假设您有两个子控件以有限的方式访问: Label MyLabel 只能更改其背景颜色,并且 GridView GridView 应通过用户控件用户设置的处理程序响应 RowCommand 事件。它可能是这样的:



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





有想法吗?







在很多情况下,你需要做一个向前步骤并重新打包在不同的事件参数类中向用户控件的用户公开的事件的事件参数,您可以从 System.EventArgs 或其他最合适的子类事件参数类型。问题是:如果您假设用户需要访问网格视图实例来实现偶数处理程序,则唯一的参考是 sender 参数。这不是字符串输入,需要类型大小写,此外,它还公开了一个私有用户控件成员。因此,您可以使用基于您自己的事件args类型的(通用)事件类型,为用户提供此用户真正需要的信息。我上面显示的代码示例几乎相同,只有 / * ... * / 部分将提交一些非常有用的信息,并且事件参数类型将是不同的,定制的。



-SA



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.

—SA


这篇关于UserControl里面的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆