ASP. NET lazy method 4 (dynamic generation of form objects)

Source: Internet
Author: User

When submitting a form, it is often used to drag controls such as textbox to the page, and then click the submit button. The CS Code assigns the control values one by one to an object, then a save method is called and submitted successfully! The tedious operation is basically done once every form is added and modified. Let's leave these cumbersome tasks to. net.

Inherit a custom control from panel and use it as a range. Write a method to traverse all the controls in this range and assign values to the class automatically.

The homepage gives this control an attribute

Public String entity
{
Get
{
String S = (string) viewstate ["entity"];
Return (S = NULL )? String. Empty: S );
}

Set
{
Viewstate ["entity"] = value;
}
}

The attribute value is the entity class operated by the form.

Then write a method to reflect the object class, all controls in the variable range, and assign values.

Public object getentity ()
{
Type T = type. GetType (entity, true );
Object entity = activator. createinstance (t );
Foreach (Object VaR in this. Controls)
{
Type controltype = var. GetType ();
Switch (controltype. Name)
{
Case "smtextbox ":
If (! String. isnullorempty (VaR as frameworkweb. Web. smtextbox). Id ))
{
System. reflection. propertyinfo = T. getproperty (VaR as frameworkweb. Web. smtextbox). Id );
If (propertyinfo! = NULL)
{
String temptext = (VaR as frameworkweb. Web. smtextbox). text;
If (! String. isnullorempty (temptext ))
Propertyinfo. setvalue (entity, convert. changetype (temptext, propertyinfo. propertytype), null );
}
}
Break;
Case "textbox ":
If (! String. isnullorempty (VaR as textbox). Id ))
{
System. reflection. propertyinfo = T. getproperty (VaR as textbox). Id );
If (propertyinfo! = NULL)
{
String temptext = (VaR as textbox). text;
If (! String. isnullorempty (temptext ))
Propertyinfo. setvalue (entity, convert. changetype (temptext, propertyinfo. propertytype), null );
}
}
Break;
Case "smdtpicker ":
If (! String. isnullorempty (VaR as smdtpicker). Id ))
{
System. reflection. propertyinfo = T. getproperty (VaR as smdtpicker). Id );
If (propertyinfo! = NULL)
{
String temptext = (VaR as smdtpicker). text;
If (! String. isnullorempty (temptext ))
Propertyinfo. setvalue (entity, convert. changetype (temptext, propertyinfo. propertytype), null );
}
}
Break;
Case "dropdownlist ":
If (! String. isnullorempty (VaR as dropdownlist). Id ))
{
System. reflection. propertyinfo = T. getproperty (VaR as dropdownlist). Id );
If (propertyinfo! = NULL)
{
String temptext = (VaR as dropdownlist). selectedvalue;
Propertyinfo. setvalue (entity, convert. changetype (temptext, propertyinfo. propertytype), null );
}
}
Break;

Case "hiddenfield ":
If (! String. isnullorempty (VaR as hiddenfield). Id ))
{
System. reflection. propertyinfo = T. getproperty (VaR as hiddenfield). Id );
If (propertyinfo! = NULL)
{
String temptext = (VaR as hiddenfield). value;
Propertyinfo. setvalue (entity, convert. changetype (temptext, propertyinfo. propertytype), null );
}
}
Break;
}
}

Return entity;
}

When the front-end calls this method, you can directly execute this method to get this object. If there are some special cases, it will be OK after second processing.

Then, you need to assign a value to the control during modification, and then submit the modification. This can also be simplified using a similar method.

Add a database source attribute to our control.

Public Virtual Object datasource
{
Get
{
Return this. datasource;
}

Set
{
This. datasource = value;
}
}

Events when adding a Data Binding

Public Delegate void smformdatabindeventhandler (Object source, smformeventargs E );

[Category ("action"), description ("databind")]
Public event smformdatabindeventhandler databindcommand;

