我要做一个 有两个按钮 按BtnA 出来一个Panel 按BtnB 出来另一个Panel 这两个Panel都是隐藏的 按相应的Btn就出来 做成客户端的 在page_load里边写如下代码 
this.BtnA.Attributes.Add("onclick", "document.form1.UpdatePanel2.PanelA.Visible=true;"); 运行,出现javascript错误,说document.form1.UpdatePanel2.PanelA.Visible为空或不是对象。 为什么? 怎么解决?

解决方案 »

  1.   

    第一,不要在服务器端设置visible=false,这样会导致js找不到控件
    第二,给两个panel客户端id,然后使用document.getelementbyid找到panel,修改style.display=""(显示)或style.display="none"(隐藏)
      

  2.   

    BtnA.Attributes.Add("onclick", "document.getElementById('<%= PanelA.ClientID%>').style.display='block'");
    this.BtnA.Attributes.Add("onclick", "document.form1."+UpdatePanel2.ClientID+".PanelA.Visible=true;"); 
      

  3.   

    webform不是这么做的
    Visible=false的控件是不会加载到页面中的
    要用ajax局部刷新的话,可以用jquery
    前台页面 Default.aspx :<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Text_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>JSON练习</title>
        <script src="../components/jquery/jquery-1[1].3.2.min.js" type="text/javascript"></script> <!--引用 jquery类库-->
    <script type="text/javascript">
    $(function()
    {
       $("#bt1").click(function()
       {
         $.ajax({
                   url: "Default.aspx?id=1",
                   type: "get",
                   dataType: 'json',
                   cache: false,
                  beforeSend: function(result){
                      },
                  success: function(result){
                        var html='';     
             $(result).each(function(i)            //解析方法1
              {
                 html+=i+"---"+this.name+":"+this.value;
                 alert(i+"---"+this.name+":"+this.value);
              });
              alert(html);
                  },
                  error: function(result, status) {
                    if (status == 'error') {
                      alert("系统发生错误");
                    }
                   }
               });
         });
       $("#bt2").click(function()
       {
         $.ajax({
                   url: "Default.aspx?pid=2",
                   type: "get",
                   dataType: 'json',
                   cache: false,
                  beforeSend: function(result){
                      },
                  success: function(result){         
              alert(result.name);
              alert(result.value);
              alert(result.name+":"+result.value);
                  },
                  error: function(result, status) {
                    if (status == 'error') {
                      alert("系统发生错误");
                    }
                   }
               });
         });
       $("#bt3").click(function()
       {
         $.ajax({
                   url: "Default.aspx?id=1",
                   type: "get",
                   dataType: 'json',
                   cache: false,
                  beforeSend: function(result){
                      },
                  success: function(result){
                        var html='';     
             $.each(result,function(i,comment)     //解析方法2
              {
                 html+=i+"---"+comment['name']+":"+comment['value'];
                 alert(i+"---"+comment['name']+":"+comment['value']);
              });
              alert(html);
                  },
                  error: function(result, status) {
                    if (status == 'error') {
                      alert("系统发生错误");
                    }
                   }
               });
         });
    });
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="bt1" type="button" value="点击返回json集合" />
            <input id="bt2" type="button" value="点击返回一个json字符串" />
            <input id="bt3" type="button" value="点击返回json集合(解析方法不一样)" />
        </div>
        </form>
    </body>
    </html>后台代码 Default.aspx.cs :using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text;
    using System.Data.SqlClient;public partial class Text_Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    StringBuilder str = new StringBuilder();
                    for (int i = 0; i < 4; i++)
                    {
                        str.AppendFormat("{{name:\"{0}\",value:\"{1}\"}},", "ssss" + i.ToString(), i.ToString());
                    }
                    Response.Write("[" + str.ToString().TrimEnd(',') + "]"); //去掉最后的 ','返回一个json字符串
                    Response.End();
                }
                if (Request.QueryString["pid"] != null)
                {
                    StringBuilder str = new StringBuilder();
                    str.AppendFormat("{{name:\"{0}\",value:\"{1}\"}}", "ssss5", "5");
                    Response.Write(str.ToString());
                    Response.End();
                }
                JScript.Alert(tt1.Value);
            }
        }
        //连接数据库。用数据库的内容填充。
        //private string GetDepotjson()
        //{
        //    StringBuilder jsonstr = new StringBuilder();    //    SqlDataReader dr = bd.GetList("");   //读取数据库,返回SqlDataReader
        //    while (dr.Read())
        //    {
        //        jsonstr.AppendFormat("{{name:\"{0}\",value:\"{1}\"}},", dr["Tname"], dr["TID"]);
        //    }
        //    dr.Close();                         //关闭
        //    return "[" + jsonstr.ToString().TrimEnd(',') + "]"; //返回一个json字符串
        //}   
    }
      

  4.   

    if (!Panel1.Visible)
            {
                Panel1.Visible = true;
                UpdatePanel1.Update();
            }
            else if (!Panel2.Visible)
            {
                Panel2.Visible = true;
                UpdatePanel2.Update();
            }