Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to maintain state in (asp.net) custom server control?
    text
    copied!<p>I am trying to make a custom server control which inherits from DropDownList. I give the control an XML input containing some key/value pairs and my control shows them as a DropDownList. I make the list items in the override Render method like this:</p> <pre><code>foreach (XElement child in root.Elements("Choice")) { string title = child.Element("Title").Value; string score = child.Element("Score").Value; item = new ListItem(); item.Text = title; item.Value = score; this.Items.Add(item); } </code></pre> <p>The problem is that, when the user selects and item in the list, and the page posts back, the selected item is lost, and the list is re-initialized with the default data.</p> <p>Does anyone have any idea how to keep the selected item, i.e. maintain the state?</p> <p>Here is the complete source:</p> <pre><code>public class MultipleChoiceQuestionView2 : DropDownList </code></pre> <p>{ public MultipleChoiceQuestionView2() : base() { }</p> <p>protected override void Render(HtmlTextWriter writer) { writer.RenderBeginTag(HtmlTextWriterTag.Table); writer.RenderBeginTag(HtmlTextWriterTag.Tr);</p> <p>writer.RenderBeginTag(HtmlTextWriterTag.Td); #region Parse Contets if (!String.IsNullOrEmpty(this.Contents)) { XElement root = XElement.Parse(this.Contents);</p> <p>if (root.HasAttributes) { this.NoOfChoices = Int32.Parse(root.Attribute("ItemCount").Value); }</p> <p>this.Items.Clear(); this.Style.Add("width", "100px"); this.Style.Add("font-family", "Tahoma"); this.Items.Clear(); ListItem item = new ListItem(); item.Text = ""; item.Value = "0"; this.Items.Add(item);</p> <p>foreach (XElement child in root.Elements("Choice")) { string title = child.Element("Title").Value; string score = child.Element("Score").Value; item = new ListItem(); item.Text = title; item.Value = score;</p> <pre><code>this.Items.Add(item); </code></pre> <p>} } #endregion base.Render(writer); writer.RenderEndTag();</p> <p>if (this.Required) { RequiredFieldValidator rfv = new RequiredFieldValidator(); rfv.ControlToValidate = this.ID; rfv.InitialValue = "0"; rfv.Text = "*"; if (!String.IsNullOrEmpty(this.ValidationGroup)) { rfv.ValidationGroup = this.ValidationGroup; } writer.RenderBeginTag(HtmlTextWriterTag.Td); rfv.RenderControl(writer); writer.RenderEndTag(); }</p> <p>writer.RenderEndTag(); writer.RenderEndTag(); } #region Properties public string Contents { get { return ViewState["Contents"] == null ? "" : ViewState["Contents"].ToString(); } set { ViewState["Contents"] = value; } }</p> <p>private int mNoOfChoices; public int NoOfChoices { get { return mNoOfChoices; } set { mNoOfChoices = value; } }</p> <p>private string mValidationGroup; public string ValidationGroup { get { return mValidationGroup; } set { mValidationGroup = value; } }</p> <p>public string SelectedChoice { get { return ""; } }</p> <p>private bool mRequired = false; public bool Required { get { return mRequired; } set { mRequired = value; } }</p> <p>#endregion }</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload