码迷,mamicode.com
首页 > Web开发 > 详细

ASP.net 自定义控件GridView

时间:2014-09-24 18:37:07      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

bubuko.com,布布扣
   1 using System;
   2 using System.Web.UI;
   3 using System.Web.UI.WebControls;
   4 using System.Web.UI.HtmlControls;
   5 using System.Collections;
   6 using System.Collections.Generic;
   7 using System.Text;
   8 using System.Web.UI.Design;
   9 using System.ComponentModel;
  10 using SILK2010;
  11 using System.Reflection;
  12 using System.Web;
  13 
  14 
  15 
  16 namespace FrameWorkV4.Controls
  17 {
  18     public class SMQGridView : System.Web.UI.WebControls.GridView
  19     {
  20         #region Private Property
  21         private int dataCout;
  22         private int DataCount
  23         {
  24             get
  25             {
  26                 if (dataCout == 0 && DataSource != null)
  27                 {
  28                     if (DataSource is IEnumerable)
  29                     {
  30                         IEnumerable DataSourceList = (IEnumerable)DataSource;
  31                         IEnumerator enumerator = DataSourceList.GetEnumerator();
  32                         enumerator.Reset();
  33                         while (enumerator.MoveNext())
  34                         {
  35                             dataCout++;
  36                         }
  37                     }
  38                     if (DataSource is IListSource)
  39                     {
  40                         IListSource DataSourceList = (IListSource)DataSource;
  41                         IList list = DataSourceList.GetList();
  42                         dataCout = list.Count;
  43                     }
  44                 }
  45                 return dataCout;
  46             }
  47         }
  48         private int FirstPageIndex
  49         {
  50             get
  51             {
  52                 return 0;
  53             }
  54         }
  55         private int PreviosPageIndex
  56         {
  57             get
  58             {
  59                 return Math.Max(PageIndex - 1, 0);
  60             }
  61         }
  62         private int NextPageIndex
  63         {
  64             get
  65             {
  66                 return Math.Min(PageIndex + 1, PageCount - 1);
  67             }
  68         }
  69         private int LastPageIndex
  70         {
  71             get
  72             {
  73                 return PageCount - 1;
  74             }
  75         }
  76         //Added by tony at 2010.12.16
  77         private int TotalDataCount
  78         {
  79             get
  80             {
  81                 object o = ViewState["TotalDataCount"];
  82                 int _dataCout = 0;
  83                 if (o != null)
  84                 {
  85                     _dataCout = int.Parse(o.ToString());
  86                 }
  87                 return _dataCout;
  88             }
  89             set
  90             {
  91                 ViewState["TotalDataCount"] = value;
  92             }
  93         }
  94         #endregion
  95 
  96         #region Public Property
  97         public string OrderByField
  98         {
  99             get
 100             {
 101                 object o = ViewState["OrderByField"];
 102                 if (o == null)
 103                 {
 104                     for (int i = 0; i < Columns.Count; i++)
 105                     {
 106                         if (Columns[i].SortExpression.Trim() != string.Empty)
 107                             return Columns[i].SortExpression;
 108                     }
 109                     return string.Empty;
 110                 }
 111                 return (string)o;
 112             }
 113             set
 114             {
 115                 ViewState["OrderByField"] = value;
 116             }
 117         }
 118         public bool OrderByAsc
 119         {
 120             get
 121             {
 122                 object o = ViewState["OrderByAsc"];
 123                 if (o == null)
 124                 {
 125                     return true;
 126                 }
 127                 return (bool)o;
 128             }
 129             set
 130             {
 131                 ViewState["OrderByAsc"] = value;
 132             }
 133         }
 134         public string EntityType;
 135         public string PrimaryKeyName;
 136         public string GridViewName;
 137         private bool showSaveChangesButton
 138         {
 139             get
 140             {
 141                 foreach (DataControlField field in Columns)
 142                 {
 143                     if (field is SMQGridViewField)
 144                         return true;
 145                 }
 146                 return false;
 147             }
 148         }
 149         #endregion
 150 
 151 
 152         private bool _isSaveChange = true;
 153         public bool _IsSaveChange
 154         {
 155             get
 156             {
 157                 return _isSaveChange;
 158             }
 159             set
 160             {
 161                 _isSaveChange = value;
 162             }
 163 
 164         }
 165 
 166 
 167         //Added by tony at 2010.12.16
 168         public override object DataSource
 169         {
 170             get
 171             {
 172                 return base.DataSource;
 173             }
 174             set
 175             {
 176                 base.DataSource = value;
 177                 int _dataCout = 0;
 178                 if (value is IEnumerable)
 179                 {
 180                     IEnumerable DataSourceList = (IEnumerable)value;
 181                     IEnumerator enumerator = DataSourceList.GetEnumerator();
 182                     enumerator.Reset();
 183                     while (enumerator.MoveNext())
 184                     {
 185                         _dataCout++;
 186                     }
 187                 }
 188                 if (value is IListSource)
 189                 {
 190                     IListSource DataSourceList = (IListSource)DataSource;
 191                     IList list = DataSourceList.GetList();
 192                     _dataCout = list.Count;
 193                 }
 194                 TotalDataCount = _dataCout;
 195             }
 196         }
 197 
 198         private bool _isAllowSaveChange = false;
 199 
 200 
 201 
 202         public bool IsAllowSaveChange
 203         {
 204             get { return _isAllowSaveChange; }
 205             set { _isAllowSaveChange = value; }
 206         }
 207 
 208 
 209         private bool _alwaysShowSaveChange = true;
 210 
 211         public bool AlwaysShowSaveChange
 212         {
 213             get { return _alwaysShowSaveChange; }
 214             set { _alwaysShowSaveChange = value; }
 215         }
 216         private bool _isAllowAllEdit = false;
 217 
 218         public bool IsAllowAllEdit
 219         {
 220             get { return _isAllowAllEdit; }
 221             set { _isAllowAllEdit = value; }
 222         }
 223 
 224         private int _gv_ProgramProductId;
 225         public int GV_ProgramProductId
 226         {
 227             get { return _gv_ProgramProductId; }
 228             set { _gv_ProgramProductId = value; }
 229         }
 230 
 231         private bool _isProductFeature = false;
 232         public bool IsProductFeature
 233         {
 234             get { return _isProductFeature; }
 235             set { _isProductFeature = value; }
 236         }
 237 
 238         private bool _isSweepCart = false;
 239         public bool IsSweepCart
 240         {
 241             get { return _isSweepCart; }
 242             set { _isSweepCart = value; }
 243         }
 244 
 245         private bool _isprogramproductordertype = false;
 246         public bool IsProgramProductOrderType
 247         {
 248             get
 249             {
 250                 return _isprogramproductordertype;
 251             }
 252             set
 253             {
 254                 _isprogramproductordertype = value;
 255             }
 256         }
 257 
 258         private bool _isprogramproductcustomertype = false;
 259 
 260         public bool IsProgramProductCustomerType
 261         {
 262             get
 263             {
 264                 return _isprogramproductcustomertype;
 265             }
 266             set
 267             {
 268                 _isprogramproductcustomertype = value;
 269             }
 270         }
 271 
 272         private bool _isReducePrice = false;
 273         public bool IsReducePrice
 274         {
 275             get { return _isReducePrice; }
 276             set { _isReducePrice = value; }
 277         }
 278 
 279         private int _programproductid = 0;
 280         public int ProgramProductId
 281         {
 282             get
 283             {
 284                 return _programproductid;
 285             }
 286             set
 287             {
 288                 _programproductid = value;
 289             }
 290         }
 291 
 292         private bool _isAllowPage = true;
 293         public bool IsAllowPage
 294         {
 295             get { return _isAllowPage; }
 296             set { _isAllowPage = value; }
 297         }
 298 
 299 
 300         private bool _updateppstatus = false;
 301         public bool UpdatePPStatus
 302         {
 303             get
 304             {
 305                 return _updateppstatus;
 306             }
 307             set
 308             {
 309                 _updateppstatus = value;
 310             }
 311         }
 312 
 313 
 314         private bool bHasRecord
 315         {
 316             get
 317             {
 318                 try
 319                 {
 320                     return bool.Parse(ViewState["bHasRecord"].ToString());
 321                 }
 322                 catch
 323                 {
 324                     return false;
 325                 }
 326             }
 327             set
 328             {
 329                 ViewState["bHasRecord"] = value.ToString();
 330             }
 331         }       
 332 
 333         protected override void OnDataBinding(EventArgs e)
 334         {
 335             if (DataSource == null)
 336             {
 337                 base.OnDataBinding(e);
 338                 return;
 339             }
 340 
 341             var aa = DataSource as List<SILK2010.Entity>;
 342             if ((DataSource is SILK2010.EntityList) == false)
 343             {
 344                 base.OnDataBinding(e);
 345                 return;
 346             }
 347 
 348 
 349             SILK2010.EntityList ds = DataSource as SILK2010.EntityList;
 350             if (!string.IsNullOrEmpty(OrderByField))
 351             {
 352                 ds.Sort(OrderByField, OrderByAsc);
 353             }
 354             if (!string.IsNullOrEmpty(PrimaryKeyName))
 355             {
 356                 this.DataKeyNames = new string[1] { PrimaryKeyName };
 357             }
 358             if (!_isAllowPage)
 359             {
 360                 this.AllowPaging = false;
 361             }
 362             else
 363             {
 364                 this.AllowPaging = true;
 365             }
 366 
 367             if (ds.Count == 0)
 368             {
 369                 if (string.IsNullOrEmpty(this.EntityType))
 370                 {
 371                     return;
 372                 }
 373 
 374                 Assembly asmb = Assembly.LoadFrom(HttpContext.Current.Server.MapPath("~/Bin/ProTexusModel.dll"));
 375                 Entity entity = System.Activator.CreateInstance(asmb.GetType("ProTexus.Model." + EntityType)) as Entity;
 376 
 377                 ds.Add(entity);
 378 
 379                 this.DataSource = ds;
 380 
 381                 this.bHasRecord = false;
 382             }
 383             else
 384             {
 385                 this.bHasRecord = true;
 386             }
 387             base.OnDataBinding(e);
 388         }
 389 
 390         private void SetAdvancedPage(GridViewRowEventArgs e)
 391         {
 392             HtmlGenericControl prolistbottom = new HtmlGenericControl("DIV");
 393             e.Row.Cells[0].Controls.Add(prolistbottom);
 394             prolistbottom.Attributes.Add("class", "prolistbottom textindent10");
 395             HtmlGenericControl floatleft = new HtmlGenericControl("DIV");
 396             floatleft.Attributes.Add("class", "floatleft fontwhite");
 397             prolistbottom.Controls.Add(floatleft);
 398             HtmlGenericControl pagesort = new HtmlGenericControl("DIV");
 399             floatleft.Controls.Add(pagesort);
 400             pagesort.Attributes.Add("class", "pagesort");
 401 
 402             LiteralControl Pages = new LiteralControl();
 403             Pages.Text = "<b>Page(s):</>"; 
 404             pagesort.Controls.Add(Pages);
 405 
 406             LiteralControl _space = new LiteralControl();
 407 
 408             //Build "First" and "Previous" button cell.
 409             Button _btnFirst = new Button();
 410             _btnFirst.Text = "First";
 411             _btnFirst.CssClass = "button";
 412             _btnFirst.CommandName = "Pager";
 413             _btnFirst.CommandArgument = FirstPageIndex.ToString();
 414             pagesort.Controls.Add(_btnFirst);
 415 
 416             Button _btnPrevious = new Button();
 417             _btnPrevious.Text = "Previous";
 418             _btnPrevious.CssClass = "button";
 419             _btnPrevious.CommandName = "Pager";
 420             _btnPrevious.CommandArgument = PreviosPageIndex.ToString();
 421             pagesort.Controls.Add(_btnPrevious);
 422 
 423             //Build Page list button.
 424             for (int i = 0; i < PageCount; i++)
 425             {
 426                 if (i > PageIndex - 10 && i < PageIndex + 10)
 427                 {
 428                     LinkButton _pageLink = new LinkButton();
 429                     int cur = i + 1;
 430                     if (PageIndex == i)
 431                     {
 432                         _pageLink.Text = "<b><font size=4>" + cur.ToString() + "</font></>";
 433                     }
 434                     else
 435                     {
 436                         _pageLink.Text = "" + cur.ToString() + "";
 437                     }
 438                     _pageLink.CommandName = "Pager";
 439                     _pageLink.CommandArgument = i.ToString();
 440                     pagesort.Controls.Add(_pageLink);
 441 
 442                     if (PageIndex == i)
 443                     {
 444                         _pageLink.Enabled = false;
 445                     }
 446 
 447                     _space = new LiteralControl();
 448                     _space.Text = "&nbsp;";
 449                     pagesort.Controls.Add(_space);
 450                 }
 451             }
 452 
 453             //Build "Next" and "Last" button cell.
 454             _space = new LiteralControl();
 455             _space.Text = "&nbsp;";
 456 
 457             Button _btnNext = new Button();
 458             _btnNext.Text = "Next";
 459             _btnNext.CssClass = "button";
 460             _btnNext.CommandName = "Pager";
 461             _btnNext.CommandArgument = NextPageIndex.ToString();
 462             pagesort.Controls.Add(_btnNext);
 463             pagesort.Controls.Add(_space);
 464 
 465             Button _btnLast = new Button();
 466             _btnLast.Text = "Last";
 467             _btnLast.CssClass = "button";
 468             _btnLast.CommandName = "Pager";
 469             _btnLast.CommandArgument = LastPageIndex.ToString();
 470             pagesort.Controls.Add(_btnLast);
 471             pagesort.Controls.Add(_space);
 472 
 473             //Build "Showing Result:" cell.
 474             LiteralControl PageResult = new LiteralControl();
 475             int _currentPageBeginNo = 0;
 476             int _currentPageEndNo = 0;
 477 
 478             //if (DataCount != 0)
 479             //{
 480             //  _currentPageBeginNo = PageIndex * PageSize + 1;
 481             //  _currentPageEndNo = _currentPageBeginNo + PageSize - 1;
 482 
 483             //  if (_currentPageEndNo > DataCount)
 484             //  {
 485             //    _currentPageEndNo = DataCount;
 486             //  }
 487             //}
 488 
 489             //PageResult.Text = "Showing Result:&nbsp;" + _currentPageBeginNo.ToString() + "-";
 490             //if (IsAllowPage)
 491             //{
 492             //    PageResult.Text += _currentPageEndNo.ToString();
 493             //}
 494             //else
 495             //{
 496             //    PageResult.Text += DataCount.ToString();
 497             //}
 498             //PageResult.Text += " of " + DataCount.ToString();
 499             if (TotalDataCount != 0)
 500             {
 501                 _currentPageBeginNo = PageIndex * PageSize + 1;
 502                 _currentPageEndNo = _currentPageBeginNo + PageSize - 1;
 503 
 504                 if (_currentPageEndNo > TotalDataCount)
 505                 {
 506                     _currentPageEndNo = TotalDataCount;
 507                 }
 508             }
 509 
 510             PageResult.Text = "Showing Result:&nbsp;" + _currentPageBeginNo.ToString() + "-";
 511             if (IsAllowPage)
 512             {
 513                 PageResult.Text += _currentPageEndNo.ToString();
 514             }
 515             else
 516             {
 517                 PageResult.Text += TotalDataCount.ToString();
 518             }
 519             PageResult.Text += " of " + TotalDataCount.ToString();
 520             pagesort.Controls.Add(PageResult);
 521 
 522             //Build Save Changes Button Cell
 523             HtmlGenericControl floatright = new HtmlGenericControl("DIV");
 524             floatright.Attributes.Add("class", "floatright");
 525             if (showSaveChangesButton && IsAllowSaveChange && AlwaysShowSaveChange)
 526             {
 527                 Button btn = new Button();
 528                 btn.CommandName = "SaveChanges";
 529                 btn.Text = "Save Changes";
 530                 btn.CssClass = "buttonBig";
 531                 floatright.Controls.Add(btn);
 532             }
 533 
 534             if (showSaveChangesButton && IsAllowAllEdit)
 535             {
 536                 Button btn = new Button();
 537                 btn.CommandName = "AllEdit";
 538                 btn.Text = "Edit All";
 539                 btn.CssClass = "buttonBig";
 540                 floatright.Controls.Add(btn);
 541             }
 542             prolistbottom.Controls.Add(floatright);
 543 
 544             //HtmlGenericControl floatclear = new HtmlGenericControl("DIV");
 545             //floatclear.Attributes.Add("class", "floatclear");
 546             //prolistbottom.Controls.Add(floatclear);
 547 
 548             if (PageIndex == 0)
 549             {
 550                 _btnFirst.Enabled = false;
 551                 _btnPrevious.Enabled = false;
 552             }
 553             if (PageIndex == PageCount - 1)
 554             {
 555                 _btnNext.Enabled = false;
 556                 _btnLast.Enabled = false;
 557             }
 558         }
 559 
 560         protected override void OnRowCreated(GridViewRowEventArgs e)
 561         {
 562             switch (e.Row.RowType)
 563             {
 564                 case DataControlRowType.Footer:
 565                     if (this.bHasRecord && Rows.Count != 0)
 566                     {
 567                         e.Row.Cells[0].ColumnSpan = Columns.Count;
 568                         for (int i = 0; i < Columns.Count - 1; i++)
 569                             e.Row.Cells.RemoveAt(1);
 570                         SetAdvancedPage(e);
 571                         PagerSettings.Visible = false;
 572                         ShowFooter = true;
 573                     }
 574                     break;
 575                 case DataControlRowType.Header:
 576                     #region Build Header
 577                     for (int i = 0; i < Columns.Count; i++)
 578                     {
 579                         TableCell cell = e.Row.Cells[i];
 580                         if (Columns[i].SortExpression.Trim().Length != 0)
 581                         {
 582                             LinkButton _headText = new LinkButton();
 583                             //_headText.CssClass = HeadCss;
 584                             _headText.Text = Columns[i].HeaderText;
 585                             _headText.ToolTip = "Sort this column";
 586                             _headText.CommandName = "Sort";
 587                             _headText.CommandArgument = Columns[i].SortExpression;
 588 
 589                             cell.Controls.Add(_headText);
 590 
 591                             if (OrderByField == Columns[i].SortExpression)
 592                             {
 593                                 ImageButton _headImage = new ImageButton();
 594                                 _headImage.CommandName = "Sort";
 595                                 _headImage.CommandArgument = Columns[i].SortExpression;
 596                                 _headImage.ToolTip = "Sort this column";
 597                                 if (OrderByAsc == true)
 598                                 {
 599                                     _headImage.ImageUrl = "~/Skin/PROTEXUS/Images/SortDirDESC.gif";
 600 
 601                                 }
 602                                 else
 603                                 {
 604                                     _headImage.ImageUrl = "~/Skin/PROTEXUS/Images/SortDirASC.gif";
 605                                 }
 606                                 cell.Controls.Add(_headImage);
 607                             }
 608 
 609                         }
 610                         else
 611                         {
 612                             LinkButton _headText = new LinkButton();
 613                             _headText.Text = Columns[i].HeaderText;
 614                             cell.Controls.Add(_headText);
 615                         }
 616 
 617 
 618                         if (Columns[i] is SMQGridViewField)
 619                         {
 620                             SMQGridViewField field = Columns[i] as SMQGridViewField;
 621                             if (field.CanEdit)
 622                             {
 623                                 if (!IsAllowSaveChange)
 624                                     IsAllowSaveChange = true;
 625 
 626                                 ImageButton _headEdit = new ImageButton();
 627                                 _headEdit.CommandName = "EditColumn";
 628                                 _headEdit.CommandArgument = field.DataField;
 629                                 _headEdit.ToolTip = "Edit this column";
 630                                 if (field.IsEditing)
 631                                 {
 632                                     _headEdit.ImageUrl = "~/Skin/PROTEXUS/Images/icon_edit.gif";
 633                                 }
 634                                 else
 635                                 {
 636                                     _headEdit.ImageUrl = "~/Skin/PROTEXUS/Images/icon_edit_f2.gif";
 637                                 }
 638                                 cell.Controls.Add(_headEdit);
 639                             }
 640                             if (field.fieldType == FieldType.Delete)
 641                             {
 642                                 Image img = new Image();
 643                                 img.ImageUrl = "~/Skin/PROTEXUS/Images/icon_del.gif";
 644                                 cell.Controls.Add(img);
 645                                 CheckBox cbx = new CheckBox();
 646                                 cell.Controls.Add(cbx);
 647                             }
 648                             if (cell.Controls.Count == 0)
 649                             {
 650                                 LiteralControl lbl = new LiteralControl();
 651                                 lbl.Text = field.HeaderText;
 652                                 cell.Controls.Add(lbl);
 653                             }
 654                         }
 655                     }
 656                     #endregion
 657                     break;
 658                 default:
 659                     if (this.bHasRecord)
 660                     {
 661                         e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#F2F2F2");
 662                         base.OnRowCreated(e);
 663                     }
 664                    
 665                     break;
 666             }
 667         }
 668         public void SetNoRecordRow(GridViewRowEventArgs e)
 669         {
 670             //if (!this.bHasRecord)
 671             //{
 672             //    for (int i = 0; i < e.Row.Cells.Count; i++)
 673             //    {
 674             //        e.Row.Cells[i].Text = "&nbsp;";
 675             //    }
 676             //}
 677         }
 678 
 679         protected override void OnRowCommand(GridViewCommandEventArgs e)
 680         {
 681             switch (e.CommandName)
 682             {               
 683                 case "Sort":
 684                     OrderByField = e.CommandArgument.ToString();
 685                     OrderByAsc = !OrderByAsc;
 686                     break;
 687                 case "Pager":
 688                     PageIndex = int.Parse(e.CommandArgument.ToString());
 689                     break;
 690                 case "EditColumn":
 691                     foreach (DataControlField field in Columns)
 692                     {
 693                         if (field is SMQGridViewField)
 694                         {
 695                             SMQGridViewField fld = field as SMQGridViewField;
 696                             if (fld.DataField == e.CommandArgument.ToString())
 697                                 fld.IsEditing = !fld.IsEditing;
 698                         }
 699                     }
 700                     break;
 701                 case "AllEdit":
 702                     {
 703                         foreach (DataControlField field in Columns)
 704                         {
 705                             if (field is SMQGridViewField)
 706                             {
 707                                 SMQGridViewField fld = field as SMQGridViewField;
 708                                 fld.IsEditing = true;
 709                             }
 710                         }
 711                         break;
 712                     }
 713                 case "SaveChanges":
 714                     if (_IsSaveChange)
 715                     {
 716                         if (!SaveChanges())
 717                             return;
 718                     }
 719                     break;
 720             }
 721             base.OnRowCommand(e);
 722         }
 723 
 724         protected override void Render(HtmlTextWriter writer)
 725         {
 726             this.Style.Add("border-collapse", "separate!important");
 727             writer.Write("<div class=\"proList\"><div class=\"prolistTop fontbold textindent10\">" + GridViewName + "</div>");
 728             base.Render(writer);
 729             writer.Write("</div>");
 730 
 731             ScriptManager.RegisterStartupScript(this, this.GetType(), this.UniqueID, this.SetGVNoRecordScript(), true);
 732 
 733         }
 734 
 735         private bool SaveChanges()
 736         {
 737             if (string.IsNullOrEmpty(EntityType))
 738                 return false;
 739 
 740             #region Validate has smq field.
 741             bool hasSMQField = false;
 742             foreach (DataControlField field in this.Columns)
 743             {
 744                 if (field is SMQGridViewField)
 745                 {
 746                     SMQGridViewField fie = field as SMQGridViewField;
 747                     if (fie.IsEditing || fie.fieldType == FieldType.Delete)
 748                         hasSMQField = true;
 749                 }
 750             }
 751             if (!hasSMQField)
 752                 return false;
 753             #endregion
 754 
 755             EntityList DeleteList = new EntityList();
 756             EntityList UpdateList = new EntityList();
 757             foreach (GridViewRow row in Rows)
 758             {
 759                 int PrimaryKeyValue = int.Parse(DataKeys[row.RowIndex].Value.ToString());
 760 
 761                 Entity entity = System.Activator.CreateInstance(Type.GetType("ProTexus.Model." + EntityType)) as Entity;
 762 
 763                 #region Get Table Name
 764                 PropertyInfo[] properties = entity.GetType().GetProperties();
 765                 foreach (BindingClassAttribute attr in entity.GetType().GetCustomAttributes(typeof(BindingClassAttribute), true))
 766                 {
 767                     entity.TableName = attr.TableName;
 768                     break;
 769                 }
 770                 #endregion
 771 
 772                 #region Get Original Data
 773                 string Usp_SQL = "SELECT top 1 * FROM [" + entity.TableName + "] WHERE [" + PrimaryKeyName + "]=" + PrimaryKeyValue.ToString();
 774                 ReturnValue _result = entity.getEntity(Usp_SQL);
 775                 if (!_result.Success)
 776                 {
 777                     return false;
 778                 }
 779                 entity = _result.Object;
 780                 #endregion
 781 
 782                 #region Set New Data
 783                 foreach (DataControlFieldCell cell in row.Cells)
 784                 {
 785                     if (cell.ContainingField is SMQGridViewField)
 786                     {
 787 
 788                         SMQGridViewField field = cell.ContainingField as SMQGridViewField;
 789 
 790                         if (field.fieldType == FieldType.Delete)
 791                         {
 792                             CheckBox cbx = cell.Controls[0] as CheckBox;
 793                             if (cbx.Checked)
 794                             {
 795                                 entity.GetType().GetProperty(PrimaryKeyName).SetValue(entity, PrimaryKeyValue, null);
 796                                 DeleteList.Add(entity);
 797                                 goto NewRow;
 798                             }
 799                         }
 800                         else
 801                         {
 802                             if (field.IsEditing)
 803                             {
 804                                 if (field.fieldType == FieldType.DateTime)
 805                                 {
 806                                     //DateSelector dsl = cell.Controls[0] as DateSelector;
 807                                     //if (dsl.HasDateTime)
 808                                     //{
 809                                     //    entity.GetType().GetProperty(field.DataField).SetValue(entity, dsl.DateTime, null);
 810                                     //}
 811                                     //else
 812                                     //{
 813                                     //    cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Please provide a datetime</div>"));
 814                                     //    return false;
 815                                     //}
 816                                 }
 817                                 if (field.fieldType == FieldType.CheckBox)
 818                                 {
 819                                     CheckBox cbx = cell.Controls[0] as CheckBox;
 820                                     entity.GetType().GetProperty(field.DataField).SetValue(entity, cbx.Checked, null);
 821                                 }
 822                                 if (field.fieldType == FieldType.CheckBoxFeature)
 823                                 {
 824                                     CheckBox cbx = cell.Controls[0] as CheckBox;
 825                                     string statusId = "Status";
 826                                     entity.GetType().GetProperty(statusId).SetValue(entity, cbx.Checked, null);
 827                                 }
 828 
 829                                 if (field.fieldType == FieldType.Status)
 830                                 {
 831                                     DropDownList ddl = cell.Controls[0] as DropDownList;
 832                                     string contentStatusName = "ContentStatusId";
 833                                     if (!string.IsNullOrEmpty(field.ContentStatusName))
 834                                     {
 835                                         contentStatusName = field.ContentStatusName;
 836                                     }
 837                                     entity.GetType().GetProperty(contentStatusName).SetValue(entity, int.Parse(ddl.SelectedValue), null);
 838                                 }
 839 
 840                                 if (field.fieldType == FieldType.PPStatus)
 841                                 {
 842                                     DropDownList ddl = cell.Controls[0] as DropDownList;
 843                                     string contentStatusName = "ContentStatusId";
 844                                     if (!string.IsNullOrEmpty(field.ContentStatusName))
 845                                     {
 846                                         contentStatusName = field.ContentStatusName;
 847                                     }
 848                                     entity.GetType().GetProperty(contentStatusName).SetValue(entity, int.Parse(ddl.SelectedValue), null);
 849                                 }
 850 
 851                                 if (field.fieldType == FieldType.Feature)
 852                                 {
 853                                     DropDownList ddl_Feature = cell.Controls[0] as DropDownList;
 854                                     string locationId = "Location";
 855                                     if (!string.IsNullOrEmpty(field.FeatureLocation))
 856                                     {
 857                                         locationId = field.FeatureLocation;
 858                                     }
 859                                     entity.GetType().GetProperty(locationId).SetValue(entity, int.Parse(ddl_Feature.SelectedValue), null);
 860                                 }
 861 
 862                                 if (field.fieldType == FieldType.Int)
 863                                 {
 864                                     TextBox txt = cell.Controls[0] as TextBox;
 865                                     if (SILK2010.Utilities.isInteger(txt.Text))
 866                                     {
 867                                         entity.GetType().GetProperty(field.DataField).SetValue(entity, int.Parse(txt.Text), null);
 868                                     }
 869                                     else
 870                                     {
 871                                         cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Please provide a integer</div>"));
 872                                         return false;
 873                                     }
 874                                 }
 875 
 876                                 if (field.fieldType == FieldType.MaxQty)
 877                                 {
 878                                     TextBox txt = cell.Controls[0] as TextBox;
 879 
 880                                     if (!string.IsNullOrEmpty(txt.Text))
 881                                     {
 882                                         if (SILK2010.Utilities.IsDouble(txt.Text.Trim()))
 883                                         {
 884                                             //if (int.Parse(txt.Text.Trim()) <= 0)
 885                                             //{
 886                                             //    cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Duration‘s data must larger than 0</div>"));
 887                                             //    return false;
 888 
 889                                             //}
 890                                             //else
 891                                             //{
 892                                             entity.GetType().GetProperty(field.DataField).SetValue(entity, int.Parse(txt.Text), null);
 893                                             //}
 894                                         }
 895                                         else
 896                                         {
 897                                             cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Please provide a number</div>"));
 898                                             return false;
 899                                         }
 900                                     }
 901                                     else
 902                                     {
 903                                         entity.GetType().GetProperty(field.DataField).SetValue(entity, null, null);
 904 
 905                                     }
 906 
 907 
 908                                 }
 909                                 if (field.fieldType == FieldType.Double)
 910                                 {
 911                                     TextBox txt = cell.Controls[0] as TextBox;
 912                                     if (SILK2010.Utilities.IsDouble(txt.Text))
 913                                     {
 914                                         entity.GetType().GetProperty(field.DataField).SetValue(entity, double.Parse(txt.Text), null);
 915                                     }
 916                                     else
 917                                     {
 918                                         cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Please provide a number</div>"));
 919                                         return false;
 920                                     }
 921                                 }
 922                                 if (field.fieldType == FieldType.TextBox || field.fieldType == FieldType.LinkButton)
 923                                 {
 924                                     TextBox txt = cell.Controls[0] as TextBox;
 925                                     entity.GetType().GetProperty(field.DataField).SetValue(entity, txt.Text, null);
 926                                 }
 927                                 if (field.fieldType == FieldType.Duration)
 928                                 {
 929                                     TextBox txt_Duration = cell.Controls[0] as TextBox;
 930                                     DropDownList ddl_TimeType = cell.Controls[1] as DropDownList;
 931 
 932                                     if (!string.IsNullOrEmpty(txt_Duration.Text))
 933                                     {
 934                                         if (SILK2010.Utilities.IsDouble(txt_Duration.Text.Trim()))
 935                                         {
 936                                             //if (double.Parse(txt_Duration.Text.Trim()) <= 0)
 937                                             //{
 938                                             //    cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Duration‘s data must larger than 0</div>"));
 939                                             //    return false;
 940                                             //}
 941                                             //else
 942                                             //{
 943                                             if (ddl_TimeType.SelectedValue == "Order")
 944                                             {
 945                                                 entity.GetType().GetProperty("Duration").SetValue(entity, double.Parse("1"), null);
 946                                                 entity.GetType().GetProperty("TimeType").SetValue(entity, ddl_TimeType.SelectedValue, null);
 947 
 948                                             }
 949                                             else
 950                                             {
 951 
 952                                                 entity.GetType().GetProperty("Duration").SetValue(entity, double.Parse(txt_Duration.Text.Trim()), null);
 953                                                 entity.GetType().GetProperty("TimeType").SetValue(entity, ddl_TimeType.SelectedValue, null);
 954                                             }
 955                                             //}
 956 
 957                                         }
 958                                         else
 959                                         {
 960 
 961                                             cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Please provide a number for Duration field</div>"));
 962                                             return false;
 963 
 964                                         }
 965                                     }
 966                                     else
 967                                     {
 968                                         if (ddl_TimeType.SelectedValue == "Order")
 969                                         {
 970                                             entity.GetType().GetProperty("Duration").SetValue(entity, double.Parse("1"), null);
 971                                             entity.GetType().GetProperty("TimeType").SetValue(entity, ddl_TimeType.SelectedValue, null);
 972 
 973                                         }
 974                                         else
 975                                         {
 976                                             entity.GetType().GetProperty("Duration").SetValue(entity, null, null);
 977                                             entity.GetType().GetProperty("TimeType").SetValue(entity, null, null);
 978                                         }
 979 
 980                                     }
 981 
 982                                 }
 983                             }
 984                         }
 985                     }
 986                 }
 987 
 988 
 989                 UpdateList.Add(entity);
 990 
 991 
 992               //UpdateList.Add(entity);
 993 
 994             NewRow:
 995                 continue;
 996 
 997                 #endregion
 998 
 999             }
1000 
1001             #region Save New Data
1002 
1003             Entity entityForTrans = System.Activator.CreateInstance(Type.GetType("ZstoreAdmin.Model." + EntityType)) as Entity;
1004 
1005             Transaction trans = new Transaction(entityForTrans.DataConnectProviders);
1006 
1007             ReturnValue result = new ReturnValue();
1008 
1009             foreach (Entity entity in DeleteList)
1010             {
1011                 result = entity.Delete(trans);
1012                 if (!result.Success)
1013                 {
1014                     if (trans.InTransaction)
1015                         trans.RollbackTransaction();
1016                     return false;
1017                 }
1018             }
1019 
1020 
1021 
1022             foreach (Entity entity in UpdateList)
1023             {
1024                 result = entity.Update(trans);
1025                 if (!result.Success)
1026                 {
1027                     if (trans.InTransaction)
1028                         trans.RollbackTransaction();
1029                     return false;
1030                 }
1031             }
1032 
1033 
1034             trans.CommitTransaction();
1035 
1036             #endregion
1037 
1038             #region Restore Grid State
1039 
1040             foreach (DataControlField field in this.Columns)
1041             {
1042                 if (field is SMQGridViewField)
1043                 {
1044                     ((SMQGridViewField)field).IsEditing = false;
1045                 }
1046             }
1047 
1048             #endregion
1049 
1050             return true;
1051         }
1052 
1053         public bool SaveChangesAfterValidation()
1054         {
1055             return this.SaveChanges();
1056         }
1057 
1058         private string SetGVNoRecordScript()
1059         {
1060 
1061             if (this.bHasRecord)
1062             {
1063                 return "";
1064             }
1065 
1066 
1067             StringBuilder sb = new StringBuilder();
1068 
1069             sb.Append("\r\n function SetGVNoRecord" + this.ClientID + "(){");
1070             sb.Append("\r\n     var gv = document.getElementById(‘" + this.ClientID + "‘);");
1071             sb.Append("\r\n     if(gv == null){ return;}");
1072             sb.Append("\r\n     var rowCount = gv.rows.length;");
1073             sb.Append("\r\n     for(var i = rowCount - 1 ; i > 0;i--){");
1074             sb.Append("\r\n         if(i == 1){");
1075             sb.Append("\r\n             for(var j =0 ; j < gv.rows[i].cells.length;j++){");
1076             sb.Append("\r\n                 gv.rows[i].cells[j].innerHTML=‘&nbsp;‘;");
1077             sb.Append("\r\n             }");
1078             sb.Append("\r\n         }");
1079             sb.Append("\r\n         else {");
1080             sb.Append("\r\n             gv.deleteRow(i);");
1081             sb.Append("\r\n         }");
1082             sb.Append("\r\n     }");
1083             sb.Append("\r\n }");
1084             sb.Append("\r\n SetGVNoRecord" + this.ClientID + "();");
1085 
1086             return sb.ToString();
1087         }
1088     }
1089 }
GridView

 

