File and Log #3--add controls dynamically, [Image] Guest counter (recorded with a. txt file)

Source: Internet
Author: User

File and Log #3--add controls dynamically, [Image] Guest counter (recorded with a. txt file)

Two previous articles (included in book "Last episode" of the 17th chapter)

Please look at "asp. NET thematic practice ", Song Kong Publishing

File and Log #1--asp. NET file and I/O Stream (log file for system record) file and log #2--[] Guest counter (recorded with. txt files)

There are so many free resources, and no one has to make a "guest counter".

The results of this paradigm may not be useful, but the empirical experience is interesting.

Reader's letter asking:

The second example above, if you use the Image control (<asp:Image>) and write in the post-program code, what do you do?

It's really simple, but it's just a " dynamic add -on" control.

You can find many examples in my book "Last episode" in the third chapter of the placeholder control or the book "a"

Add the Web Form control dynamically to the website using the program code, add ASP.

Just write the Page. Form. Controls.Add () .

Add "sub-options" to the list control dynamically, for example: Radio DropDownList or check CheckBoxList

--List control ID. Items.Add ()

[Exercise] Dynamically adds a new item (item) under the DropDownList or ListBox #1

Dynamic addition of HTML volume label properties (attribute)

--XXX. Attributes.Add ("Property name", "value")

Exercise GridView bat Effect and JavaScript--e.row.attributes.add () method

Are the procedures of the above three similar?

Previous example , still using the traditional HTML tags

Assuming that the current number of visitors is 1234, then we output, first from the last "4" output, and then output "3" ...

Simply put, the result is "in turn ", a "picture" that corresponds to this number in one place, output to the screen

While (Length >= 1)    {every digit of the number of visitors to//--is presented in pictures. strURL = ". Substring ((Length-1), 1) + "picture file name. gif '/>" + strurl;//Because C # syntax does not have the VB Mid () function, so C # syntax inside, change to write//program code strURL = "Mid (visitors, Length, 1) + "picture file name. gif '/>" + strurl;< /c6>Length = (Length-1);        }

One of the benefits of this approach: when your numbers are from 999

To be raised to four-digit 1000, no additional judgment and program code is required to prevent

However, if you change to a <asp:Image> control , it will be different!

because we're in the post-program code Adding controls dynamically , so must " Sequential "output, from beginning to end Sequential Output

assuming that the current number of visitors is 1234, So when we output, we'll start with the First "1" output, then output "2" .....

for (int i =1; I <= Length; i++){ Each digit of the//--visitor number is presented in a picture. //Because C # syntax does not have the Mid () function of VB,//program code strURL = "Mid (Visitors, I, 1) + "picture file name. gif '/>" & Strur L//So the C # syntax inside, change to write--image img = new image ();img. IMAGEURL = "image/" + visitors . Substring ((i-1), 1) + "picture file name. gif";Page.form. Controls.Add   (IMG); //Add controls dynamically.         }

In addition, this example finds another missing, which is to close the resource too late when "reading" The text file

If the program is wrong, it will lock the. txt file and cannot be released.

It is recommended that the program to close and release resources move Up

After reading, do not have to shut down immediately, release.

//==== Read the file (be sure to modify the permissions of this file, need to "write" permission) = =StreamReader sr = new StreamReader (Server.MapPath ("Counter.txt"));//--put in the file, the original number of visitors [plus one]String visitors = Sr. ReadLine (); Sr.  Close (); //file read, end! Sr. Dispose ();

Related article: Adding Controls dynamically

Reposted Asp. NET dynamic join control [exercise]. The FindControl () method is #1 with the placeholder control (adding "child controls" dynamically, errors that occur due to Page_Load)

Actually adding Web form controls dynamically is not difficult,

Most of the people are stuck in the "dynamic join control, but the user entered the value, the post-program code is not caught "@@facesymbol@@

You can refer to these three articles, starting with this article:

Automatic polling area, network questionnaire, questionnaire survey, how to plan and design? #1 (concept) ... A lesson in the ability to test beginners

File and Log #3--add controls dynamically, [Image] Guest counter (recorded with a. txt file)

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.