c# - ASP.net Recover JavaScript Created Attribute from PostBack -


i've create control derivates textbox. on class added call javascript code that, when content of text changes, adds attribute called "post" value "true" control. wish collect value.

so far have this:

asp

<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="cweb.web.ui.usr.defpage" %> <%@ register tagprefix="controls" namespace="cweb.web.controls" assembly="web" %> <html> <head>     <script type="text/javascript">         function setpost(element){if(!element.hasattribute('post')){z=document.createattribute('post');z.value='true';element.setattributenode(z);}else{element.setattribute('post','true');}}     </script> </head> <body>     <controls:textbox runat="server" id="abc" /> </body> </html> 

codebehind (and textbox derivated class)

using sysweb = global::system.web.ui; using ctrweb = global::cweb.web.controls; namespace cweb.web {     internal static class binder     {         internal const string postablekey = "post";         internal const string auxfieldkey = "_ck";         internal static bool getcheck(string ck) { return (ck != null && ck != "" && (ck == global::cweb.data.fields.boolean.valuetrue || ck.contains("t") || ck.contains("v"))); }          private static bool getcheckinput(ctrweb.controls.generic.fieldcontrol control, ref bool found)         {             if (control == null || control.page == null) { found = false; }             else             {                 string value = control.page.request.form[control.clientid + ctrweb.binder.auxfieldkey]; //if it's child null, means it's unchecked.                 found = (value != null && value != "");                 return ((found) ? (value == "1" || value == "on" || value.contains("t")) : false);             }             return false;         }          internal static bool getcheck(ctrweb.controls.generic.fieldcontrol control, bool outsideinput, string viewstatekey = ctrweb.binder.checkattribute)         {             string value = control.page.request.form[control.uniqueid]; //if main control exists it's child shal too.             if (value == null || value == "") { return ctrweb.binder.getcheck((string)control.getviewstate()[viewstatekey]); }             else if (outsideinput)             {                 bool found = false;                 return ctrweb.binder.getcheckinput(control, ref found);             } else { return ctrweb.binder.getcheck((string)control.getattributes()[viewstatekey]); }         }          internal static void setcheck(ctrweb.controls.generic.fieldcontrol control, bool outsideinput, string viewstatekey = ctrweb.binder.checkattribute)         {             bool fvalue = ctrweb.binder.getcheck(control, outsideinput, viewstatekey: viewstatekey);             control.getviewstate()[viewstatekey] = ((fvalue) ? "true" : "false");         }     }      namespace controls     {         public interface fieldcontrol         {             string clientid { get; }             string uniqueid { get; }             sysweb.page page { get; }             sysweb.attributecollection getattributes();             sysweb.statebag getviewstate();         }          public class textbox : sysweb.webcontrols.textbox, ctrweb.fieldcontrol         {             public bool postable             {                 { return ctrweb.binder.getcheck(this, false, viewstatekey: ctrweb.binder.postablekey); }                 set { ctrweb.binder.setcheck(this, false, viewstatekey: ctrweb.binder.postablekey); }             }              protected override void loadviewstate(object savedstate)             {                 this.baseloadviewstate(savedstate);                 this.postable = ctrweb.binder.getcheck(this, false, viewstatekey: ctrweb.binder.postablekey);             }              protected override void oninit(global::system.eventargs e)             {                 if (!this.page.ispostback)                 {                     this.attributes.add("onchange", "setpost(this)");                     this.attributes.add(mdweb.binder.postablekey, "false");                 }                 base.oninit(e);             }         }     }      namespace ui.usr     {         public class defpage : sysweb.page { protected ctrweb.textbox abc; }     } } 

some codes not copied here because have nothing issue.

in order data posted server, have store in form value.

html elements in entirety aren't posted server. key/value pairs of form elements are. (webforms trying trick you thinking whole page posted server, it's lie.)

add hidden form field page, simple this:

<asp:hidden id="somehiddenfield" /> 

then in javascript, set value of field:

document.getelementbyid('<%= somehiddenfield.clientid %>').value = 'true'; 

then when page posts server, 'true' value in hidden field:

somehiddenfield.value 

Comments