bubuko.com,布布扣
   1 using System;
   2 using System.Web.UI;
   3 using System.Web.UI.WebControls;
   4 using System.Web.UI.HtmlControls;
   5 using System.Collections;
   6 using System.Collections.Generic;
   7 using System.Text;
   8 using System.Web.UI.Design;
   9 using System.ComponentModel;
  10 using SILK2010;
  11 using System.Reflection;
  12 using System.Web;
  13 
  14 
  15 
  16 namespace FrameWorkV4.Controls
  17 {
  18     public class SMQGridView : System.Web.UI.WebControls.GridView
  19     {
  20         #region Private Property
  21         private int dataCout;
  22         private int DataCount
  23         {
  24             get
  25             {
  26                 if (dataCout == 0 && DataSource != null)
  27                 {
  28                     if (DataSource is IEnumerable)
  29                     {
  30                         IEnumerable DataSourceList = (IEnumerable)DataSource;
  31                         IEnumerator enumerator = DataSourceList.GetEnumerator();
  32                         enumerator.Reset();
  33                         while (enumerator.MoveNext())
  34                         {
  35                             dataCout++;
  36                         }
  37                     }
  38                     if (DataSource is IListSource)
  39                     {
  40                         IListSource DataSourceList = (IListSource)DataSource;
  41                         IList list = DataSourceList.GetList();
  42                         dataCout = list.Count;
  43                     }
  44                 }
  45                 return dataCout;
  46             }
  47         }
  48         private int FirstPageIndex
  49         {
  50             get
  51             {
  52                 return 0;
  53             }
  54         }
  55         private int PreviosPageIndex
  56         {
  57             get
  58             {
  59                 return Math.Max(PageIndex - 1, 0);
  60             }
  61         }
  62         private int NextPageIndex
  63         {
  64             get
  65             {
  66                 return Math.Min(PageIndex + 1, PageCount - 1);
  67             }
  68         }
  69         private int LastPageIndex
  70         {
  71             get
  72             {
  73                 return PageCount - 1;
  74             }
  75         }
  76         //Added by tony at 2010.12.16
  77         private int TotalDataCount
  78         {
  79             get
  80             {
  81                 object o = ViewState["TotalDataCount"];
  82                 int _dataCout = 0;
  83                 if (o != null)
  84                 {
  85                     _dataCout = int.Parse(o.ToString());
  86                 }
  87                 return _dataCout;
  88             }
  89             set
  90             {
  91                 ViewState["TotalDataCount"] = value;
  92             }
  93         }
  94         #endregion
  95 
  96         #region Public Property
  97         public string OrderByField
  98         {
  99             get
 100             {
 101                 object o = ViewState["OrderByField"];
 102                 if (o == null)
 103                 {
 104                     for (int i = 0; i < Columns.Count; i++)
 105                     {
 106                         if (Columns[i].SortExpression.Trim() != string.Empty)
 107                             return Columns[i].SortExpression;
 108                     }
 109                     return string.Empty;
 110                 }
 111                 return (string)o;
 112             }
 113             set
 114             {
 115                 ViewState["OrderByField"] = value;
 116             }
 117         }
 118         public bool OrderByAsc
 119         {
 120             get
 121             {
 122                 object o = ViewState["OrderByAsc"];
 123                 if (o == null)
 124                 {
 125                     return true;
 126                 }
 127                 return (bool)o;
 128             }
 129             set
 130             {
 131                 ViewState["OrderByAsc"] = value;
 132             }
 133         }
 134         public string EntityType;
 135         public string PrimaryKeyName;
 136         public string GridViewName;
 137         private bool showSaveChangesButton
 138         {
 139             get
 140             {
 141                 foreach (DataControlField field in Columns)
 142                 {
 143                     if (field is SMQGridViewField)
 144                         return true;
 145                 }
 146                 return false;
 147             }
 148         }
 149         #endregion
 150 
 151 
 152         private bool _isSaveChange = true;
 153         public bool _IsSaveChange
 154         {
 155             get
 156             {
 157                 return _isSaveChange;
 158             }
 159             set
 160             {
 161                 _isSaveChange = value;
 162             }
 163 
 164         }
 165 
 166 
 167         //Added by tony at 2010.12.16
 168         public override object DataSource
 169         {
 170             get
 171             {
 172                 return base.DataSource;
 173             }
 174             set
 175             {
 176                 base.DataSource = value;
 177                 int _dataCout = 0;
 178                 if (value is IEnumerable)
 179                 {
 180                     IEnumerable DataSourceList = (IEnumerable)value;
 181                     IEnumerator enumerator = DataSourceList.GetEnumerator();
 182                     enumerator.Reset();
 183                     while (enumerator.MoveNext())
 184                     {
 185                         _dataCout++;
 186                     }
 187                 }
 188                 if (value is IListSource)
 189                 {
 190                     IListSource DataSourceList = (IListSource)DataSource;
 191                     IList list = DataSourceList.GetList();
 192                     _dataCout = list.Count;
 193                 }
 194                 TotalDataCount = _dataCout;
 195             }
 196         }
 197 
 198         private bool _isAllowSaveChange = false;
 199 
 200 
 201 
 202         public bool IsAllowSaveChange
 203         {
 204             get { return _isAllowSaveChange; }
 205             set { _isAllowSaveChange = value; }
 206         }
 207 
 208 
 209         private bool _alwaysShowSaveChange = true;
 210 
 211         public bool AlwaysShowSaveChange
 212         {
 213             get { return _alwaysShowSaveChange; }
 214             set { _alwaysShowSaveChange = value; }
 215         }
 216         private bool _isAllowAllEdit = false;
 217 
 218         public bool IsAllowAllEdit
 219         {
 220             get { return _isAllowAllEdit; }
 221             set { _isAllowAllEdit = value; }
 222         }
 223 
 224         private int _gv_ProgramProductId;
 225         public int GV_ProgramProductId
 226         {
 227             get { return _gv_ProgramProductId; }
 228             set { _gv_ProgramProductId = value; }
 229         }
 230 
 231         private bool _isProductFeature = false;
 232         public bool IsProductFeature
 233         {
 234             get { return _isProductFeature; }
 235             set { _isProductFeature = value; }
 236         }
 237 
 238         private bool _isSweepCart = false;
 239         public bool IsSweepCart
 240         {
 241             get { return _isSweepCart; }
 242             set { _isSweepCart = value; }
 243         }
 244 
 245         private bool _isprogramproductordertype = false;
 246         public bool IsProgramProductOrderType
 247         {
 248             get
 249             {
 250                 return _isprogramproductordertype;
 251             }
 252             set
 253             {
 254                 _isprogramproductordertype = value;
 255             }
 256         }
 257 
 258         private bool _isprogramproductcustomertype = false;
 259 
 260         public bool IsProgramProductCustomerType
 261         {
 262             get
 263             {
 264                 return _isprogramproductcustomertype;
 265             }
 266             set
 267             {
 268                 _isprogramproductcustomertype = value;
 269             }
 270         }
 271 
 272         private bool _isReducePrice = false;
 273         public bool IsReducePrice
 274         {
 275             get { return _isReducePrice; }
 276             set { _isReducePrice = value; }
 277         }
 278 
 279         private int _programproductid = 0;
 280         public int ProgramProductId
 281         {
 282             get
 283             {
 284                 return _programproductid;
 285             }
 286             set
 287             {
 288                 _programproductid = value;
 289             }
 290         }
 291 
 292         private bool _isAllowPage = true;
 293         public bool IsAllowPage
 294         {
 295             get { return _isAllowPage; }
 296             set { _isAllowPage = value; }
 297         }
 298 
 299 
 300         private bool _updateppstatus = false;
 301         public bool UpdatePPStatus
 302         {
 303             get
 304             {
 305                 return _updateppstatus;
 306             }
 307             set
 308             {
 309                 _updateppstatus = value;
 310             }
 311         }
 312 
 313 
 314         private bool bHasRecord
 315         {
 316             get
 317             {
 318                 try
 319                 {
 320                     return bool.Parse(ViewState["bHasRecord"].ToString());
 321                 }
 322                 catch
 323                 {
 324                     return false;
 325                 }
 326             }
 327             set
 328             {
 329                 ViewState["bHasRecord"] = value.ToString();
 330             }
 331         }       
 332 
 333         protected override void OnDataBinding(EventArgs e)
 334         {
 335             if (DataSource == null)
 336             {
 337                 base.OnDataBinding(e);
 338                 return;
 339             }
 340 
 341             var aa = DataSource as List<SILK2010.Entity>;
 342             if ((DataSource is SILK2010.EntityList) == false)
 343             {
 344                 base.OnDataBinding(e);
 345                 return;
 346             }
 347 
 348 
 349             SILK2010.EntityList ds = DataSource as SILK2010.EntityList;
 350             if (!string.IsNullOrEmpty(OrderByField))
 351             {
 352                 ds.Sort(OrderByField, OrderByAsc);
 353             }
 354             if (!string.IsNullOrEmpty(PrimaryKeyName))
 355             {
 356                 this.DataKeyNames = new string[1] { PrimaryKeyName };
 357             }
 358             if (!_isAllowPage)
 359             {
 360                 this.AllowPaging = false;
 361             }
 362             else
 363             {
 364                 this.AllowPaging = true;
 365             }
 366 
 367             if (ds.Count == 0)
 368             {
 369                 if (string.IsNullOrEmpty(this.EntityType))
 370                 {
 371                     return;
 372                 }
 373 
 374                 Assembly asmb = Assembly.LoadFrom(HttpContext.Current.Server.MapPath("~/Bin/ProTexusModel.dll"));
 375                 Entity entity = System.Activator.CreateInstance(asmb.GetType("ProTexus.Model." + EntityType)) as Entity;
 376 
 377                 ds.Add(entity);
 378 
 379                 this.DataSource = ds;
 380 
 381                 this.bHasRecord = false;
 382             }
 383             else
 384             {
 385                 this.bHasRecord = true;
 386             }
 387             base.OnDataBinding(e);
 388         }
 389 
 390         private void SetAdvancedPage(GridViewRowEventArgs e)
 391         {
 392             HtmlGenericControl prolistbottom = new HtmlGenericControl("DIV");
 393             e.Row.Cells[0].Controls.Add(prolistbottom);
 394             prolistbottom.Attributes.Add("class", "prolistbottom textindent10");
 395             HtmlGenericControl floatleft = new HtmlGenericControl("DIV");
 396             floatleft.Attributes.Add("class", "floatleft fontwhite");
 397             prolistbottom.Controls.Add(floatleft);
 398             HtmlGenericControl pagesort = new HtmlGenericControl("DIV");
 399             floatleft.Controls.Add(pagesort);
 400             pagesort.Attributes.Add("class", "pagesort");
 401 
 402             LiteralControl Pages = new LiteralControl();
 403             Pages.Text = "<b>Page(s):</>"; 
 404             pagesort.Controls.Add(Pages);
 405 
 406             LiteralControl _space = new LiteralControl();
 407 
 408             //Build "First" and "Previous" button cell.
 409             Button _btnFirst = new Button();
 410             _btnFirst.Text = "First";
 411             _btnFirst.CssClass = "button";
 412             _btnFirst.CommandName = "Pager";
 413             _btnFirst.CommandArgument = FirstPageIndex.ToString();
 414             pagesort.Controls.Add(_btnFirst);
 415 
 416             Button _btnPrevious = new Button();
 417             _btnPrevious.Text = "Previous";
 418             _btnPrevious.CssClass = "button";
 419             _btnPrevious.CommandName = "Pager";
 420             _btnPrevious.CommandArgument = PreviosPageIndex.ToString();
 421             pagesort.Controls.Add(_btnPrevious);
 422 
 423             //Build Page list button.
 424             for (int i = 0; i < PageCount; i++)
 425             {
 426                 if (i > PageIndex - 10 && i < PageIndex + 10)
 427                 {
 428                     LinkButton _pageLink = new LinkButton();
 429                     int cur = i + 1;
 430                     if (PageIndex == i)
 431                     {
 432                         _pageLink.Text = "<b><font size=4>" + cur.ToString() + "</font></>";
 433                     }
 434                     else
 435                     {
 436                         _pageLink.Text = "" + cur.ToString() + "";
 437                     }
 438                     _pageLink.CommandName = "Pager";
 439                     _pageLink.CommandArgument = i.ToString();
 440                     pagesort.Controls.Add(_pageLink);
 441 
 442                     if (PageIndex == i)
 443                     {
 444                         _pageLink.Enabled = false;
 445                     }
 446 
 447                     _space = new LiteralControl();
 448                     _space.Text = "&nbsp;";
 449                     pagesort.Controls.Add(_space);
 450                 }
 451             }
 452 
 453             //Build "Next" and "Last" button cell.
 454             _space = new LiteralControl();
 455             _space.Text = "&nbsp;";
 456 
 457             Button _btnNext = new Button();
 458             _btnNext.Text = "Next";
 459             _btnNext.CssClass = "button";
 460             _btnNext.CommandName = "Pager";
 461             _btnNext.CommandArgument = NextPageIndex.ToString();
 462             pagesort.Controls.Add(_btnNext);
 463             pagesort.Controls.Add(_space);
 464 
 465             Button _btnLast = new Button();
 466             _btnLast.Text = "Last";
 467             _btnLast.CssClass = "button";
 468             _btnLast.CommandName = "Pager";
 469             _btnLast.CommandArgument = LastPageIndex.ToString();
 470             pagesort.Controls.Add(_btnLast);
 471             pagesort.Controls.Add(_space);
 472 
 473             //Build "Showing Result:" cell.
 474             LiteralControl PageResult = new LiteralControl();
 475             int _currentPageBeginNo = 0;
 476             int _currentPageEndNo = 0;
 477 
 478             //if (DataCount != 0)
 479             //{
 480             //  _currentPageBeginNo = PageIndex * PageSize + 1;
 481             //  _currentPageEndNo = _currentPageBeginNo + PageSize - 1;
 482 
 483             //  if (_currentPageEndNo > DataCount)
 484             //  {
 485             //    _currentPageEndNo = DataCount;
 486             //  }
 487             //}
 488 
 489             //PageResult.Text = "Showing Result:&nbsp;" + _currentPageBeginNo.ToString() + "-";
 490             //if (IsAllowPage)
 491             //{
 492             //    PageResult.Text += _currentPageEndNo.ToString();
 493             //}
 494             //else
 495             //{
 496             //    PageResult.Text += DataCount.ToString();
 497             //}
 498             //PageResult.Text += " of " + DataCount.ToString();
 499             if (TotalDataCount != 0)
 500             {
 501                 _currentPageBeginNo = PageIndex * PageSize + 1;
 502                 _currentPageEndNo = _currentPageBeginNo + PageSize - 1;
 503 
 504                 if (_currentPageEndNo > TotalDataCount)
 505                 {
 506                     _currentPageEndNo = TotalDataCount;
 507                 }
 508             }
 509 
 510             PageResult.Text = "Showing Result:&nbsp;" + _currentPageBeginNo.ToString() + "-";
 511             if (IsAllowPage)
 512             {
 513                 PageResult.Text += _currentPageEndNo.ToString();
 514             }
 515             else
 516             {
 517                 PageResult.Text += TotalDataCount.ToString();
 518             }
 519             PageResult.Text += " of " + TotalDataCount.ToString();
 520             pagesort.Controls.Add(PageResult);
 521 
 522             //Build Save Changes Button Cell
 523             HtmlGenericControl floatright = new HtmlGenericControl("DIV");
 524             floatright.Attributes.Add("class", "floatright");
 525             if (showSaveChangesButton && IsAllowSaveChange && AlwaysShowSaveChange)
 526             {
 527                 Button btn = new Button();
 528                 btn.CommandName = "SaveChanges";
 529                 btn.Text = "Save Changes";
 530                 btn.CssClass = "buttonBig";
 531                 floatright.Controls.Add(btn);
 532             }
 533 
 534             if (showSaveChangesButton && IsAllowAllEdit)
 535             {
 536                 Button btn = new Button();
 537                 btn.CommandName = "AllEdit";
 538                 btn.Text = "Edit All";
 539                 btn.CssClass = "buttonBig";
 540                 floatright.Controls.Add(btn);
 541             }
 542             prolistbottom.Controls.Add(floatright);
 543 
 544             //HtmlGenericControl floatclear = new HtmlGenericControl("DIV");
 545             //floatclear.Attributes.Add("class", "floatclear");
 546             //prolistbottom.Controls.Add(floatclear);
 547 
 548             if (PageIndex == 0)
 549             {
 550                 _btnFirst.Enabled = false;
 551                 _btnPrevious.Enabled = false;
 552             }
 553             if (PageIndex == PageCount - 1)
 554             {
 555                 _btnNext.Enabled = false;
 556                 _btnLast.Enabled = false;
 557             }
 558         }
 559 
 560         protected override void OnRowCreated(GridViewRowEventArgs e)
 561         {
 562             switch (e.Row.RowType)
 563             {
 564                 case DataControlRowType.Footer:
 565                     if (this.bHasRecord && Rows.Count != 0)
 566                     {
 567                         e.Row.Cells[0].ColumnSpan = Columns.Count;
 568                         for (int i = 0; i < Columns.Count - 1; i++)
 569                             e.Row.Cells.RemoveAt(1);
 570                         SetAdvancedPage(e);
 571                         PagerSettings.Visible = false;
 572                         ShowFooter = true;
 573                     }
 574                     break;
 575                 case DataControlRowType.Header:
 576                     #region Build Header
 577                     for (int i = 0; i < Columns.Count; i++)
 578                     {
 579                         TableCell cell = e.Row.Cells[i];
 580                         if (Columns[i].SortExpression.Trim().Length != 0)
 581                         {
 582                             LinkButton _headText = new LinkButton();
 583                             //_headText.CssClass = HeadCss;
 584                             _headText.Text = Columns[i].HeaderText;
 585                             _headText.ToolTip = "Sort this column";
 586                             _headText.CommandName = "Sort";
 587                             _headText.CommandArgument = Columns[i].SortExpression;
 588 
 589                             cell.Controls.Add(_headText);
 590 
 591                             if (OrderByField == Columns[i].SortExpression)
 592                             {
 593                                 ImageButton _headImage = new ImageButton();
 594                                 _headImage.CommandName = "Sort";
 595                                 _headImage.CommandArgument = Columns[i].SortExpression;
 596                                 _headImage.ToolTip = "Sort this column";
 597                                 if (OrderByAsc == true)
 598                                 {
 599                                     _headImage.ImageUrl = "~/Skin/PROTEXUS/Images/SortDirDESC.gif";
 600 
 601                                 }
 602                                 else
 603                                 {
 604                                     _headImage.ImageUrl = "~/Skin/PROTEXUS/Images/SortDirASC.gif";
 605                                 }
 606                                 cell.Controls.Add(_headImage);
 607                             }
 608 
 609                         }
 610                         else
 611                         {
 612                             LinkButton _headText = new LinkButton();
 613                             _headText.Text = Columns[i].HeaderText;
 614                             cell.Controls.Add(_headText);
 615                         }
 616 
 617 
 618                         if (Columns[i] is SMQGridViewField)
 619                         {
 620                             SMQGridViewField field = Columns[i] as SMQGridViewField;
 621                             if (field.CanEdit)
 622                             {
 623                                 if (!IsAllowSaveChange)
 624                                     IsAllowSaveChange = true;
 625 
 626                                 ImageButton _headEdit = new ImageButton();
 627                                 _headEdit.CommandName = "EditColumn";
 628                                 _headEdit.CommandArgument = field.DataField;
 629                                 _headEdit.ToolTip = "Edit this column";
 630                                 if (field.IsEditing)
 631                                 {
 632                                     _headEdit.ImageUrl = "~/Skin/PROTEXUS/Images/icon_edit.gif";
 633                                 }
 634                                 else
 635                                 {
 636                                     _headEdit.ImageUrl = "~/Skin/PROTEXUS/Images/icon_edit_f2.gif";
 637                                 }
 638                                 cell.Controls.Add(_headEdit);
 639                             }
 640                             if (field.fieldType == FieldType.Delete)
 641                             {
 642                                 Image img = new Image();
 643                                 img.ImageUrl = "~/Skin/PROTEXUS/Images/icon_del.gif";
 644                                 cell.Controls.Add(img);
 645                                 CheckBox cbx = new CheckBox();
 646                                 cell.Controls.Add(cbx);
 647                             }
 648                             if (cell.Controls.Count == 0)
 649                             {
 650                                 LiteralControl lbl = new LiteralControl();
 651                                 lbl.Text = field.HeaderText;
 652                                 cell.Controls.Add(lbl);
 653                             }
 654                         }
 655                     }
 656                     #endregion
 657                     break;
 658                 default:
 659                     if (this.bHasRecord)
 660                     {
 661                         e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#F2F2F2");
 662                         base.OnRowCreated(e);
 663                     }
 664                    
 665                     break;
 666             }
 667         }
 668         public void SetNoRecordRow(GridViewRowEventArgs e)
 669         {
 670             //if (!this.bHasRecord)
 671             //{
 672             //    for (int i = 0; i < e.Row.Cells.Count; i++)
 673             //    {
 674             //        e.Row.Cells[i].Text = "&nbsp;";
 675             //    }
 676             //}
 677         }
 678 
 679         protected override void OnRowCommand(GridViewCommandEventArgs e)
 680         {
 681             switch (e.CommandName)
 682             {               
 683                 case "Sort":
 684                     OrderByField = e.CommandArgument.ToString();
 685                     OrderByAsc = !OrderByAsc;
 686                     break;
 687                 case "Pager":
 688                     PageIndex = int.Parse(e.CommandArgument.ToString());
 689                     break;
 690                 case "EditColumn":
 691                     foreach (DataControlField field in Columns)
 692                     {
 693                         if (field is SMQGridViewField)
 694                         {
 695                             SMQGridViewField fld = field as SMQGridViewField;
 696                             if (fld.DataField == e.CommandArgument.ToString())
 697                                 fld.IsEditing = !fld.IsEditing;
 698                         }
 699                     }
 700                     break;
 701                 case "AllEdit":
 702                     {
 703                         foreach (DataControlField field in Columns)
 704                         {
 705                             if (field is SMQGridViewField)
 706                             {
 707                                 SMQGridViewField fld = field as SMQGridViewField;
 708                                 fld.IsEditing = true;
 709                             }
 710                         }
 711                         break;
 712                     }
 713                 case "SaveChanges":
 714                     if (_IsSaveChange)
 715                     {
 716                         if (!SaveChanges())
 717                             return;
 718                     }
 719                     break;
 720             }
 721             base.OnRowCommand(e);
 722         }
 723 
 724         protected override void Render(HtmlTextWriter writer)
 725         {
 726             this.Style.Add("border-collapse", "separate!important");
 727             writer.Write("<div class=\"proList\"><div class=\"prolistTop fontbold textindent10\">" + GridViewName + "</div>");
 728             base.Render(writer);
 729             writer.Write("</div>");
 730 
 731             ScriptManager.RegisterStartupScript(this, this.GetType(), this.UniqueID, this.SetGVNoRecordScript(), true);
 732 
 733         }
 734 
 735         private bool SaveChanges()
 736         {
 737             if (string.IsNullOrEmpty(EntityType))
 738                 return false;
 739 
 740             #region Validate has smq field.
 741             bool hasSMQField = false;
 742             foreach (DataControlField field in this.Columns)
 743             {
 744                 if (field is SMQGridViewField)
 745                 {
 746                     SMQGridViewField fie = field as SMQGridViewField;
 747                     if (fie.IsEditing || fie.fieldType == FieldType.Delete)
 748                         hasSMQField = true;
 749                 }
 750             }
 751             if (!hasSMQField)
 752                 return false;
 753             #endregion
 754 
 755             EntityList DeleteList = new EntityList();
 756             EntityList UpdateList = new EntityList();
 757             foreach (GridViewRow row in Rows)
 758             {
 759                 int PrimaryKeyValue = int.Parse(DataKeys[row.RowIndex].Value.ToString());
 760 
 761                 Entity entity = System.Activator.CreateInstance(Type.GetType("ProTexus.Model." + EntityType)) as Entity;
 762 
 763                 #region Get Table Name
 764                 PropertyInfo[] properties = entity.GetType().GetProperties();
 765                 foreach (BindingClassAttribute attr in entity.GetType().GetCustomAttributes(typeof(BindingClassAttribute), true))
 766                 {
 767                     entity.TableName = attr.TableName;
 768                     break;
 769                 }
 770                 #endregion
 771 
 772                 #region Get Original Data
 773                 string Usp_SQL = "SELECT top 1 * FROM [" + entity.TableName + "] WHERE [" + PrimaryKeyName + "]=" + PrimaryKeyValue.ToString();
 774                 ReturnValue _result = entity.getEntity(Usp_SQL);
 775                 if (!_result.Success)
 776                 {
 777                     return false;
 778                 }
 779                 entity = _result.Object;
 780                 #endregion
 781 
 782                 #region Set New Data
 783                 foreach (DataControlFieldCell cell in row.Cells)
 784                 {
 785                     if (cell.ContainingField is SMQGridViewField)
 786                     {
 787 
 788                         SMQGridViewField field = cell.ContainingField as SMQGridViewField;
 789 
 790                         if (field.fieldType == FieldType.Delete)
 791                         {
 792                             CheckBox cbx = cell.Controls[0] as CheckBox;
 793                             if (cbx.Checked)
 794                             {
 795                                 entity.GetType().GetProperty(PrimaryKeyName).SetValue(entity, PrimaryKeyValue, null);
 796                                 DeleteList.Add(entity);
 797                                 goto NewRow;
 798                             }
 799                         }
 800                         else
 801                         {
 802                             if (field.IsEditing)
 803                             {
 804                                 if (field.fieldType == FieldType.DateTime)
 805                                 {
 806                                     //DateSelector dsl = cell.Controls[0] as DateSelector;
 807                                     //if (dsl.HasDateTime)
 808                                     //{
 809                                     //    entity.GetType().GetProperty(field.DataField).SetValue(entity, dsl.DateTime, null);
 810                                     //}
 811                                     //else
 812                                     //{
 813                                     //    cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Please provide a datetime</div>"));
 814                                     //    return false;
 815                                     //}
 816                                 }
 817                                 if (field.fieldType == FieldType.CheckBox)
 818                                 {
 819                                     CheckBox cbx = cell.Controls[0] as CheckBox;
 820                                     entity.GetType().GetProperty(field.DataField).SetValue(entity, cbx.Checked, null);
 821                                 }
 822                                 if (field.fieldType == FieldType.CheckBoxFeature)
 823                                 {
 824                                     CheckBox cbx = cell.Controls[0] as CheckBox;
 825                                     string statusId = "Status";
 826                                     entity.GetType().GetProperty(statusId).SetValue(entity, cbx.Checked, null);
 827                                 }
 828 
 829                                 if (field.fieldType == FieldType.Status)
 830                                 {
 831                                     DropDownList ddl = cell.Controls[0] as DropDownList;
 832                                     string contentStatusName = "ContentStatusId";
 833                                     if (!string.IsNullOrEmpty(field.ContentStatusName))
 834                                     {
 835                                         contentStatusName = field.ContentStatusName;
 836                                     }
 837                                     entity.GetType().GetProperty(contentStatusName).SetValue(entity, int.Parse(ddl.SelectedValue), null);
 838                                 }
 839 
 840                                 if (field.fieldType == FieldType.PPStatus)
 841                                 {
 842                                     DropDownList ddl = cell.Controls[0] as DropDownList;
 843                                     string contentStatusName = "ContentStatusId";
 844                                     if (!string.IsNullOrEmpty(field.ContentStatusName))
 845                                     {
 846                                         contentStatusName = field.ContentStatusName;
 847                                     }
 848                                     entity.GetType().GetProperty(contentStatusName).SetValue(entity, int.Parse(ddl.SelectedValue), null);
 849                                 }
 850 
 851                                 if (field.fieldType == FieldType.Feature)
 852                                 {
 853                                     DropDownList ddl_Feature = cell.Controls[0] as DropDownList;
 854                                     string locationId = "Location";
 855                                     if (!string.IsNullOrEmpty(field.FeatureLocation))
 856                                     {
 857                                         locationId = field.FeatureLocation;
 858                                     }
 859                                     entity.GetType().GetProperty(locationId).SetValue(entity, int.Parse(ddl_Feature.SelectedValue), null);
 860                                 }
 861 
 862                                 if (field.fieldType == FieldType.Int)
 863                                 {
 864                                     TextBox txt = cell.Controls[0] as TextBox;
 865                                     if (SILK2010.Utilities.isInteger(txt.Text))
 866                                     {
 867                                         entity.GetType().GetProperty(field.DataField).SetValue(entity, int.Parse(txt.Text), null);
 868                                     }
 869                                     else
 870                                     {
 871                                         cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Please provide a integer</div>"));
 872                                         return false;
 873                                     }
 874                                 }
 875 
 876                                 if (field.fieldType == FieldType.MaxQty)
 877                                 {
 878                                     TextBox txt = cell.Controls[0] as TextBox;
 879 
 880                                     if (!string.IsNullOrEmpty(txt.Text))
 881                                     {
 882                                         if (SILK2010.Utilities.IsDouble(txt.Text.Trim()))
 883                                         {
 884                                             //if (int.Parse(txt.Text.Trim()) <= 0)
 885                                             //{
 886                                             //    cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Duration‘s data must larger than 0</div>"));
 887                                             //    return false;
 888 
 889                                             //}
 890                                             //else
 891                                             //{
 892                                             entity.GetType().GetProperty(field.DataField).SetValue(entity, int.Parse(txt.Text), null);
 893                                             //}
 894                                         }
 895                                         else
 896                                         {
 897                                             cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Please provide a number</div>"));
 898                                             return false;
 899                                         }
 900                                     }
 901                                     else
 902                                     {
 903                                         entity.GetType().GetProperty(field.DataField).SetValue(entity, null, null);
 904 
 905                                     }
 906 
 907 
 908                                 }
 909                                 if (field.fieldType == FieldType.Double)
 910                                 {
 911                                     TextBox txt = cell.Controls[0] as TextBox;
 912                                     if (SILK2010.Utilities.IsDouble(txt.Text))
 913                                     {
 914                                         entity.GetType().GetProperty(field.DataField).SetValue(entity, double.Parse(txt.Text), null);
 915                                     }
 916                                     else
 917                                     {
 918                                         cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Please provide a number</div>"));
 919                                         return false;
 920                                     }
 921                                 }
 922                                 if (field.fieldType == FieldType.TextBox || field.fieldType == FieldType.LinkButton)
 923                                 {
 924                                     TextBox txt = cell.Controls[0] as TextBox;
 925                                     entity.GetType().GetProperty(field.DataField).SetValue(entity, txt.Text, null);
 926                                 }
 927                                 if (field.fieldType == FieldType.Duration)
 928                                 {
 929                                     TextBox txt_Duration = cell.Controls[0] as TextBox;
 930                                     DropDownList ddl_TimeType = cell.Controls[1] as DropDownList;
 931 
 932                                     if (!string.IsNullOrEmpty(txt_Duration.Text))
 933                                     {
 934                                         if (SILK2010.Utilities.IsDouble(txt_Duration.Text.Trim()))
 935                                         {
 936                                             //if (double.Parse(txt_Duration.Text.Trim()) <= 0)
 937                                             //{
 938                                             //    cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Duration‘s data must larger than 0</div>"));
 939                                             //    return false;
 940                                             //}
 941                                             //else
 942                                             //{
 943                                             if (ddl_TimeType.SelectedValue == "Order")
 944                                             {
 945                                                 entity.GetType().GetProperty("Duration").SetValue(entity, double.Parse("1"), null);
 946                                                 entity.GetType().GetProperty("TimeType").SetValue(entity, ddl_TimeType.SelectedValue, null);
 947 
 948                                             }
 949                                             else
 950                                             {
 951 
 952                                                 entity.GetType().GetProperty("Duration").SetValue(entity, double.Parse(txt_Duration.Text.Trim()), null);
 953                                                 entity.GetType().GetProperty("TimeType").SetValue(entity, ddl_TimeType.SelectedValue, null);
 954                                             }
 955                                             //}
 956 
 957                                         }
 958                                         else
 959                                         {
 960 
 961                                             cell.Controls.Add(new LiteralControl("<div class=\"MessageSummary\">Please provide a number for Duration field</div>"));
 962                                             return false;
 963 
 964                                         }
 965                                     }
 966                                     else
 967                                     {
 968                                         if (ddl_TimeType.SelectedValue == "Order")
 969                                         {
 970                                             entity.GetType().GetProperty("Duration").SetValue(entity, double.Parse("1"), null);
 971                                             entity.GetType().GetProperty("TimeType").SetValue(entity, ddl_TimeType.SelectedValue, null);
 972 
 973                                         }
 974                                         else
 975                                         {
 976                                             entity.GetType().GetProperty("Duration").SetValue(entity, null, null);
 977                                             entity.GetType().GetProperty("TimeType").SetValue(entity, null, null);
 978                                         }
 979 
 980                                     }
 981 
 982                                 }
 983                             }
 984                         }
 985                     }
 986                 }
 987 
 988 
 989                 UpdateList.Add(entity);
 990 
 991 
 992               //UpdateList.Add(entity);
 993 
 994             NewRow:
 995                 continue;
 996 
 997                 #endregion
 998 
 999             }
1000 
1001             #region Save New Data
1002 
1003             Entity entityForTrans = System.Activator.CreateInstance(Type.GetType("ZstoreAdmin.Model." + EntityType)) as Entity;
1004 
1005             Transaction trans = new Transaction(entityForTrans.DataConnectProviders);
1006 
1007             ReturnValue result = new ReturnValue();
1008 
1009             foreach (Entity entity in DeleteList)
1010             {
1011                 result = entity.Delete(trans);
1012                 if (!result.Success)
1013                 {
1014                     if (trans.InTransaction)
1015                         trans.RollbackTransaction();
1016                     return false;
1017                 }
1018             }
1019 
1020 
1021 
1022             foreach (Entity entity in UpdateList)
1023             {
1024                 result = entity.Update(trans);
1025                 if (!result.Success)
1026                 {
1027                     if (trans.InTransaction)
1028                         trans.RollbackTransaction();
1029                     return false;
1030                 }
1031             }
1032 
1033 
1034             trans.CommitTransaction();
1035 
1036             #endregion
1037 
1038             #region Restore Grid State
1039 
1040             foreach (DataControlField field in this.Columns)
1041             {
1042                 if (field is SMQGridViewField)
1043                 {
1044                     ((SMQGridViewField)field).IsEditing = false;
1045                 }
1046             }
1047 
1048             #endregion
1049 
1050             return true;
1051         }
1052 
1053         public bool SaveChangesAfterValidation()
1054         {
1055             return this.SaveChanges();
1056         }
1057 
1058         private string SetGVNoRecordScript()
1059         {
1060 
1061             if (this.bHasRecord)
1062             {
1063                 return "";
1064             }
1065 
1066 
1067             StringBuilder sb = new StringBuilder();
1068 
1069             sb.Append("\r\n function SetGVNoRecord" + this.ClientID + "(){");
1070             sb.Append("\r\n     var gv = document.getElementById(‘" + this.ClientID + "‘);");
1071             sb.Append("\r\n     if(gv == null){ return;}");
1072             sb.Append("\r\n     var rowCount = gv.rows.length;");
1073             sb.Append("\r\n     for(var i = rowCount - 1 ; i > 0;i--){");
1074             sb.Append("\r\n         if(i == 1){");
1075             sb.Append("\r\n             for(var j =0 ; j < gv.rows[i].cells.length;j++){");
1076             sb.Append("\r\n                 gv.rows[i].cells[j].innerHTML=‘&nbsp;‘;");
1077             sb.Append("\r\n             }");
1078             sb.Append("\r\n         }");
1079             sb.Append("\r\n         else {");
1080             sb.Append("\r\n             gv.deleteRow(i);");
1081             sb.Append("\r\n         }");
1082             sb.Append("\r\n     }");
1083             sb.Append("\r\n }");
1084             sb.Append("\r\n SetGVNoRecord" + this.ClientID + "();");
1085 
1086             return sb.ToString();
1087         }
1088     }
1089 }
View Code

 

 

 

 

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.Design;
using System.ComponentModel;
using SILK2010;

