将变量传递给用户控件 [英] Passing Variables into User Control

查看:98
本文介绍了将变量传递给用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义用户控件,它是一个gridview。我需要执行数据库查询并根据我加载控件的页面上的内容将数据绑定到该gridview。我正在动态创建控件,所以我想知道我是否可以重载构造函数或其他东西?或者我是否需要回拨或其他什么?用户控件正在页面上的重载OnInit()方法中实例化。谢谢!

I have a custom user control, and in it is a gridview. I need to perform a database query and bind data to that gridview based on what is on the page I loaded the control onto. I''m dynamically creating the controls, so I''m wondering if I can overload the constructor or something? Or do I have to call a postback or something? The user controls are being instantiated in an overloaded OnInit() method on the page. Thanks!

推荐答案

如果你将一些数据传递给某个类的实例并在某个地方使用它而不仅仅是这个传递方法,它就不能是一个变量;它必须是类成员,字段,属性或两者(由字段支持的属性)。



是的,您可以添加具有适当参数的构造函数,但这将允许您只修改一些(实例)成员的某些值。这不是设计使用控制类的预期方法。



标准设计仅使用无参数构造函数。要解决您的问题,您需要添加内部或(更常见的)公共读写属性。请注意,出于某些原因,使用内部或公共字段被认为是一种不好的风格。



-SA
If you pass some data to an instance of some class and use it somewhere beyond just this passing method, it cannot be a "variable"; it has to be a class member, a field, a property, or both (property backed by a field).

Yes, you can add a constructor with appropriate parameters, but this will allow you to modify some value of some (instance) member(s) only once. And this is not the intended approach for the design of use control classes.

The "standard" design is using parameterless constructors only. To solve your problem, you need to add an internal or (more usually) public read-write property. Note that using an internal or public field is considered a bad style, by some good reasons.

—SA


这篇关于将变量传递给用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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