如何将选定的ListItem从ListBox动态复制到DropDownList并使其在网页中正确显示

时间:2013-08-16 16:35:36

标签: c# asp.net webforms

我的问题是代码隐藏看起来像是有效的,根据调试器,它确实(没有错误或异常)。不幸的是,它实际上并不适用于我的asp.net webforms网页,DropdownList实际上并没有填充ListBox的选定项目。这真的应该很简单,但显然不是。

我的研究表明,Listboxes和DropDownList应该在UpdatePanels中,并且子项作为触发器需要设置为true,并且在ScriptManager的帮助下它将拦截回发请求并仅进行部分页面更新。然而,当这不起作用时,我尝试直接调用updatepanels.update()方法,这也不起作用。

这是我的C#代码。

  protected void GroupList_SelectedIndexChanged(object sender, EventArgs e)
    {
        updategroupEntry();
    }

    protected void updategroupEntry() 
    {
        EnterAGroup.Items.Clear();
        foreach (ListItem li in GroupList.Items)
        {
            if (li.Selected)
            {
                EnterAGroup.Items.Add(li);
            }
        }
        OptionsUpdater.Update();
        OverallUpdater.Update();
    }

在我的代码中,我使用了嵌套的更新面板,这里显示了整体/周围的一个,我还在第二个DropdownList中包含了一个结束标记。这是第一个代码段。

<asp:UpdatePanel ID="OverallUpdater" UpdateMode="Conditional" runat="server" ChildrenAsTriggers="true">
       <ContentTemplate>

 //irrelevant code omitted

                <asp:Listbox AutoPostback="True" ID="GroupList" runat="server" Width="166px"  SelectionMode="Multiple" OnSelectedIndexChanged="GroupList_SelectedIndexChanged" DataSourceID="GroupSource" DataTextField="GroupName" DataValueField="GroupID">
                </asp:Listbox>
                <asp:SqlDataSource ID="GroupSource" runat="server"
                     ConnectionString="<%$ ConnectionStrings:ACESConnectionString %>" 
                    SelectCommand="SELECT [GroupName], [GroupID] FROM [PrimaryGroup] ORDER BY [GroupName]"></asp:SqlDataSource>

 //more code omitted

              <%--End of Options Panel --%>
                    </asp:Panel>
                </ContentTemplate>
              </asp:UpdatePanel> 

        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="viewapps" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="UpdateButton" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="FilterButton" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>  

在我的aspx页面中,我的代码围绕着我的DropDownList。

    <asp:UpdatePanel ID="OptionsUpdater" UpdateMode="Conditional" runat="server"  ChildrenAsTriggers="true" ViewStateMode="Enabled"> 
       <ContentTemplate>

  //other code omitted for brevity.

          <asp:TableCell>
             <asp:DropDownList runat="server" ID="EnterAGroup" BackColor="PaleVioletRed" AutoPostBack="true"></asp:DropDownList>
          </asp:TableCell>

 //other code omitted for brevity.

    </ContentTemplate>
 </asp:UpdatePanel> 

所以代码隐藏说它的作用,但它只是不在网页上更新。如何让它正常工作?

我很乐意根据要求提供更多代码。 此外,如果您需要测试代码,我建议您使用自己的SqlDataSource替换某些表中的2列。

更新 我尝试了Cherian M Paul的想法,并且它产生了奇怪的结果,当我从组中选择所有项目但不仅仅是少数时,它只有中途工作。

当选择所有组时,这是中途工作。

Half way working when ALL groups are selected

以下是仅选择部分图片时无法正常工作的图像。 Not working when only some are selected

更新

经过进一步调查后,有一个断开连接点,网页点击不再达到代码隐藏。

我有一个断开连接的视频。基本上,最后没有任何点击次数会触发我的代码隐藏中的事件。

https://docs.google.com/file/d/0B12ZVcp_VQ_xR3c0cU1LaXd5Vkk/edit?usp=sharing

2 个答案:

答案 0 :(得分:1)

我相信,您需要为ID为OptionsUpdater的第二个UpdatePanel添加触发器。与你对第一个的方式类似。

<asp:AsyncPostBackTrigger ControlID="GroupList" EventName="GroupList_SelectedIndexChanged" />

请尝试更新html。此外没有部分更新,同时使用更新面板,asp.net实际上完成了所有工作,但只有一部分页面更新,用户感觉它在后端做了一些事情。

我可以建议您使用jQuery实现这些任务吗?

答案 1 :(得分:0)

foreach (ListItem li in GroupList.Items)
{
    if (li.Selected)
    {
        EnterAGroup.Items.Add(li);
    }
}

上述代码无效,因为下拉列表不能包含多个选定项目。请更改下面的代码,然后它将工作。

foreach (ListItem li in GroupList.Items)
{
    if (li.Selected)
    {
        EnterAGroup.Items.Add(new ListItem(li.Text, li.Value));
    }
}