将动态控件插入到控件集合的中间位置

5
这是我的第一篇帖子!我非常着急,所以我超越了标准的谷歌搜索。我认为这是一个高级或专家级别的.NET问题。
问题在于我已经构建了一个.NET Web应用程序,需要能够动态地将用户控件插入到列表中间。如果只需要将控件添加到列表末尾,我对动态控件非常熟悉(例如:我熟悉像这样的文章:http://msdn.microsoft.com/en-us/library/ms972976.aspx)。但是,如果我需要将UserControl添加到Controls集合的前面或中间的某个位置,我就会迷失不知,因为控件的UniqueID会被破坏。
举个简单的例子,假设我有一个Panel,我要向其中添加名为MyControl.ascx的UserControl列表。页面上还有一些事件可以触发,需要动态地将MyControl.ascx插入到Panel的Controls集合的指定索引中。MyControl.ascx还有一个或多个事件需要订阅。这意味着控件需要在这些控件的事件触发之前加载,否则它们将不会触发。如果你不知道我在说什么,那么我可能表达得不好,或者这个问题可能太难了:)
以下是一些C#伪代码,以演示此问题。问题在于Controls.AddAt(index,control)方法不会相应地调整控件的UniqueID值。例如,在Controls集合中考虑以下控件:
无论我是否实际编写直接依赖于UniqueID的代码,.NET都间接地使用UniqueID将上一个postback中触发的事件与新postback中加载的控件链接在一起。以我的前面的例子为例:
在初始页面加载(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>

如果我点击一个值为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);
    }

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


现在看来,你的做法是错误的,你正在解释你的问题而不是解释你想要实现什么。这其中有一个区别,这使得很难建议更好的方法(有时候这些方法就是正确的方法)去解决一个特定的问题。请编辑你的问题! - John Leidegren
2个回答

5
为了让自动编号生效,您需要在每次回传时以相同的顺序将控件添加到面板中。显然,在回传时,必须分配控件的唯一ID,否则您将无法获得控件事件。
UniqueID 在第一次使用时初始化为 Control.ID* 属性的值。如果未设置该属性,则会自动生成 ctlxx,就像您所观察到的那样。一旦分配,控件的 UniqueID 就是只读的。
因此,如果您有某种主键形式,可以根据该主键创建控件时设置其 ID 属性。当页面加载其状态时,UniqueID 将被设置为该值。
注:UniqueID 属性实际上是命名容器前缀和 ID 属性的组合。

感谢回复,Sunlight。一旦将UniqueID添加到myPanel中,它就会被设置。问题在于当调用“SomeOtherControl_ClickInsert”时,控件已经在面板上并且已经分配了UniqueID。当我尝试插入我的控件时,已经为时过晚。 - jakejgordon
我真的很讨厌这个300字符限制。无论如何,我知道UniqueID是父控件和页面以及控件在控件集合中的索引的组合。我只是不知道如何将控件放在集合的中间并连接到正确的事件。 - jakejgordon
如果动态控件中的任何一个被点击,就需要在回发时将钩子连接到正确的事件上。您能详细说明一下吗?我可能表达不清楚,不太确定。 - jakejgordon
哦,等等……我现在明白你的意思了!当我将控件添加到面板时,我还没有设置它的control.ID——这就是为什么它会设置自己的uniqueID。由于我确实有一个主键值,我应该能够将ID设置为该键。我会试着解决一下,并很快回复。谢谢! - jakejgordon
好的,你现在正式成为一个天才。那就是问题所在!我没有在将控件添加到另一个控件集合之前设置控件的ID,因此.NET正在使用他们自己的计数器。当我将ID分配给自己的主键值时,问题得到了解决。谢谢Sunlight!! - jakejgordon

0
请在页面指令中使用以下属性。
MaintainScrollPositionOnPostback = true

我相信这会有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接