Problems encountered in dynamically adding controls in DataList

Source: Internet
Author: User
Tags first row

In the exam section, the need to add dynamic checkbox to allow users to choose whether to do the choice problem, or to do a judgment question, I in the DataList ItemDataBound event to add a checkbox to the DataList, in the interface can also show, I have a button outside the DataList, to loop through this DataList, to determine which user chose which, but the error came out, always "hold the object reference to the instance", with my experience, know that this is not found this control, find the wrong one afternoon, or can not find, Look carefully, looking for code errors, found no errors, but how can not find it, in looking for DataList events, found that will be in the Itemcreate event, the code copy to the Itemcreate event, in operation, it was successful, which I understand, Why the control has not been found, if the page is first accessed (Page.IsPostBack = False), then the first time the DataBind is executed, the ItemCreated event is fired first, that is, after the DataBind is executed, First the header line is built with itemcreated, then the header row is bound with ItemDataBound, then the first line is built with ItemCreated, then the itemdatabound is used to bind the first row, In other words, itemcreated and ItemDataBound are executed alternately.
When the page returns, the ItemCreated event is executed, before Page_Load, but the ItemDataBound event is no longer executed. If you want to add any controls dynamically in the DataList, you need to be in the ItemCreated event, not in the ItemDataBound event.

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.