2011-06-02 95 views
0

我已經構建了以下自定義控件,並且在單擊項目符號列表項目時設置了選定的值。在ASP.Net自定義控件上保留選定的項目

我的問題是,我需要在回發後設置css類(SetSelected()),但它總是在視圖狀態中拾取前一個條目,而不是拾取新值。

我不認爲我應該使用PageLoad來設置CSS類,但我不確定在哪裏最好做到這一點。

任何人都可以幫忙嗎?

[DefaultProperty("SelectedValue"), 
ToolboxData("<{0}:GlossaryList runat=\"server\" />")] 
public class GlossaryList : WebControl 
{ 
    const string MANAGEDMETADATASERVICE = "Managed Metadata Service"; 
    const string TOYOTA = "Toyota"; 

    BulletedList _bulletList = new BulletedList(); 

    public String SelectedText 
    { 
     get 
     { 
      object selectedText = ViewState["SelectedText"]; 
      return (selectedText == null) ? String.Empty : (string)selectedText; 
     } 
     set 
     { 
      ViewState["SelectedText"] = value; 
     } 
    } 

    public String SelectedValue 
    { 
     get 
     { 
      object selectedValue = ViewState["SelectedValue"]; 
      return (selectedValue == null) ? String.Empty : (string)selectedValue; 
     } 
     set 
     { 
      ViewState["SelectedValue"] = value; 
     } 
    } 

    protected override void OnInit(EventArgs e) 
    { 
     CreateBulletedList();  

     base.OnInit(e); 
    } 

    protected override void OnLoad(EventArgs e) 
    { 
     if (this.Page.IsPostBack) 
     { 
      SetSelected(); 
     } 

     base.OnLoad(e); 
    } 

    private void SetSelected() 
    { 
     //if(this.Controls != null && this.Controls.Count > 0) 
     //{ 
     // foreach (ListItem listItem in ((BulletedList)this.Controls[0]).Items) 
     // { 
     //  if (listItem.Value == SelectedValue) 
     //  { 
     //   listItem.Attributes.Add("class", "active"); 
     //  } 
     // } 
     //} 
    } 

    protected void CreateBulletedList() 
    { 
     _bulletList.Click += new BulletedListEventHandler(BulletListItem_Click); 
     _bulletList.DisplayMode = BulletedListDisplayMode.LinkButton; 
     _bulletList.CssClass = "letter-selector"; 

     for (char c = 'A'; c <= 'Z'; c++) 
     { 
      ListItem listItem = new ListItem(); 

      listItem.Text = c.ToString(); 
      listItem.Value = c.ToString(); 

      if (SelectedValue == c.ToString()) 
      { 
       listItem.Attributes.Add("class", "active"); 
      } 

      _bulletList.Items.Add(listItem); 
     } 

     if (!this.Page.IsPostBack) 
     { 
      _bulletList.Items[0].Selected = true; 
      _bulletList.Items[0].Attributes.Add("class", "active"); 
     } 

     this.Controls.Add(_bulletList); 
    } 

    private void BulletListItem_Click(object sender, BulletedListEventArgs e) 
    { 
     SelectedValue = SelectedText = _bulletList.Items[e.Index].Value; 

     SetActive(e.Index); 
    } 

    private void SetActive(int index) 
    { 
     if (this.Controls != null && ((BulletedList)this.Controls[0]).Items.Count > 0) 
     { 
      foreach (ListItem listItem in ((BulletedList)this.Controls[0]).Items) 
      { 
       listItem.Attributes.Clear(); 
      } 
     } 

     ((BulletedList)this.Controls[0]).Items[index].Attributes.Add("class", "active"); 
    } 
} 

回答

1

添加以下代碼:

protected override void CreateChildControls() 
{ 
    Controls.Clear(); 
    CreateBulletedList(); 
} 

public override ControlCollection Controls 
{ 
    get 
    { 
     EnsureChildControls(); 
     return base.Controls; 
    } 
} 


protected override void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 
    EnsureChildControls(); 
} 

更改的setSelected方法:

private void SetSelected() 
{ 
     foreach (ListItem listItem in _bulletList.Items) 
     { 
      if (listItem.Value == SelectedValue) 
      { 
       listItem.Attributes.Add("class", "active"); 
      } 
     } 

} 
+0

感謝那些工作一種享受。你能解釋一下怎麼回事? – Burt 2011-06-02 13:07:05

+0

請檢查此鏈接http://msdn.microsoft.com/en-us/library/3257x3ea.aspx另外,請注意,您必須從INamingContainer界面繼承您的控件。 – 2011-06-02 13:25:52