c# - ASP.NET custom control for short term memory -


i'm reasonably new web programming, , i'm still struggling grips fact server has terrible short term memory, forgetting what's on screen time see it.

i'm trying create custom control allows edit data fields inside repeater. depending on format of fields (the elementformat property), either text box or drop down list should generated. inside markup of page have:

<custom:editfieldcontrol id="editfieldcontrol"      runat="server"      elementid='<%#eval("id")%>'                                                            elementvalue='<%#eval("value")%>'      elementformat='<%#eval("format")%>'      visible="false" /> 

control code-behind looks this:

public class editfieldcontrol : webcontrol {      private textbox textbox;     private dropdownlist dropdownlist;          protected override void createchildcontrols()     {         switch (the format of field)         {             case "text":             {                 textbox = new textbox                 {                     text = existing value,                     cssclass = "fieldtextbox"                 };                 this.controls.add(textbox);                 break;                            }             case "dropdownlist":             {                 dropdownlist = new dropdownlist                 {                     //go possible values database                     cssclass = "fielddropdown"                 };                 this.controls.add(dropdownlist);                 break;             }             default:                 throw new argumentexception("invalid format type");         }         base.createchildcontrols();     }      public string value     {                 {             switch (elementformat)             {                 case "dropdownlist":                     return dropdownlist.selectedvalue;                 default:                     return textbox.text;             }         }     } } 

the problem is, when "save field" button clicked, server looks editfieldcontrol in bit of repeater, finds it, , calls value method, promptly errors because trying access either text box or drop down list no longer exist. question is, how @ value user has type in/selected? presume have recreate control earlier in lifecycle, i'm not sure when.

the asp:repeater not ideal control edit data, it's more used display. suitable editing formview, detailsview , best in case listview control. can define separate templates display (itemtemplate), editing (edititemtemplate) , inserting (insertitemtemplate). value property needs setter allow two-way binding. use bind instead of eval.

hope helps.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -