Some time ago, I closed the door and charged my resources. After several days, I started my graduation design journey.
This is a user registration page. The layout is to use table. The control mainly uses label, Textbox, And button. The user needs to input 12 items and then press "Submit, after verification, insert the database.
There is also a "reset" button.
First of all, my idea is to use JavaScript and server-side verification in combination. For JavaScript, please wait for the next summary.
First, I will verify if all textbox items are empty. If they are empty, I will prompt that the user must be filled out. The problem lies in traversing all textbox controls!
At that time, I wrote the following code and made several modifications, but the basic idea was not changed.
Protected void button#click (Object sender, eventargs e) {bool isok = true; foreach (control C in this. controls) {If (C. getType () = typeof (textbox) {If (C as textbox ). TEXT = "") {isok = false ;}} if (isok = false) {response. write ("<SCRIPT> alert ('fill in all information'); </SCRIPT>");} // database utility goes here ~ }
Although I think this code should be okay, but it is really a hit. Alert didn't come out a window when the browser was killed.
During my debugging, I found that the program did not enter the following loop after it was re-launched:
if (c.GetType() == typeof(TextBox)) { if (c as Textbox).Text == "") { IsOK = false; } }
That is to say, we have never found a textbox-type control!
I took a closer look and found that sometimes I could find system. Web. UI. literalcontrol, but I don't know what it is. I will check the information when I have time.
I have searched for articles on control traversal on the Internet and found that their code is the same as my thinking. I really don't know whether it is my tragedy or those who resort to fraud?
Finally, someone said that recursion was required, but the person did not have a clear idea or code.
After a long time, I finally got it ready.
===================== ======================================
In order to test, I put a label on the page and modified the code of the page so that the program can output a specific textbox on a label that is null and 0 as the blank one, non-null values are represented by 1,
Therefore, a string of 12 characters must be output.
1. When loading a page, clear the label first.
protected void Page_Load(object sender, EventArgs e) { lblss.Text = ""; }
2. The following method returns the string to determine which textbox is null, indicating whether the test is successful or not.
public string SubmitInfo(ControlCollection cc) { string s = ""; foreach (Control c in cc) { if (c.HasControls()) { SubmitInfo(c.Controls); } else { if (c.GetType() == typeof(TextBox)) { if ((c as TextBox).Text == "") { lblss.Text += "0"; } else { lblss.Text += "1"; } } } } return s; }
3. Based on the string returned by the above method, if there is "0", you can determine that there is a blank textbox on the page, and whether the position is empty (if you want, you can also dynamically prompt the user "XX information is not blank "),
The code will not be available.
Finally, I am very confident that it takes a few hours to traverse all the controls.
In addition, a lot of information on the Internet is confusing and cannot be easily believed.
It is hereby summarized for future reference.