各位老师好!
请教一个关于控件刷新后的定位问题。
现做一个在线聊天室,在ASP.NET中的updatepanel里拖进去一个txtContext控件用来显示聊天记录(已设置滚动条),
即:在点击“发言”按钮之后,滚动条要留在新发言内容的位置,而不要重新居顶。如何做到?
分数不多,真心请教,谢谢!

解决方案 »

  1.   

    设置窗口滚动条的位置:
    window.scroll(0,document.body.scrollHeight)例,在打开网页时让页面处于页面底端
    <body   onload="window.scroll(0,document.body.scrollHeight)">
      

  2.   

    设置组件的滚动条的位置:假设有个输入框,id为TextArea1
    方法一:
    TextArea1.scrollTop=100;//滚动到输入框的100位置
     方法二:
    document.getElementById("TextArea1").scrollTop=document.getElementById("TextArea1").scrollHeight; //滚动到输入框的底端体验代码:
    <title>设置</title> 
    </head> 
    <body> 
    <div id="Layer1" style="background:gray;width:300px;height:100px;overflow:auto;">Happy New Year ! ! !<p> 
    <input type=button name='button_export' onclick=OpenWord() value=设置> 
    <p><p><p><p><p><p><br/><P><p><p><p><p><p><p><p><p><p><p>><p><p><p><p><p> 
    <br/><P><p><p><p><p><p><p><p><p><p><p> 
    <p><p><p><p><p><br/><P><p><p><p><p><p><p><p><p><p><p> 
    <p><p><p><p><p><br/><P><p><p><p><p><p><p><p><p><p><p> 
    <p><p><p><p><p><br/><P><p><p><p><p><p><p><p><p><p><p> 
    this is a test! </div> <p>this is a test!</p> </body> 
    </html> 
    <script language="javascript"> 
    function OpenWord(){ 
    Layer1.style.border=1 
    Layer1.scrollTop=100; 

    </script> 
      

  3.   

    <script type ="text/javascript" >
       txtContext.scrollTop = txtContext.scrollHeight;
       document.getElementById("UpdatePanel1").scrollTop=document.getElementById("UpdatePanel1").scrollHeight; //滚动到输入框的底端
       document.getElementById("txtContext").scrollTop=document.getElementById("txtContext").scrollHeight; //滚动到输入框的底端   function AppendText(id)
       {
            var image=document.getElementById (id).getAttribute ("title");
            var txt=document.getElementById ("txtSend");
            txt.value+="/表情:"+image+"_"+id+":/";
            txt.focus();
       }   </script> 
    </head>
    <body onload ="selectTxtbox()" background="image/index.jpg">
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            &nbsp;</div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick">
                    </asp:Timer>
                    <table width="99%">
                        <tr>
                            <td style="width: 715px">
                                <asp:Label ID="labWelcome" runat="server" Text="<font color=black size=4> <b>★★★★★★★★★★★★★★★★<br><center>--欢迎进入高博宿舍管理系统--在线聊天区--</center></b></font>" Width="716px"></asp:Label>&nbsp;
                                <asp:Panel ID="PanelContext" runat="server" Height="300px" ScrollBars="Auto" Width="99%" BackColor="#E0E0E0" OnDataBinding="PanelContext_DataBinding">
                    <asp:Label ID="txtContext" runat="server" OnDataBinding="txtContext_DataBinding" OnLoad="txtContext_Load"></asp:Label>
                                    </asp:Panel>......不行啊 没成功