How to solve the problem of multi-field asp.net in new time _ practical skills

Source: Internet
Author: User

ASP.net developers often encounter a situation when Kafa, that is, there are too many fields in the new page, you need an assignment entity or build an SQL statement to save it when you click Save. This not only wastes energy but also consumes a lot of text line controls to write code. Is it possible to use a more convenient solution to the idea? Improve the cohesion of your code.

1. Thinking

We know that the most common new page is a lot of text boxes to let users enter the content, and then click the Save button to submit the data persisted to the database.

The traditional way to click Submit is to assign a read to a text box.

The HTML code is as follows:

<asp:textbox id= "TextBox2" runat= "Server" text= "TextBox2" ></asp:TextBox> 
<asp:textbox id= " TextBox3 "runat=" Server "text=" TextBox3 "></asp:TextBox> 

Button Submit Event code:

protected void button2_click (object sender, EventArgs e) 
{       
  string colName1 = TextBox2.Text;
  string colName2 = TextBox3.Text;       
  Datasave (colname1,colname2); 
} 


If a page has a lot of fields, it will result in the need for many assignment statements. So in the case of multiple fields we can go through the controls on the page to assign values, and then put the results into the collection to commit the persisted data.

Button Submit Time Code:

protected void button2_click (object sender, EventArgs e) 
 {       
   dictionary<string, string> entitydic = new Dictionary<string, string> ();       
   foreach (Control CNL in Mypanel.controls)       
   {         
      if (CNL is textbox)         
      {           
       TextBox TB = (CNL as textbox);           
       Entitydic.add (TB. Getmapcolumnsname (), TB. Text);         
      }        
   Idbhelper dbhelp = DataBaseProvider.Instance.GetDBHelper ("orm");       
   string result = Dbhelp.datasave (entitydic);     
} 

Analyze the Code:

1. First define a dictionary set to hold the values in the field text box, key is listed, value saves the actual input values

2. Loop through the control in the container, add nodes to the collection, key for the control ID is the database, value is the actual input value, that is, need to add to the value of the database

3. Save data by calling Save method

Problem:

At this point, someone might ask, would it be too unsafe to keep the name of the control on the page consistent with the field name of the database? The solution to this problem is to have the database field one of its own custom algorithm encryption displayed on the page, in the encapsulation of a TextBox extension method, this method is used to parse the algorithm, return the correct listing, the code is as follows:

public static class Textboxex 
{public    
  static string Getmapcolumnsname (this TextBox i)    
  {      
   string Mycolumnsname = my.id;      
   Future text IDs can be stored for security as encrypted values can be responsible for decrypting the return      
   mycolumnsname    
  }  
} 

This can be used in very little code to solve, when new or edited, the page's field too many problems!

2. Response to demand accounts

Now if the field of the page is new, or reduced, we may not need to submit the event of the button, but add or remove the corresponding display control on the page!

For example, there are 5 TextBox controls on the page, and if you need to add a TextBox control on the page, write the ID in the text that you've added, OK!

The above is to share with you how to solve asp.net new multiple field value problem method, I hope we read carefully, apply to their own learning.

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.