前几天遇到个问题,有一个DropDownList需要对某些Item对标记。我们大家知道Dropdownlist最终呈现是<select>,最后我们想到html是:

   1:    <select>
   2:          <option value="volvo" title="share">Volvo</option>
   3:          <option value="saab"  title="share">Saab</option>
   4:          <option value="mercedes">Mercedes</option>
   5:          <option value="audi">Audi</option>
   6:      </select>

     在Server端我们使用Attributes给他们加上title属性, 如以下代码片段:

   1:         return ddlresultset.Select(t =>
   2:              {
   3:                  var newitem = new ListItem(t.UserId.HasValue ? t.Name : "*" + t.Name, t.Id.ToString());
   4:                  if (!t.UserId.HasValue)
   5:                      newitem.Attributes.Add("title", "share");
   6:                  return newitem;
   7:              }).OrderBy(d => d.Text).ToArray();

现在我们绑定上去,当我们做POSTBACK时,发现title属性丢掉了,原因是DropDownList控制没有存储title属性到ViewState。我们看下DropDownList类继承关系:

System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.BaseDataBoundControl
System.Web.UI.WebControls.DataBoundControl
System.Web.UI.WebControls.ListControl
System.Web.UI.WebControls.BulletedList
System.Web.UI.WebControls.CheckBoxList
System.Web.UI.WebControls.DropDownList
System.Web.UI.WebControls.ListBox
System.Web.UI.WebControls.RadioButtonList

相同的问题将出现派生ListControl的控件。解决方法,是重写SaveViewState 与 LoadViewState方法,看如下代码:

   1:          /// <summary>
   2:          /// Saves the current view state of the <see cref="T:System.Web.UI.WebControls.ListControl"/> -derived control and the items it contains.
   3:          /// </summary>
   4:          /// <returns>
   5:          /// An <see cref="T:System.Object"/> that contains the saved state of the <see cref="T:System.Web.UI.WebControls.ListControl"/> control.
   6:          /// </returns>
   7:          protected override object SaveViewState()
   8:          {
   9:              // Create an object array with one element for the CheckBoxList's
  10:              // ViewState contents, and one element for each ListItem in skmCheckBoxList
  11:              object[] state = new object[this.Items.Count + 1];
  12:   
  13:              object baseState = base.SaveViewState();
  14:              state[0] = baseState;
  15:   
  16:              // Now, see if we even need to save the view state
  17:              bool itemHasAttributes = false;
  18:              for (int i = 0; i < this.Items.Count; i++)
  19:              {
  20:                  if (this.Items[i].Attributes.Count > 0)
  21:                  {
  22:                      itemHasAttributes = true;
  23:   
  24:                      // Create an array of the item's Attribute's keys and values
  25:                      object[] attribKV = new object[this.Items[i].Attributes.Count * 2];
  26:                      int k = 0;
  27:                      foreach (string key in this.Items[i].Attributes.Keys)
  28:                      {
  29:                          attribKV[k++] = key;
  30:                          attribKV[k++] = this.Items[i].Attributes[key];
  31:                      }
  32:   
  33:                      state[i + 1] = attribKV;
  34:                  }
  35:              }
  36:   
  37:              // return either baseState or state, depending on whether or not
  38:              // any ListItems had attributes
  39:              if (itemHasAttributes)
  40:                  return state;
  41:              else
  42:                  return baseState;
  43:          }
  44:   
  45:   
  46:          /// <summary>
  47:          /// Loads the previously saved view state of the <see cref="T:System.Web.UI.WebControls.DetailsView"/> control.
  48:          /// </summary>
  49:          /// <param name="savedState">An <see cref="T:System.Object"/> that represents the state of the <see cref="T:System.Web.UI.WebControls.ListControl"/> -derived control.</param>
  50:          protected override void LoadViewState(object savedState)
  51:          {
  52:              if (savedState == null) return;
  53:   
  54:              // see if savedState is an object or object array
  55:              if (savedState is object[])
  56:              {
  57:                  // we have an array of items with attributes
  58:                  object[] state = (object[])savedState;
  59:                  base.LoadViewState(state[0]);   // load the base state
  60:   
  61:                  for (int i = 1; i < state.Length; i++)
  62:                  {
  63:                      if (state[i] != null)
  64:                      {
  65:                          // Load back in the attributes
  66:                          object[] attribKV = (object[])state[i];
  67:                          for (int k = 0; k < attribKV.Length; k += 2)
  68:                              this.Items[i - 1].Attributes.Add(attribKV[k].ToString(),
  69:                                                             attribKV[k + 1].ToString());
  70:                      }
  71:                  }
  72:              }
  73:              else
  74:                  // we have just the base state
  75:                  base.LoadViewState(savedState);
  76:          }
  77:      }

这样也可实现,我们看下呈现后ViewState中有我们那个title属性了,通过对ViewState的Decode 清晰可见:

ViewStateForDDL

很简单,希望对您Develop有帮助!


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog

posted on 2010-05-26 10:18  PetterLiu  阅读(625)  评论(0编辑  收藏  举报