namespace FrameWorkV4.Controls
{
    public class SMQGridViewField : BoundField
    {
        public bool CanEdit;
        public bool IsEditing
        {
            get
            {
                object o = ViewState["IsEditing"];
                if (o == null)
                {
                    return false;
                }
                return (bool)o;
            }
            set
            {
                ViewState["IsEditing"] = value;
            }
        }
        public FieldType fieldType;
        public string ContentStatusName;
        public string FeatureLocation;
        public string StatusName;

        public SMQGridViewField()
        { }

        protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
        {
            base.InitializeDataCell(cell, rowState);
            if (IsEditing)
            {
                switch (fieldType)
                {
                    case FieldType.DateTime:
                        //DateSelector dsl = new DateSelector();
                        //cell.Controls.Add(dsl);
                        break;
                    case FieldType.Status:
                        DropDownList ddl = new DropDownList();
                        ddl.Items.Add(new ListItem("Active", "1"));
                        ddl.Items.Add(new ListItem("Pending", "2"));
                        ddl.Items.Add(new ListItem("Inactive", "3"));
                        ddl.Attributes.Add("onchange", "TableViewer_Mark_Field_Altered(this);");
                        ddl.Attributes.Add("onfocus", "TableViewer_Set_Field_Defaults(this)");
                        cell.Controls.Add(ddl);
                        break;
                    case FieldType.Feature:
                        DropDownList ddl_Feature = new DropDownList();
                        ddl_Feature.Items.Add(new ListItem("Top", "1"));
                        ddl_Feature.Items.Add(new ListItem("Bottom", "2"));
                        ddl_Feature.Attributes.Add("onchange", "TableViewer_Mark_Field_Altered(this);");
                        ddl_Feature.Attributes.Add("onfocus", "TableViewer_Set_Field_Defaults(this)");
                        cell.Controls.Add(ddl_Feature);
                        break;
                    case FieldType.CheckBox:
                        CheckBox cbx = new CheckBox();
                        cbx.Attributes.Add("onclick", "TableViewer_Mark_Field_Altered(this);");
                        cbx.Attributes.Add("onfocus", "TableViewer_Set_Field_Defaults(this)");
                        cell.Controls.Add(cbx);
                        break;
                    case FieldType.CheckBoxFeature:
                        CheckBox cbxfeature = new CheckBox();
                        cbxfeature.Attributes.Add("onclick", "TableViewer_Mark_Field_Altered(this);");
                        cbxfeature.Attributes.Add("onfocus", "TableViewer_Set_Field_Defaults(this)");
                        cell.Controls.Add(cbxfeature);
                        break;
                    case FieldType.MaxQty:
                        TextBox txtMaxQty = new TextBox();
                        txtMaxQty.Width = new Unit("40%");
                        txtMaxQty.Attributes.Add("onkeyup", "TableViewer_Mark_Field_Altered(this);");
                        txtMaxQty.Attributes.Add("onfocus", "TableViewer_Set_Field_Defaults(this)");
                        cell.Controls.Add(txtMaxQty);
                        break;
                    case FieldType.Duration:
                        TextBox txtDuration = new TextBox();
                        txtDuration.Width = new Unit("40%");
                        txtDuration.Attributes.Add("onkeyup", "TableViewer_Mark_Field_Altered(this);");
                        txtDuration.Attributes.Add("onfocus", "TableViewer_Set_Field_Defaults(this)");
                        cell.Controls.Add(txtDuration);
                        DropDownList ddl_TimeType = new DropDownList();
                        ddl_TimeType.Items.Insert(0, new ListItem("Seconds", "Seconds"));
                        ddl_TimeType.Items.Insert(1, new ListItem("Minutes", "Minutes"));
                        ddl_TimeType.Items.Insert(2, new ListItem("Hours", "Hours"));
                        ddl_TimeType.Items.Insert(3, new ListItem("Days", "Days"));
                        ddl_TimeType.Items.Insert(4, new ListItem("Months", "Months"));
                        ddl_TimeType.Items.Insert(5, new ListItem("Years", "Years"));
                        ddl_TimeType.Items.Insert(6, new ListItem("Lifetime", "Lifetime"));
                        ddl_TimeType.Items.Insert(7, new ListItem("Order", "Order"));
                        ddl_TimeType.Attributes.Add("onchange", "TableViewer_Mark_Field_Altered(this);");
                        ddl_TimeType.Attributes.Add("onfocus", "TableViewer_Set_Field_Defaults(this)");
                        cell.Controls.Add(ddl_TimeType);
                        break;
                    case FieldType.PPStatus:
                        DropDownList ddl_ppstatus = new DropDownList();
                        ddl_ppstatus.Items.Add(new ListItem("Active", "1"));
                        ddl_ppstatus.Items.Add(new ListItem("Pending", "2"));
                        ddl_ppstatus.Items.Add(new ListItem("Inactive", "3"));
                        ddl_ppstatus.Attributes.Add("onchange", "TableViewer_Mark_Field_Altered(this);");
                        ddl_ppstatus.Attributes.Add("onfocus", "TableViewer_Set_Field_Defaults(this)");
                        cell.Controls.Add(ddl_ppstatus);
                        break;
                    default:
                        TextBox txt = new TextBox();
                        txt.Width = new Unit("90%");
                        txt.Attributes.Add("onkeyup", "TableViewer_Mark_Field_Altered(this);");
                        txt.Attributes.Add("onfocus", "TableViewer_Set_Field_Defaults(this)");
                        cell.Controls.Add(txt);
                        break;
                }
            }
            else if (fieldType == FieldType.LinkButton)
            {
                LinkButton lbt = new LinkButton();
                lbt.CommandName = "Sel";
                cell.Controls.Add(lbt);
            }
            else if (fieldType == FieldType.Delete)
            {
                this.IsEditing = false;
                CheckBox cbx = new CheckBox();
                cbx.Attributes.Add("onclick", "if (this.checked) this.parentNode.style.backgroundColor = ‘Red‘; else this.parentNode.style.backgroundColor = ‘‘;");
                cell.Controls.Add(cbx);
            }
        }

