Data paging in Asp.net dynamic forms

Source: Internet
Author: User

However, the problem is that the subjects of different departments are different. Therefore, when designing a database, we usually take the scores of students and certain subjects as a record, at this time, we need to perform a logic processing for Row-to-column conversion.

Solution:
Using the gridview to generate a form makes it more difficult to implement it. It is even harder to display the link in the list;
Generate HTML and output it to the page, which is flexible and convenient;
Basic functions:
Dynamically generate a table header;
Paging data;
Query data;
Hyperlink each score to view details;

PageCode Copy codeThe Code is as follows: <Div id = "datadiv1">
<Asp: literal id = "labtable" runat = "server"> </ASP: literal>
<Div class = "toolblock" style = "border-top: solid 1px # c0cedf; padding-top: 5px">
<BW: virtualpager id = "virtualpager1" runat = "server" itemsperpage = "10" onpageindexchanged = "virtualpager1_pageindexchanged"/>
</Div>
</Div>

Background code (generate HTML string) Copy code The Code is as follows: // <summary>
/// Load the score in Table mode.
/// Step 1: Obtain the unique batch by year and month.
/// Step 2: generate the header.
/// Step 3: Start to cycle every user.
/// Step 4: cycle the assessment items of each user.
/// </Summary>
Protected void bindtable ()
{
Labtable. Text = string. empty;
// Obtain the unique batch by year and month.
String year = dropdownimportyear. selectedvalue;
String month = dropdownimportmonth. selectedvalue;
String group_kh_date = year + month;
Groupservice = new groupservice ();
Datatable dtgroup = groupservice. getbytemplatecodeandgroup_kh_date (templatecode, group_kh_date );
If (dtgroup. Rows. Count> 0)
{
Guid groupid = (guid) dbutils. toguid (dtgroup. Rows [0] ["ID"]);
Int groupstutas = dbutils. toint32 (dtgroup. Rows [0] ["groupstutas"]. tostring ());
Datatable dtcells = personalgradeservice. getdistincttemplateitemnamebygroupid (groupid );
Groupid = groupid. tostring ();
If (dtcells. Rows. Count> 0)
{
Int cellscount = 0; // number of assessment items.
Stringbuilder strtable = new stringbuilder ();
Strtable. appendformat ("<Table cellpadding = \" 0 \ "cellspacing = \" 0 \ "class = \" tblclass \ "> ");
Strtable. appendformat ("<tr> ");
Strtable. appendformat ("<TH> User Name </Th> </span> ");
Cellscount = dtcells. Rows. count;
For (INT I = 0; I <cellscount; I ++)
{
Strtable. appendformat ("<TH> {0} </Th>", dtcells. Rows [I] ["name"]. tostring ());
}
Strtable. appendformat ("</tr> ");
Datatable dtgradedata = getdata (cellscount );
Int cursor = 1; // indicates the first record of a user.
String username = string. empty;
String realname = string. empty;
For (INT I = 0; I <dtgradedata. Rows. Count; I ++)
{
If (cursor = 1)
{
Username = dtgradedata. Rows [I] ["username"]. tostring ();
Realname = dtgradedata. Rows [I] ["realname"]. tostring ();
Strtable. appendformat ("<TD width = \" 12% \ "> {0} </TD>", realname );
}
String id = dtgradedata. Rows [I] ["ID"]. tostring ();
String templateitemcode = dtgradedata. Rows [I] ["templateitemcode"]. tostring ();
Strtable. appendformat ("<TD width = \" 50px \ "> ");

String gradedata = dtgradedata. Rows [I] ["gradedata"]. tostring ();
If (! String. isnullorempty (gradedata) & gradedata! = "0 ")
{
Gradedata = math. Round (decimal. parse (gradedata), 2). tostring ();
// Convert to two decimal places
}
Strtable. appendformat ("{0}", gradedata );
// Unposted score can be modified
If (groupstutas! = (INT) botwave. MRPs. constants. groupstutas. finishpush)
{
Strtable. appendformat ("<br/> <a style = \" text-Decoration: underline \ "class = \" thickbox \ "href = 'usergradedataedit. aspx? Id = {0} & bemodifer = {1} & keepthis = true & tb_iframe = true & Height = 250 & width = 500 '> ", ID, username );
Strtable. appendformat (" ");
Strtable. appendformat ("</a> ");
}
Strtable. appendformat ("<a target = \" _ blank \ "style = \" text-Decoration: underline \ "href = 'HTTP: // www.cnblogs.com/checkobjectmanager/pages/logscorechangedetail.aspx? Groupid = {0} & templatecode = {1} & templateitemcode = {2} & username = {3} & returnurl = '> ", groupid, templatecode, templateitemcode, username );
Strtable. appendformat (" ");
Strtable. appendformat ("</a> ");
Strtable. appendformat ("</TD> ");
If (cursor <cellscount)
Cursor ++;
Else
{
Strtable. appendformat ("</tr> ");
Cursor = 1;
}
}
Strtable. appendformat ("</table> ");
Labtable. Text = strtable. tostring ();
}
}
}

Several auxiliary methods (how to handle the paging problem is related to the use of the cellscount parameter, and cellscount indicates the number of subjects)Copy code The Code is as follows: protected void btnsearch_click (Object sender, eventargs E)
{
Searchnow = true;
Virtualpager1.currentpageindex = 1;
Bindtable ();
}
Protected void virtualpager1_pageindexchanged (Object sender, botwave. Web. Controls. pagechangedeventargs E)
{
Bindtable ();
}
/// <Summary>
/// Obtain the score.
/// </Summary>
/// <Returns> </returns>
Private datatable getdata (INT cellscount)
{
Int nrecordcount = 0;
String condition = "";
String filtername = txtusername. Text. Trim (); // name search.
Stringbuilder sb = new stringbuilder ();
SB. appendformat ("groupid = '{0}'", groupid );
If (! String. isnullorempty (filtername ))
SB. appendformat ("and (username like '% {0} %' or realname like '% {0} %')", filtername );
Condition = sb. tostring ();
If (searchnow) // when you click query on many pages or the last page, currentpageindex is returned. This is to prevent this.
Virtualpager1.currentpageindex = 0;
Datatable dt = personalgradeservice. getgradedata (virtualpager1.currentpageindex, virtualpager1.itemsperpage * cellscount, condition, ref nrecordcount );
Virtualpager1.totalrecordcount = nrecordcount/cellscount;
Virtualpager1.databind ();
Searchnow = false; // you need to copy it again.
Return DT;
}

Business Logic (paging)Copy codeThe Code is as follows: public system. Data. datatable getgradedata (INT currentpageindex, int pagesize, string condition, ref int nrecordcount)
{
String fieldshow = "userid, templatecode, templateitemcode, gradedata, ID, username, realname ";
String fieldorder = "username ASC, templateitemcode ASC ";
String where = condition;
If (string. isnullorempty (where ))
{
Where = "1 = 1 ";
}
Return ibatisdbhelper. getpagedlist ("DBO. vw_usergradedata", "ID", currentpageindex, pagesize, fieldshow, fieldorder, where, ref nrecordcount );
}

Note:
when using If HTML is output on the page to display the form, pay attention to the length of the string. If HTML is too long, an error will occur because literal has a limit of 8000 characters;
sort the data in the database to ensure that the data exactly corresponds to the header.
here is a prerequisite, that is to say, the queried data contains the same number of records. For example, students A and B have the same number of subjects, this dynamic form makes no sense. If there is a difference, it can also be done, but it will be a little more troublesome;

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.