<asp:UpdatePanel ID="ChatBufferPanel" runat="server" UpdateMode=Conditional>
<ContentTemplate>
<asp:TextBox ID="msgBuffer" runat="server" Width="99%" ReadOnly=true Height="472px"
TextMode=MultiLine style="OVERFLOW-X:hidden OVERFLOW-Y:auto"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ClearRecord" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
这是我的一点代码,UpdatePanel里边就放了一个TextBox.现在我就想让它如果出现垂直滚动条后,就显示在最后一行。默认的是回到第一行。
<ContentTemplate>
<asp:TextBox ID="msgBuffer" runat="server" Width="99%" ReadOnly=true Height="472px"
TextMode=MultiLine style="OVERFLOW-X:hidden OVERFLOW-Y:auto"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ClearRecord" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
这是我的一点代码,UpdatePanel里边就放了一个TextBox.现在我就想让它如果出现垂直滚动条后,就显示在最后一行。默认的是回到第一行。
* 『送』千元开发板,嵌入式开发热招!
* 构建出色多核应用程序
* CSDN网友快速赚分技巧
* IBM dW精华技术,开发者必看
* 走进嵌入式学院,就业世界500强
* 财富两千强公司都选甲骨文SOA解决方案
* CUDA—大规模并行计算的利器 </asp:TextBox>
<script type="text/javascript">
window.onload = function() {
var txt = document.getElementById("TextBox1");
txt.scrollTop = txt.scrollHeight; // IE Only
}
</script>
document.body.focus(); // 加上这个
txt.scrollTop = txt.scrollHeight; // IE Only
protected void SendMsg_Click(object sender, EventArgs e)
{
string record = String.Empty;
if (String.IsNullOrEmpty(msgText.Text))
{
return;
}
record = uName+" " +DropDownList2.SelectedValue.ToString()+ " 说:" + DateTime.Now.ToString() + "\n" + msgText.Text;
Application["chatcontent"] += "\n"; Application["chatcontent"] += record; msgBuffer.Text = Application["chatcontent"].ToString();
//发送信息的文本框清空
msgText.Text = String.Empty;
//更新聊天记录的TextBox
ChatBufferPanel.Update();
}
点击“发送”就会更新msgBuffer(聊天记录文本框),加上6楼的代码只能初始化页面的时候达到效果。从新更新就不行了。
<script type="text/javascript">
function SetScroll(){
setTimeout(function(){document.getElementById("msgBuffer").scrollTop=document.getElementById("msgBuffer").scrollHeight;},50);
}
function addLoadEvent(func){
var oldonload=window.onload;
if(typeof window.onload!='function'){
window.onload=func;
}else{
window.onload=function(){
oldonload();
func();
}
}
}addLoadEvent(SetScroll);
</script>
加载页面的时候可以做过到最下边Ajax没有测试,UpdatePanel更新的时候是不是可以调用一下SetScroll,这个也应该有办法或者楼主写个回调,自己实现异步更新,应该也不难
document.body.focus(); // 加上这个
txt.scrollTop = txt.scrollHeight; // IE,FF,这句就是滚动
document.body.focus(); // 加上这个
txt.scrollTop = txt.scrollHeight;
正确的
msgBuffer.Attributes.Add("onclick","SetScroll()");还是自己用js写个回调更好一些,网上资料也挺多
http://blog.darkthread.net/blogs/darkthreadtw/archive/2007/12/08/updatepanel-call-javascript-sample.aspx关于updatepanel中注册执行javascript
http://www.cnblogs.com/microant/archive/2007/08/23/866158.html
window.onload = function() {
var txt = document.getElementById("msgBuffer");
document.body.focus();
txt.scrollTop = txt.scrollHeight;
}
</script>
不管什么事件触发,都会重新调用页面的Page_Load()事件,能在每次加载页面的时候都执行上面的一段脚本的话,我想应该就可以达到文本框的垂直滚动条一直在下面的效果了吧。
关键是如何执行这段脚本,请大家帮忙说下解决方法。
ScriptManager.RegisterStartupScript(ChatBufferPanel, GetType(), "Refresh",
"javascript:document.getElementById('msgBuffer').scrollTop=document.getElementById('msgBuffer').scrollHeight;", true);
这样写在网页的CS文件的Page_Load()方法中就可以解决了。