开发者

ASP.NET listbox MaintainScrollPositionOnPostBack re-ordering items near bottom reloads listbox at top

I've got an ASP.NET (VB) page with two listboxes. Standard stuff - select an item on开发者_运维知识库 left, hit add button and it moves to right, etc. I've also got two buttons to move items up or down in the resulting list. My problem is that if I go to the 2nd to the last item (or any in that range) and move it down in the list the list resets the scrollbar to the top position. I want the focus to be on the item that was promoted or demoted, regardless of whether it is "beneath the fold" or not.

I've got MaintainScrollPositionOnPostBack in my page declaration and it works great for the page as a whole, and although it does nothing for the listbox, this is the type of behavior I'm looking for. Can this be done in just VB, without resorting to Javascript or AJAX?

Thanks in advance for any ideas or suggestions you may have. Code snippet below:

If lstToFields.SelectedIndex < lstToFields.Items.Count - 1 Then
    Dim RowNum As Integer = lstToFields.SelectedIndex
    Dim RowVal As ListItem = lstToFields.SelectedItem
    lstToFields.Items.RemoveAt(RowNum)
    lstToFields.Items.Insert(RowNum + 1, RowVal)
    lstToFields.SelectedIndex = RowNum + 1
End If


It appears that your problem is similar to one I had recently. I'm inclined to say that PostBack is your problem, and that you will have to handle the scroll position with JavaScript. My solution was as follows:

Create a HiddenField in the ASPX page to hold the current position of the ScrollBar.


< asp : HiddenField ID ="hdnScollTop" EnableViewState =true runat="server" />

Create JavaScript functions in a tag to save and load the value from the HiddenField, which you Get and Set the scroll position with. I also had an Updatelist() function which I had to use for my multi-select ListBox.

function Updatelist() { //details removed } function GetListBoxScrollPosition(){ var sel = document.getElementById('<%=lstbxStuff.ClientID%>'); var hdnScrollTop = document.getElementById('<%=hdnScollTop.ClientID %>'); hdnScrollTop.innerText=sel.scrollTop; } function SetListBoxScrollPosition(){ var sel = document.getElementById('<%=lstbxStuff.ClientID%>'); var hdnScrollTop = document.getElementById('<%=hdnScollTop.ClientID %>'); sel.scrollTop=hdnScrollTop.value; //not sure why it's in value when I clearly put it in innerText. This is what works. }

In Form_Load, register the JavaScript functions, and initialize the HiddenField Value. In the code-behind's Page_Load I set all of the scripts.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindACLs();
            //I use the hidden field to set the lstbxStuff 
            //scroll bar, which will scroll to the top anyway. 
            //This is to avoid a JavaScript error. 
            hdnScollTop.Value = "0";
        }
        else
        {
            lstbxStuff.Focus();
        }
        lstbxACLs.Attributes.Add("onclick", "GetListBoxScrollPosition();Updatelist();");
        lstbxACLs.Attributes.Add("onfocus", "SetListBoxScrollPosition();");


    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