About datalist nested datalist (the focus is to deal with some special structures and data binding)

Source: Internet
Author: User
Recently, the company wants to present a customer display system: (similar to a questionnaire and generating statistical charts) involving the nested datalist, now I want to share my experiences with you and find a simpler and more appropriate method!

Nesting datalist with datalist is nothing more than constructing a display function similar to a tree structure (N topics include N small topics, and each big topic is embedded with a small topic)

For example, my project is a survey project with the following results:

Have you ever had coffee?

Drinking
No
Do you like coffee?

Like
Average
Dislike
Your monthly salary?

1000 or less
From 1000 to 5000
More than 5000
Your degree?

Junior college or below
Junior college
Big Ben
Master's degree or above

The red content is the question and the green content is the answer content. Because the database design will certainly separate the question and answer, and multiple answers to one question (one-to-many relationship ), so Questions and answers
It will be in different tables and cannot implement the order structure according to this structure using a statement (that is, the final style cannot be completed using a datalist ).
Then we will use the nested method. The red part is the parent datalist, and the green part is the Child datalist, which does not interfere with each other.

Front-end code:
<% @ Import namespace = "system. Data" %>
<Asp: datalist id = "parent" runat = "server" Height = "134px" width = "133px" oninit = "parent_init">
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # eval ("questioncontent ") %> 'font-bold = "true" forecolor = "red"> </ASP: Label>
& Nbsp; <asp: datalist id = "child" runat = "server" datasource = '<% # (datarowview) container. dataitem ). row. getchildrows ("myrelations") %> 'forecolor = "green">
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # databinder. eval (container. dataitem, "[\" answercontent \ "]") %> '> </ASP: Label>
</Itemtemplate>
</ASP: datalist>
</Itemtemplate>
</ASP: datalist>

Note: the green part of the code is the binding of the project question and the selection answer.
In the red part of the code, bind "myrelations" to the data source of the sub-datalist to the name of the relation between the question and the selected answer table (for the reason of this relation name, we will introduce it in the following code)

Background code:

String connstr = configurationmanager. connectionstrings ["researchprojectconnectionstring"]. connectionstring; // read the Web. config connection string
Sqlconnection conn = new sqlconnection (connstr );
Sqldataadapter sqlda1 = new sqldataadapter ("select * From Question", Conn); // traverse the problem table
Sqldataadapter sqlda2 = new sqldataadapter ("select * From answer", Conn); // traverse the answer table
Dataset DS = new dataset ();
Sqlda1.fill (DS, "Question ");
Sqlda2.fill (DS, "Answer ");
DS. relations. add ("myrelations", DS. tables ["Question"]. columns ["questionorder"], DS. tables ["Answer"]. columns ["questionorder"]); // associate two tables.
Parent. datasource = Ds. Tables ["Question"]; // bind the parent datalist TO THE SOURCE


Page. databind (); // bind
Conn. Close ();

Note: The associated line of code means that the inner join query in the SQL statement is to connect two tables through questionorder.

In the question table, the questionorder and the questionorder in the answer table must correspond one to one and have a one-to-many relationship. Then, the content of the latter can be automatically arranged after the relevant content of the former, which constitutes the final effect.

(God scolded me just now and said I didn't make it clear. I 'd like to give you an example now! Amen !)
The questionorder in the question table contains four types of values: 1, 2, 3, and 4.
The questionorder in the answer table will have n numbers, 1 N numbers, 2 N numbers, 3 N numbers, 4 numbers, but there will never be any other such as 5, 6.

In this case:

Question table answer table

Questionorder ques questionorder
1. Are you God? 1 is God
2. Are you a good guy? 1 not God
3 Do you like coffee? 2. Good guys
4 do you like to write a blog? 2 not a good person
3. Love to drink
3. Average
3 do not like to drink
4 love writing
4. Do not write

The result obtained through the above method is as follows:

Are you God?
Is God
Not God
Are you a good guy?
Good guy
Not a good person
Do you like coffee?
Drinking
Average
Do not like to drink
Do you like to write a blog?
Love writing
Do not write


Take a closer look and you will understand it!

The above is my experience. At present, we have made 1/3 of the functions, and we will also use JavaScript to control the answer so that the mouse can be dragged to the specified area, then, submit and generate the report chart (in the preparation process ..)

The following is what we hope you can share with me:
I. The above method is old. It is the practice of Version 1.1:

<% @ Import namespace = "system. Data" %>
<Asp: datalist id = "parent" runat = "server" Height = "134px" width = "133px" oninit = "parent_init">
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # eval ("questioncontent ") %> 'font-bold = "true" forecolor = "red"> </ASP: Label>
& Nbsp; <asp: datalist id = "child" runat = "server" datasource = '<% # (datarowview) container. dataitem ). row. getchildrows ("myrelations") %> 'forecolor = "green">
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # databinder. eval (container. dataitem, "[\" answercontent \ "]") %> '> </ASP: Label>
</Itemtemplate>
</ASP: datalist>
</Itemtemplate>
</ASP: datalist>

You can see <% # (datarowview) container. dataitem ). row. getchildrows ("myrelations") %> 'and' <% # databinder. eval (container. dataitem, "[\" answercontent \ "]") %>'
 
Which of the following experts can write a 1.1 statement ???

2: I heard there are other methods. Which of the following is better? Please kindly advise!

In addition, I tried to use
Parent datalist child = (datalist) datalist. findcontrol ("subdatalist ");
Child. datasource = DS // DS is the data source
But I failed because the root parent datalist child = (datalist) datalist. findcontrol ("subdatalist ");
The error cannot be found. I hope you can solve this problem by finding the sub-datalist. At present, I have not succeeded.

All interns are cheering!

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.