        protected override void OnDataBindField(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            GridViewRow row = control.NamingContainer as GridViewRow;
            object dataValue = this.GetValue(control.NamingContainer);

            if (fieldType == FieldType.Status)
            {
                if (dataValue != null)
                {
                    switch (dataValue.ToString().ToUpper())
                    {
                        case "ACTIVE":
                            row.BackColor = System.Drawing.ColorTranslator.FromHtml("#F2F2F2");
                            break;
                        case "PENDING":
                            row.BackColor = System.Drawing.ColorTranslator.FromHtml("#FCD5E5");
                            row.Font.Italic = true;
                            break;
                        case "INACTIVE":
                            row.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFF3CE");
                            break;
                    }
                }
            }

            if (IsEditing)
            {
                if (this.fieldType == FieldType.DateTime)
                {
                    //if (dataValue != null)
                    //{
                    //    if (dataValue.GetType().Name == "DateTime")
                    //    {
                    //        ((DateSelector)control.Controls[0]).DateTime = (DateTime)dataValue;
                    //    }
                    //    else
                    //    {
                    //        try
                    //        {
                    //            ((DateSelector)control.Controls[0]).DateTime = DateTime.Parse(dataValue.ToString());
                    //        }
                    //        catch
                    //        { }
                    //    }
                    //}

                }
                if (this.fieldType == FieldType.TextBox)
                {
                    if (dataValue != null)
                        ((TextBox)control.Controls[0]).Text = dataValue.ToString();
                }
                if (this.fieldType == FieldType.LinkButton)
                {
                    if (dataValue != null)
                        ((TextBox)control.Controls[0]).Text = dataValue.ToString();
                }
                if (this.fieldType == FieldType.CheckBox)
                {
                    if (dataValue != null)
                        ((CheckBox)control.Controls[0]).Checked = (bool)dataValue;
                }
                if (this.fieldType == FieldType.Int)
                {
                    if (dataValue != null)
                        ((TextBox)control.Controls[0]).Text = dataValue.ToString();
                }
                if (this.fieldType == FieldType.MaxQty)
                {
                    if (dataValue != null)
                        ((TextBox)control.Controls[0]).Text = dataValue.ToString();
                }
                if (this.fieldType == FieldType.Double)
                {
                    if (dataValue != null)
                        ((TextBox)control.Controls[0]).Text = dataValue.ToString();
                }
                if (this.fieldType == FieldType.Status)
                {
                    if (dataValue != null)
                    {
                        switch (dataValue.ToString().ToUpper())
                        {
                            case "ACTIVE":
                                ((DropDownList)control.Controls[0]).SelectedValue = "1";
                                break;
                            case "PENDING":
                                ((DropDownList)control.Controls[0]).SelectedValue = "2";
                                break;
                            case "INACTIVE":
                                ((DropDownList)control.Controls[0]).SelectedValue = "3";
                                break;
                        }
                    }
                }

                if (this.fieldType == FieldType.PPStatus)
                {
                    if (dataValue != null)
                    {
                        switch (dataValue.ToString().ToUpper())
                        {
                            case "ACTIVE":
                                ((DropDownList)control.Controls[0]).SelectedValue = "1";
                                break;
                            case "PENDING":
                                ((DropDownList)control.Controls[0]).SelectedValue = "2";
                                break;
                            case "INACTIVE":
                                ((DropDownList)control.Controls[0]).SelectedValue = "3";
                                break;
                        }
                    }
                }


                if (this.fieldType == FieldType.Duration)
                {
                    if (dataValue != null)
                    {

                        string[] _info = dataValue.ToString().Split( );

                        if (_info.Length == 2)
                        {

                            if (!string.IsNullOrEmpty(_info[0].ToString()))
                            {
                                ((TextBox)control.Controls[0]).Text = _info[0].ToString();
                            }



                            if (!string.IsNullOrEmpty(_info[1].ToString()))
                            {
                                ((DropDownList)control.Controls[1]).SelectedValue = _info[1].ToString();
                            }
                        }

                    }
                }



            }
            else if (control.Controls.Count > 0 && this.fieldType == FieldType.LinkButton)
            {
                if (dataValue != null)
                {
                    ((LinkButton)control.Controls[0]).Text = dataValue.ToString();
                    ((LinkButton)control.Controls[0]).CommandArgument = ((GridViewRow)control.Parent).RowIndex.ToString();
                }
            }
            else
            {
                base.OnDataBindField(sender, e);
            }
        }
    }

    public enum FieldType
    {
        TextBox,
        CheckBox,
        Status,
        LinkButton,
        Delete,
        DateTime,
        Int,
        Double,
        Feature,
        CheckBoxFeature,
        Duration,
        MaxQty,
        PPStatus,
    }
}

 

ASP.net 自定义控件GridView

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/hellolong/p/3991090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!