找回密码
 立即注册

QQ登录

只需一步,快速开始

ZenosZeng 讲师达人认证 悬赏达人认证
超级版主   /  发表于:2012-7-3 16:52  /   查看:7850  /  回复:2
在实际产品设计过程中,我们经常会讨论产品给用户带来的使用体验,我们希望产品能给用户带来最完美的体验。完美的用户体验很大程度上受到产品易用性的影响,比如:是否能给提供便捷的数据录入方式。本文主要讲解在Spread  for ASP.NET中,如何通过自定义单元格类型来提升产品的用户使用体验。

本文通过自定义单元格类型要实现以下功能:
1、单元格处于非编辑状态时,只显示文字
2、单元格处于编辑状态时,单元格中显示TextBox和Button控件
3、编辑状态下,用户可以直接在TextBox中输入数据
4、编辑状态下,用户点击Button控件之后弹出一个新的窗体,用户在新的窗体中选择数据后,将选择的数据返回给单元格

我们先来看一下最终的运行效果:
PopupCellType.png

实现方法是通过自定义CellType与ModalPopupExtender一起完成该功能。
详细实现步骤如下:
1. 实现自定义单元格类型
    1.1. 创建一个继承于GeneralCellType的自定义单元格类型 PopupCellType

      1.2. 重写GetEditorControl,返回由TextBox和Button组成的控件
  1.     public override System.Web.UI.Control GetEditorControl(string id, System.Web.UI.WebControls.TableCell parent, FarPoint.Web.Spread.Appearance style, FarPoint.Web.Spread.Inset margin, object value, bool upperLevel)
  2.     {
  3.         Table table = new Table();
  4.         table.CellPadding = 0;
  5.         table.CellSpacing = 0;
  6.         table.BorderStyle = BorderStyle.None;
  7.         table.BorderWidth = new Unit(0, UnitType.Pixel);
  8.         table.Width = new Unit(100, UnitType.Percentage);

  9.         TableRow row = new TableRow();

  10.         TableCell cell = new TableCell();
  11.         cell.VerticalAlign = VerticalAlign.Middle;
  12.         cell.HorizontalAlign = HorizontalAlign.Left;

  13.         // 用户可以直接在 TextBox 中输入数据
  14.         TextBox tb = new TextBox();
  15.         tb.Width = new Unit(99, UnitType.Percentage);
  16.         tb.ID = "PopupEditor" + _id;
  17.         // 如果不希望用户直接输入数据,可以将TextBox设置为ReadOnly,这样就可以保证数据的有效性
  18.         //tb.ReadOnly = true;
  19.         if (!string.IsNullOrEmpty(_onEnterPopup))
  20.         {
  21.             tb.Attributes.Add("onkeydown", _onEnterPopup);
  22.         }
  23.         cell.Controls.Add(tb);
  24.         row.Cells.Add(cell);

  25.         cell = new TableCell();
  26.         cell.Width = new Unit(22, UnitType.Pixel);
  27.         cell.VerticalAlign = VerticalAlign.Middle;
  28.         cell.HorizontalAlign = HorizontalAlign.Right;

  29.         // 如果用户不想通过输入的方式填写数据,可以点击[检索]按钮,在弹出的窗体中选择需要的数据
  30.         ImageButton img = new ImageButton();
  31.         img.ImageUrl = @"Images/Search.jpg";
  32.         img.BorderColor = System.Drawing.Color.LightGray;
  33.         img.BorderStyle = BorderStyle.Solid;
  34.         img.BorderWidth = new Unit(1, UnitType.Pixel);
  35.         if (!string.IsNullOrEmpty(_onClickPopup))
  36.         {
  37.             img.Attributes.Add("onclick", _onClickPopup);
  38.         }            
  39.         cell.Controls.Add(img);
  40.         row.Cells.Add(cell);

  41.         table.Rows.Add(row);

  42.         return table;
  43.     }
复制代码


    1.3. 重写EditorClientScriptUrl属性
  1.     public override string EditorClientScriptUrl
  2.     {
  3.         get
  4.         {
  5.             return "PopupEditorScript.htc";
  6.         }
  7.     }
复制代码


    1.4. 创建PopupEditorScript.htc文件
  1. <!--
  2. Copyright&#169; 2001. FarPoint Technologies. All rights reserved.
  3. -->
  4. <public:component>
  5. <PUBLIC:PROPERTY NAME="value">
  6. <get internalName="getValue"/>
  7. <put internalName="setValue"/>
  8. </PUBLIC:PROPERTY>
  9. <PUBLIC:METHOD NAME="isValid">
  10. </PUBLIC:METHOD>
  11. </public:component>
  12. <script language="javascript">
  13.     function getValue() {
  14.         if (element.all[3].value != null) {
  15.             return element.all[3].value;
  16.         } return "";
  17.     }
  18.     function setValue(val) {
  19.         element.all[3].value = val;
  20.     }
  21.     function isValid(val) {
  22.         return "";
  23.     }

  24.     //-->
  25.     </script>
复制代码


2.在页面中添加FpSpread和ModalPopupExtender控件
    2.1. 在页面中设置ModalPopupExtender控件的属性
  1.     <asp:Button ID="ButtonEdit" runat="server" Text="Edit Expanse" Style="display: none" />
  2.     <cc1:ModalPopupExtender ID="ModalPopupExtender1" BackgroundCssClass="ModalPopupBG"
  3.         runat="server" TargetControlID="ButtonEdit" PopupControlID="DivSearchWindow"
  4.         BehaviorID="SearchModalPopup">
  5.     </cc1:ModalPopupExtender>
  6.     <div id="DivSearchWindow" style="display: none; width: 615px; height: 400px;" class="popupConfirmation">
  7.         <iframe id="IframeSearch" style="width: 100%; height: 100%;" frameborder="0" scrolling="no">
  8.         </iframe>
  9.     </div>
复制代码


    2.2. 在Spread中使用PopupCellType
  1.     PopupCellType popup = new PopupCellType();

  2.     popup.ID = "1001";
  3.     popup.OnClickPopup = "return ShowPopup();";
  4.     popup.OnEnterPopup = "return EnterPress(event);";
  5.     this.FpSpread1.ActiveSheetView.Columns[0].CellType = popup;
复制代码


3. 创建弹出窗体中显示的页面

源码下载:VS2010 + Spread 6.0.3505 + IE9
PopupCellTypeDemo_Modal.zip (1.76 MB, 下载次数: 1138)

2 个回复

倒序浏览
wzw
论坛元老   /  发表于:2014-4-8 10:09:00
沙发
这个功能真的很好,可惜在Firefox或谷歌浏览器下不能用,不能取返回的值。能解决吗?很期待
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2014-4-8 16:04:00
板凳
回复 2楼wzw的帖子

请参考:点击进入
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部