点击这里给我发消息 点击这里给我发消息

动态创建Rating控件不能保存ViewState的解决方案

添加时间:2010-1-5
    相关阅读: ASP ASP.NET 解决方案 方案 HTML JavaScript C#
前言:

  Rating控件用来标识分级,目前已经有广泛应用。AJAX Control Toolkit中的Rating也做的相当成熟,也算是ASP.NET AJAX扩展中做的最像传统服务端控件的一个了。然而,最近很多朋友抱怨说如果在后台代码中动态创建Rating控件,那么在PostBack发生后,Rating的状态将会丢失(未存入ViewState)。本文介绍两种方法来解决此问题。

  问题重现:

  如果按以下代码动态创建Rating控件,则点击Button后,Rating选取的值将会丢失:

ASP.NET AJAX Advance Tips & Tricks (2) 动态创建Rating控件不能保存ViewState的解决方案

<%@PageLanguage="C#"%>
<%@RegisterAssembly="AjaxControlToolkit"Namespace="AjaxControlToolkit"TagPrefix="cc1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
  protectedvoidPage_Load(objectsender,EventArgse)
  {
    AjaxControlToolkit.Ratingrt=newRating();
    Page.Form.Controls.Add(rt);
    rt.ID="LikeRating";
    rt.MaxRating=5;
    rt.CurrentRating=2;
    rt.StarCssClass="ratingStar";
    rt.WaitingStarCssClass="savedRatingStar";
    rt.FilledStarCssClass="filledRatingStar";
    rt.EmptyStarCssClass="emptyRatingStar";
  }
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
  <title></title>
  <styletype="text/css">
    /*Rating*/.ratingStar
    {
      font-size:0pt;
      width:13px;
      height:12px;
      margin:0px;
      padding:0px;
      cursor:pointer;
      display:block;
      background-repeat:no-repeat;
    }
    .filledRatingStar
    {
      background-image:url(Images/FilledStar.png);
    }
    .emptyRatingStar
    {
      background-image:url(Images/EmptyStar.png);
    }
    .savedRatingStar
    {
      background-image:url(Images/SavedStar.png);
    }
  </style>
</head>
<body>
  <formid="form1"runat="server">
  <cc1:ToolkitScriptManagerID="ToolkitScriptManager1"runat="server">
  </cc1:ToolkitScriptManager>
  <div>
  </div>
  <asp:ButtonID="Button1"runat="server"Text="Button"/>
  </form>
  <p>
    &nbsp;</p>
</body>
</html>

原因:

  这个问题是由于动态创建的Rating控件不同于在Page_Load动态创建的其它服务端控件(如TextBox等),不会保存ViewState,要想为其创建ViewState,必须在Page_Init中进行创建。

  解决方案1:

  在Page_Init中创建控件,如下代码所示:

  解决方案1

<%@PageLanguage="C#"%>
<%@RegisterAssembly="AjaxControlToolkit"Namespace="AjaxControlToolkit"TagPrefix="cc1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
  protectedvoidPage_Init(objectsender,EventArgse)
  {
    AjaxControlToolkit.Ratingrt=newRating();
    Page.Form.Controls.Add(rt);
    rt.ID="LikeRating";
    rt.MaxRating=5;
    rt.CurrentRating=2;
    rt.StarCssClass="ratingStar";
    rt.WaitingStarCssClass="savedRatingStar";
    rt.FilledStarCssClass="filledRatingStar";
    rt.EmptyStarCssClass="emptyRatingStar";
  }
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
  <title></title>
  <styletype="text/css">
    /*Rating*/.ratingStar
    {
      font-size:0pt;
      width:13px;
      height:12px;
      margin:0px;
      padding:0px;
      cursor:pointer;
      display:block;
      background-repeat:no-repeat;
    }
    .filledRatingStar
    {
      background-image:url(Images/FilledStar.png);
    }
    .emptyRatingStar
    {
      background-image:url(Images/EmptyStar.png);
    }
    .savedRatingStar
    {
      background-image:url(Images/SavedStar.png);
    }
  </style>
</head>
<body>
  <formid="form1"runat="server">
  <cc1:ToolkitScriptManagerID="ToolkitScriptManager1"runat="server">
  </cc1:ToolkitScriptManager>
  <div>
  </div>
  <asp:ButtonID="Button1"runat="server"Text="Button"/>
  </form>
  <p>
    &nbsp;</p>
</body>
</html>

解决方案2:

  使用Hidden Field来保存Rating控件的状态:

  注意:要设置Rating控件的BehaviorID,以便在JavaScript中调用Rating控件的js方法

  解决方案2

<%@PageLanguage="C#"%>
<%@RegisterAssembly="AjaxControlToolkit"Namespace="AjaxControlToolkit"TagPrefix="cc1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
  protectedvoidPage_Load(objectsender,EventArgse)
  {
    AjaxControlToolkit.Ratingrt=newRating();
    Panel1.Controls.Add(rt);
    rt.ID="LikeRating";
    rt.MaxRating=5;
    rt.CurrentRating=2;
    rt.StarCssClass="ratingStar";
    rt.WaitingStarCssClass="savedRatingStar";
    rt.FilledStarCssClass="filledRatingStar";
    rt.EmptyStarCssClass="emptyRatingStar";
    rt.BehaviorID="hichange";
    rt.CurrentRating=Convert.ToInt32(this.Hidden1.Value);
  }
  
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
  <title></title>
  <styletype="text/css">
    /*Rating*/.ratingStar
    {
      font-size:0pt;
      width:13px;
      height:12px;
      margin:0px;
      padding:0px;
      cursor:pointer;
      display:block;
      background-repeat:no-repeat;
    }
    .filledRatingStar
    {
      background-image:url(Images/FilledStar.png);
    }
    .emptyRatingStar
    {
      background-image:url(Images/EmptyStar.png);
    }
    .savedRatingStar
    {
      background-image:url(Images/SavedStar.png);
    }
  </style>
  
  <scripttype="text/javascript">
    functionsave_rate(){
      varstate=$find("hichange").get_Rating();
      $get("Hidden1").value=state;
      alert(state);
    }
  
  </script>
</head>
<body>
  <formid="form1"runat="server">
  <cc1:ToolkitScriptManagerID="ToolkitScriptManager1"runat="server">
  </cc1:ToolkitScriptManager>
  <asp:PanelID="Panel1"runat="server"onclick="save_rate();">
  </asp:Panel>
  <asp:ButtonID="Button1"runat="server"Text="Button"/>
  <inputid="Hidden1"runat="server"type="hidden"value="1"/>
  <p>
    &nbsp;</p>
  </form>
  </body>
</html>

本文作者:未知
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册