How to solve the problem of multi-field value when ASP. NET is added? asp.net Value

Source: Internet
Author: User

How to solve the problem of multi-field value when ASP. NET is added? asp.net Value

ASP. NET developers often encounter a situation when sending a card, that is, there are too many fields in the newly added page. When clicking and saving, they need to assign values one by one or create SQL statements to save them. This not only wastes physical strength, but also requires a lot of text row controls to write code. Is it possible to use a more convenient solution after conception? Improve code cohesion.

 1. Ideas

We know that most newly added pages are text boxes that allow users to input content, and then click the Save button to submit and save the data to the database.

When clicking submit, the traditional method is to read and assign values in one 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"/>

Event code for button submission:

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

If there are many fields on the page, many assignment statements are required. Therefore, when multiple fields exist, we can traverse the controls on the page to assign values, and then put the results into the set 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:

1. Define a dictionary set to store the values in the field text box, including the key and value.

2. cyclically traverse the control in the container and add nodes to the set. The key is the Control ID, that is, the Database List. The value is the actual input value, that is, the value to be added to the database.

3. Call the Save method to save the data

Problem:

Someone may ask, is it too insecure to keep the control name on the page consistent with the database field name? The solution to this problem is to encrypt a custom algorithm of database Field on the page and encapsulate a TextBox extension method. This method is used to parse this algorithm, return the correct listing Code as follows:

Public static class TextBoxEx {public static string GetMapColumnsName (this TextBox my) {string myColumnsName = my. ID; // in the future, the text ID can be decrypted here for the security saved as an encrypted value. return myColumnsName ;}}

In this way, few codes can be used to solve the problem. During addition or editing, there are too many fields on the page!

2. respond to changes in requirements

Now, if the field on the page is added or reduced, you don't have to submit the button event. Instead, you can add or delete the corresponding display control on the page!

For example, there are five TextBox controls on the page. If you need to add one more TextBox Control on the page, you just need to write the ID as the text that has been encrypted by yourself!

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 use it in your own learning.

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.