1

I have created a Custom Server Control (Inherited from GridView).

On the page, the GridView is DataBound to a DataSet, so I do not know at design time what columns will be present in my GridView.

Now, what I want to do is, to add a textbox in every Cell for the GridView Header row, and those textboxes will control column filtering. (textboxes are added using the GridView OnRowCreated method).

So far so good, the textboxes appear, and the filtering is working.

Problem is, after every postback, the Text value of the textboxes is lost. From my experimentations, this seems to be because I'm adding the textboxes too late in the Page/Control lifecycle.

How does one deal with this type of problem, where I would need to create and add the textboxes early in the Lifecycle (like, the GridView's OnInit), but adding the textboxes is dependant on information that is obtained later in the lifecycle?

4

3 回答 3

1

Why not store the values in the ViewState and read them back (refill the text boxes) on the postback?

于 2008-11-26T02:21:18.053 回答
0

您不必担心文本框的值,只需担心它们的 ID 以及创建它们的时间;只要您创建并“提供”具有相同数量的文本框及其各自(唯一(!))ID 的页面,控制状态(...嗯...或者可能是视图状态)将负责。

您可以同时使用 Page_Init 和 Page_Load ... Page_Init 有点推荐,但这取决于您的需要。

于 2008-11-26T02:51:34.293 回答
0

有一个有用的页面:http: //msdn.microsoft.com/en-us/library/ms178472.aspx 它特别说您需要使用Pre_Init事件来创建您的控件:

PreInit:将此事件用于以下操作:

  • 检查 IsPostBack 属性以确定这是否是第一次处理页面。
  • 创建或重新创建动态控件
  • 动态设置母版页。
  • 动态设置 Theme 属性。
  • 读取或设置配置文件属性值。
于 2009-02-19T20:05:47.197 回答