How to solve the problem of multi field value selection when adding ASP. NET

  • 2021-07-06 10:43:09
  • OfStack

ASP. NET developers often encounter a situation when issuing cards, that is, there are too many fields in the newly added page, and when clicking Save, they need one assignment entity or build SQL statement to save. This not only wastes energy, but also takes up a lot of text line controls to write code. After thinking, can we use a more convenient method to solve it? Improve the cohesion of code.

1. Thoughts

We know that the most new pages are a lot of text boxes for users to enter content, and then click the Save button to submit and persist the data to the database.

When you click Submit, the traditional way is to read and assign values to 1 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> 
<asp:Button ID="Button2" runat="server" Text=" Submit " onclick="Button2_Click" /> 

Button Submit Event Code:


protected void Button2_Click(object sender, EventArgs e) 
{       
  string colName1 = TextBox2.Text;
  string colName2 = TextBox3.Text;       
  DataSave(colName1,colName2); 
} 


If the page has a lot of fields, it will lead to the need for a lot of assignment statements. So in the case of multiple fields, we can traverse the controls on the page to assign values, and then put the results into the collection to submit persistent data.

Button submission 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 under 1:

1. First, define a dictionary set to store the values in the field text box. key stores the listed values and value stores the actual input values

2. Loop through the controls in the container and add nodes to the collection. key is the ID of the control, that is, the listing of the database, and value is the actual input value, that is, the value that needs to be added to the database

3. Call the save method to save the data

Question:

At this time, some people may ask, is it too unsafe to keep the control name on the page and the field name of the database as 1? The solution to this problem is to encrypt and display an algorithm customized by database field 1 on the page, and encapsulate an expansion method of TextBox. This method is used to analyze this algorithm and return the correct listing. The code is as follows:


public static class TextBoxEx 
{    
  public static string GetMapColumnsName(this TextBox my)    
  {      
   string myColumnsName = my.ID;      
   // Future text ID Can be saved as an encrypted value for security   This can be responsible for decryption       
   return myColumnsName;    
  }  
} 

In this way, you can use very little code to solve the problem that there are too many fields on the page when adding or editing!

2. Respond to changing needs

Now if we add or decrease the fields on the page, we can add or delete the corresponding display controls on the page instead of submitting the button event!

For example, there are 5 TextBox controls on the page. If we need to add one, we only need to write one more TextBox control on the page, and write ID as the text that we add too much, then OK!

The above is how to solve the problem of multi-field value when ASP.NET is added. I hope you can read it carefully and apply it to your own study.


Related articles: