日期:2014-05-17  浏览次数:20549 次

一个简单的跳转问题,在线等
一个页面上有多个radiobutton,某一个被选中时,页面下面将显示隐藏的panel,现在想实现点击radiobutton时,页面跳转到所显示的panel上面,就是将panel在页面中间。
因为页面内容较多,如果没有跳转,只能通过下拉才能看到新显示出来的panel的内容。

------解决方案--------------------
实现方法很多,就看你的需求了‘下面是一个例子
HTML code
<%@ Page Language="C#" Debug="true" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
  }

  protected void RadioButtonX_CheckedChanged(object sender, EventArgs e)
  {
    RadioButton r = sender as RadioButton;
    if (r.ID == "RadioButton1")
    {
      Panel2.Visible = false;
      Panel1.Visible = true;
      Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "scrollTo('" + Panel1.ClientID + "')", true);
    }
    else if (r.ID == "RadioButton2")
    {
      Panel1.Visible = false;
      Panel2.Visible = true;
      Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "scrollTo('" + Panel2.ClientID + "')", true);
    }
  }
    

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <script type="text/javascript">
    function scrollTo(divid) {
      d = document.getElementById(divid);
      if (document.documentElement)
        document.documentElement.scrollTop = d.offsetTop;
      else
        document.body.scrollTop = d.offsetTop;
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <asp:RadioButton ID="RadioButton1" AutoPostBack="true" runat="server" GroupName="A" OnCheckedChanged="RadioButtonX_CheckedChanged" />
  <asp:RadioButton ID="RadioButton2" AutoPostBack="true" runat="server" GroupName="A" OnCheckedChanged="RadioButtonX_CheckedChanged" />
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <p>
    x</p>
  <asp:Panel ID="Panel1" runat="server" Visible="false">
    <div style="border: 2px solid red; height: 200px">内容11111111111111</div>
  </asp:Panel>
  <asp:Panel ID="Panel2" runat="server" Visible="false">
    <div style="border: 2px solid red; height: 200px">内容222222222222</div>
  </asp:Panel>
  </form>
</body>
</html>