开发者

Issue with .net dynamic control generation

开发者 https://www.devze.com 2023-01-10 07:30 出处:网络
I\'m working on an asp.net website (using C#). I have implemented a detailsview as part of a data entry system for this website.

I'm working on an asp.net website (using C#). I have implemented a detailsview as part of a data entry system for this website.

The detailsview contains a drop down list used to associate a category with the record being submitted to this data entry system.

The code behind file accesses a datasource (an SQL server 2005 database table), to determine the fields associated with a selected category and to generate checkbox controls based upon the fields available in that category

I understand (I think) the .net page lifecycle, and the necessity to add dynamic controls on each postback to maintain the controls and their "state". However:

  • I've read that I must add dynamic controls in the Page_Init/initialisation phase of the page lifecycle, in order for the dynamic controls properties and events to be available upon a postback

  • The value I require to query the datasource (and to determine the number and names of the dynamic controls for a category selection) is assigned in the dropdown list's SelectedIndexChanged event handler, which is always processed after the Page_init event

I'm not sure how I can pass the required value (the dropdown list's selected index) to the Page_Init event at the correct point in the page lifecycle (the Page_init event).

I would greatly appreciate any pointers/assist开发者_如何转开发ance from the stackoverflow community and thank you for taking the time to read this post.


You do not have to add the controls in the init, you can add them in page_load just fine as well. It is often recomended to add them in the init as this is the point in the page lifecycle that controls defined in the markup are instantiated. Why do you need to assign the value to determine whether the controls should be added in the SelectedIndexChanged event. If it is based on the SelectedValue of a drop down list, can you not simply access the SelectedValue and assign the value on each post back, even if it has not changed. Then you could do it in the Page_Load and then add your controls afterwards.


The value you are after is posted back to the server and can be found in Request.Form NameValueCollection. The key is the name of the dropdown list.

0

精彩评论

暂无评论...
验证码 换一张
取 消