5

这是我的第一篇文章!我非常绝望,所以我超越了我的标准谷歌搜索。我相信这是一个高级或专家级的 .NET 问题。

问题是我已经构建了一个 .NET Web 应用程序,它需要能够将用户控件动态插入到列表的中间。我对动态控件很满意,只要将它们添加到列表的末尾(即:我熟悉这样的文章:http: //msdn.microsoft.com/en-us/library/ ms972976.aspx)。但是,如果我需要将 UserControl 添加到 Controls 集合的前面或中间的某个位置,我几乎会迷失方向,因为控件的 UniqueID 被丢弃了。

作为一个简化的示例,假设我有一个面板,我正在向其中添加一个名为 MyControl.ascx 的用户控件列表。我还有一些可以在页面上触发的事件,需要将 MyControl.ascx 动态插入到面板控件集合的指定索引中。我还需要订阅 MyControl.ascx 上的一个或多个事件。这意味着需要在这些控件上的事件触发之前加载控件,否则它们将不会触发。如果您不知道我指的是什么,那么我要么措辞不佳,要么这个问题对您来说太难了:)

下面是一些 C# 伪代码来演示该问题。问题是 Controls.AddAt(index, control) 方法不会相应地调整控件的 UniqueID 值。例如,考虑 Controls 集合中的以下控件:

无论我是否实际编写直接依赖于 UniqueID 的代码,.NET 都间接使用 UniqueID 将在前一个回发中触发的事件与在新回发中加载的控件链接在一起。以我之前的例子:

在初始Page Load (Page.IsPostback == false)

<table>
  <tr>
    <td width='100'>Control index</td>
    <td width='75'>UniqueID</td>
    <td>Value</td>
  </tr>
  <tr>
    <td>0</td>
    <td>ctl00</td>
    <td>value1</td>
  </tr>
  <tr>
    <td>1</td>
    <td>ctl01</td>
    <td>value2</td>
  </tr>
  <tr>
    <td>2</td>
    <td>ctl02</td>
    <td>value3</td>
  </tr>
</table>

Page.IsPostback == false在某个其他控件想要在索引 0 处插入控件的回发 ( ) 之后:

如果我这样做,Controls.AddAt(0, newControl)那么 Controls 集合看起来像这样:

<table>
  <tr>
    <td width='100'>Control index</td>
    <td width='75'>UniqueID</td>
    <td>Value</td>
  </tr>
  <tr>
    <td>0</td>
    <td>ctl03</td>
    <td>value0  <== the controls' unique IDs do not shift!</td>
  </tr>
  <tr>
    <td>1</td>
    <td>ctl00</td>
    <td>value1</td>
  </tr>
  <tr>
    <td>2</td>
    <td>ctl01</td>
    <td>value2</td>
  </tr>
  <tr>
    <td>3</td>
    <td>ctl02</td>
    <td>value3</td>
  </tr>
</table>

因此,如果我使用 Value == value0 和 UniqueID == ctl03 单击控件中的链接按钮,则控件将在回发时按以下顺序排序,并且 UniqueID 不会按我想要的顺序排列。这将导致点击事件附加到错误的控件:

<table>
  <tr>
    <td width='100'>Control index</td>
    <td width='75'>UniqueID</td>
    <td>Value</td>
  </tr>
  <tr>
    <td>0</td>
    <td>ctl00</td>
    <td>value0  <== the control i wanted to throw the event</td>
  </tr>
  <tr>
    <td>1</td>
    <td>ctl01</td>
    <td>value1</td>
  </tr>
  <tr>
    <td>2</td>
    <td>ctl02</td>
    <td>value2</td>
  </tr>
  <tr>
    <td>3</td>
    <td>ctl03</td>
    <td>value3  <== the actual control to which the event is attached</td>
  </tr>
</table>

如果我不必处理来自这些动态控件的事件,这可能不会成为问题。这是我的代码:

// on init i just need to pull the records from the DB, turn them
// into a MyControl.ascx, and then add them to my panel
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    // get my List<SomethingFromDB> ordered by value and iterate through, 
    // adding controls to the control collection
    List<SomethingFromDB> myList = remoteService.GetSomeListFromTheDB();
    foreach(SomethingFromDB something in List<SomethingFromDB>)
    {
        // load a new MyControl.ascx
        MyControl myControl = (MyControl )LoadControl("~/Controls/MyControl .ascx");
        // populate the values of myControl with the "something"
        this.populateMyControl(something);
        // dynamically add the control to my panel
        this.myPanel.Add(myControl);     
        // subscribe to event
        myControl.SomeArbitraryEvent += new EventHandler(MyArbitraryHandler);
    }

    // This event gets fired by a magical control on the page and passes a 
    // new SomethingFromDB which needs to be inserted into the DB and then 
    // dynamically inserted into the Controls collection at the specified index
    private void SomeOtherControl_ClickInsert(object sender, MyControlEventArgs e)
    {
        // insert this record into the DB             
        remoteService.InsertIntoDB(e.SomethingFromDB);     
        // dynamically load this control
        MyControl myControl = (MyControl )LoadControl("~/Controls/MyControl .ascx");
        // get the index into which we will be inserting this control
        int index = e.NewIndex;                   
        //dynamically add the control to my panel at the specified index.
        // THIS DOES NOT ADJUST THE UNIQUEID VALUES ACCORDINGLY
        // AND IS THE SOURCE OF MY PROBLEM!
        this.myPanel.AddAt(index, myControl);
    }

请帮忙。这个要死我了。如果您需要更多信息或有疑问,请告诉我,我很乐意提供更多信息。非常感谢您的帮助!

4

2 回答 2

5

要使自动编号起作用,您希望在每次回发时以相同的顺序将控件添加到面板中。显然,在回发时,必须为控件分配其唯一 ID,否则您将无法获得控件事件。

UniqueID在第一次使用时初始化为Control.ID* 属性的值。如您所见,如果未设置该属性,它会自动生成为ctlxx。一旦分配,控件UniqueID就是只读的。

因此,如果您有某种形式的主键,您可以ID在创建控件时根据该主键设置控件的属性。然后UniqueID,当页面加载其状态时,将设置为该值。

  • 脚注:UniqueID属性实际上是命名容器的前缀和ID 属性的组合。
于 2009-02-20T16:03:21.317 回答
0

请在页面指令中使用以下属性。

MaintainScrollPositionOnPostback = true

我相信这会有所帮助。

于 2010-10-05T05:54:46.143 回答