/// Here, an object with automatically assigned values is saved for secondary processing.
Public class smformeventargs: eventargs
{
Public readonly object tempobject;
Public smformeventargs (Object tempobject)
{
This. tempobject = tempobject;
}
}

This is to deal with special cases. Sometimes some controls may not directly retrieve the database value, so they can use this event for secondary processing.

Then assign a value to the control.

Protected New void databind ()
{
Propertyinfo P;

Foreach (control VaR in this. Controls)
{
Switch (var. GetType (). Name. toupper ())
{
Case "textbox ":
System. Web. UI. webcontrols. textbox = VaR as system. Web. UI. webcontrols. textbox;
P = datasource. GetType (). getproperty (var. ID );
If (P! = NULL)
Textbox. Text = P. getvalue (datasource, null). tostring (). Trim ();
Break;
Case "smtextbox ":
Frameworkweb. Web. smtextbox = VaR as frameworkweb. Web. smtextbox;
P = datasource. GetType (). getproperty (var. ID );
If (P! = NULL)
Smtextbox. Text = P. getvalue (datasource, null). tostring (). Trim ();
Break;
Case "dropdownlist ":
System. Web. UI. webcontrols. dropdownlist DDL = VaR as system. Web. UI. webcontrols. dropdownlist;
P = datasource. GetType (). getproperty (var. ID );
If (P! = NULL)
{
Object o = P. getvalue (datasource, null );
If (o! = NULL)
DDL. selectedvalue = P. getvalue (datasource, null). tostring (). Trim ();
}
Break;
Case "literal ":
System. Web. UI. webcontrols. literal parameters = VaR as system. Web. UI. webcontrols. literal;
P = datasource. GetType (). getproperty (response. ID );
If (P! = NULL)
Gradient. Text = P. getvalue (datasource, null). tostring (). Trim ();
Break;
Case "hiddenfield ":
System. Web. UI. webcontrols. hiddenfield HF = VaR as system. Web. UI. webcontrols. hiddenfield;
P = datasource. GetType (). getproperty (HF. ID );
If (P! = NULL)
HF. value = P. getvalue (datasource, null). tostring (). Trim ();
Break;
Case "smdtpicker ":
Frameworkweb. Web. smdtpicker = VaR as frameworkweb. Web. smdtpicker;
P = datasource. GetType (). getproperty (var. ID );
If (P! = NULL)
Smdtpicker. Text = P. getvalue (datasource, null). tostring (). Trim ();
Break;
Break;
}
}
}

In this way, the control is basically complete, and no code is needed for calling.

Drag the control and write it in the submitted event.

Model. BTS item = smform. getentity () as model. BTS;
Dal. BTS. Save (item );

When modified

Model. BTS btsmodel = Dal. BTS. GetModel ("scode ");
Smform. datasource = btsmodel;

Then perform secondary processing in the event

Protected void smform_databindcommand (Object source, frameworkweb. Form. smformeventargs E)
{
Model. BTS item = E. tempobject as model. BTS;
If (item! = NULL)
{
Dropdownlist dp = smform. findcontrol ("isenable") as dropdownlist;
DP. Items. findbyvalue (item. isenable. tostring (). Selected = true;
If (item. dicsectortype> 0)
(Smform. findcontrol ("dicsectortype") as dropdownlist). Items. findbyvalue (item. dicsectortype. tostring (). Selected = true;
If (item. dicareatype> 0)
(Smform. findcontrol ("dicareatype") as dropdownlist). Items. findbyvalue (item. dicareatype. tostring (). Selected = true;

// If (item. fkdeviceid> 0)
// (Smform. findcontrol ("fkdeviceid") as dropdownlist). Items. findbyvalue (item. fkdeviceid. tostring (). Selected = true;
}
}

Then save
Model. BTS item = smform. getentity () as model. BTS;
Dal. BTS. Update (item );

Everything is so easy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.