ASP. NET simple question Management System

Source: Internet
Author: User

 

Default. aspx. CS

Using system; <br/> using system. collections; <br/> using system. configuration; <br/> using system. data; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. htmlcontrols; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. XML. LINQ; </P> <p> using system. data. sqlclient; <br/> us Ing system. data. oledb; <br/> using system. io; </P> <p> namespace web0531 <br/>{< br/> Public partial class _ default: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs e) <br/>{< br/> If (! Ispostback) <br/>{< br/> dB. databind (listview1, "select * from exam order by ID"); <br/> try <br/>{< br/> int pagesize = (INT) session ["pagesize"]; // ensure that the modification page is returned to the original page <br/> int startrowindex = (INT) session ["startrowindex"]; <br/> (datapager) listview1.findcontrol ("datapager1 ")). setpageproperties (startrowindex, pagesize, true ); <br/>}< br/> catch {}< br/>}</P> <p> // after the data item is bound, show the option "blue and bold" of the correct answer <br/> protected void listviewincluitemdatabound (Object sender, listviewitemeventargs E) <br/>{< br/> try <br/> {<br/> label lB = (Label) E. item. findcontrol ("answerlabel"); // obtain the answer <br/> string ans = LB. text. trim () + "label"; // if the answer is a; B; C; D, one of them <br/> label lbans = (Label) E. item. findcontrol (ANS); // an error occurs when no answer is available. <br/> lbans. cssclass = "BB "; // set the style of the answer options <br/>}< br/> catch {}< br/>}</P> <p> // execute the command when turning pages <br/> protected void listview1_pagepropertieschanged (Object sender, eventargs e) <br/>{< br/> int pagesize = (datapager) listview1.findcontrol ("datapager1 ")). pagesize; // number of records per page <br/> int startrowindex = (datapager) listview1.findcontrol ("datapager1 ")). startrowindex; // index value of the first line <br/> int totalrowcount = (datapager) listview1.findcontrol ("datapager1 ")). totalrowcount; // The total number of records </P> <p> // response. write ("pagesize =" + pagesize. tostring () + "; startrowindex =" + startrowindex. tostring () + "; totalrowcount =" + totalrowcount. tostring () + ";"); <br/> (datapager) listview1.findcontrol ("datapager1 ")). setpageproperties (startrowindex, pagesize, false); <br/> dB. databind (listview1, "select * from exam order by ID"); // rebind data <br/> session ["startrowindex"] = startrowindex; // record information to the session, used to return to the original page when the page is returned from the modification page <br/> session ["pagesize"] = pagesize; <br/>}</P> <p> // delete a record <br/> protected void listviewincluitemdeleting (Object sender, listviewdeleteeventargs E) <br/>{< br/> string id = (Label) listview1.items [E. itemindex]. findcontrol ("idlabel ")). text; // ID of the record to be deleted <br/> oledbconnection Cn = dB. accessconnection (); <br/> CN. open (); <br/> oledbcommand cmd = new oledbcommand ("delete from exam where id =" + id, CN); <br/> cmd. executenonquery (); <br/> CN. close (); <br/> cmd. dispose (); </P> <p> int pagesize = (datapager) listview1.findcontrol ("datapager1 ")). pagesize; // If the records on the last page are deleted, you may need to adjust <br/> int startrowindex = (datapager) listview1.findcontrol ("datapager1 ")). startrowindex; <br/> int totalrowcount = (datapager) listview1.findcontrol ("datapager1 ")). totalrowcount; <br/> If (totalrowcount <3) // if the record is to be deleted, the system replies to all original records <br/>{< br/> file. copy (server. mappath ("app_data/peixun. bak "), server. mappath ("app_data/peixun. mdb "), true); <br/>}< br/> else <br/>{< br/> If (startrowindex = totalrowcount-1) // if the record on the last page is deleted <br/>{< br/> startrowindex-= pagesize; // you need to switch back to the previous page of the original last page, datapager itself does not process <br/> (datapager) listview1.findcontrol ("datapager1 ")). setpageproperties (startrowindex, pagesize, false); <br/> session ["startrowindex"] = startrowindex; <br/> session ["pagesize"] = pagesize; <br/>}< br/> dB. databind (listview1, "select * from exam order by ID"); // rebind data <br/>}< br/>

 

Modify. aspx. CS

Using system; <br/> using system. collections; <br/> using system. configuration; <br/> using system. data; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. htmlcontrols; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. XML. LINQ; </P> <p> using system. data. oledb; </P> <p> names Pace web0531 <br/>{< br/> Public partial class modify: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs e) <br/>{< br/> If (! Ispostback) <br/>{< br/> string urlid = request. querystring ["ID"]; // ID of the record to be modified <br/> // If (string. isnullorempty (urlid) urlid = "4"; // test with <br/> oledbconnection conn = dB. accessconnection (); <br/> Conn. open (); <br/> oledbcommand cmd = new oledbcommand ("select * from exam where id =" + urlid, Conn); <br/> oledbdatareader DR = cmd. executereader (commandbehavior. closeconnection); <br/> If (dr. read () <br/>{< br/> ID. value = Dr ["ID"]. tostring (); // display the original question Information <br/> TM. TEXT = Dr ["Question"]. tostring (); <br/>. TEXT = Dr ["A"]. tostring (); <br/> B. TEXT = Dr ["B"]. tostring (); <br/> C. TEXT = Dr ["C"]. tostring (); <br/> D. TEXT = Dr ["D"]. tostring (); <br/> int I = Dr ["Answer"]. tostring () [0]-'A'; // only process one choice. <br/> if (I> = 0 & I <= 3) Da. items [I]. selected = true; <br/>}< br/> Conn. close (); <br/> dr. close (); <br/>}</P> <p> // update record <br/> protected void button#click (Object sender, eventargs E) <br/>{< br/> string RDA = NULL; <br/> for (INT I = 0; I <da. items. count; ++ I) <br/>{< br/> If (DA. items [I]. selected) <br/>{< br/> RDA = convert. tostring (char) ('A' + I); // The Answer. Only one choice is allowed. <br/> break; <br/>}< br/> If (string. isnullorempty (RDA) RDA = ""; <br/> string SQL = "Update exam set question = '" + TM. text. replace ("'", "'' ") +"', a = '"+. text. replace ("'", "'' ") +"', B = '"+ B. text. replace ("'", "'' ") +"', c = '"+ C. text. replace ("'", "'' ") +"', D = '"+ D. text. replace ("'", "'' ") +"', answer = '"+ RDA +" 'where id = "+ id. value; </P> <p> oledbconnection conn = dB. accessconnection (); <br/> Conn. open (); <br/> oledbcommand cmd = new oledbcommand (SQL, Conn); <br/> cmd. executenonquery (); // execute update <br/> Conn. close (); <br/> response. redirect ("default. aspx "); // return to the original page <br/>}< br/>

 

New. aspx. CS

Using system; <br/> using system. collections; <br/> using system. configuration; <br/> using system. data; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. htmlcontrols; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. XML. LINQ; </P> <p> using system. data. oledb; </P> <p> namespace web0531 <br/> {<br/> Public partial class _ new: system. web. UI. page // Insert a new exam question <br/>{< br/> // Insert a new record <br/> protected void button#click (Object sender, eventargs E) <br/>{< br/> string RDA = NULL; <br/> for (INT I = 0; I <da. items. count; ++ I) // The selected answer <br/>{< br/> If (DA. items [I]. selected) <br/>{< br/> RDA = convert. tostring (char) ('A' + I); // convert to a string <br/> break; // only one choice <br/>}< br/> If (string. isnullorempty (RDA) RDA = ""; <br/> string SQL = "insert into exam (question, a, B, c, d, answer) values ('"+ TM. text. replace ("'", "'' ") +"', '"+. text. replace ("'", "'' ") +"', '"+ B. text. replace ("'", "'' ") +"', '"+ C. text. replace ("'", "'' ") +"', '"+ D. text. replace ("'", "'' ") +"', '"+ RDA +"') "; </P> <p> oledbconnection conn = dB. accessconnection (); <br/> Conn. open (); <br/> oledbcommand cmd = new oledbcommand (SQL, Conn); // insert to database <br/> cmd. executenonquery (); <br/> Conn. close (); <br/> response. redirect ("default. aspx "); // return to the original page, depending on the question just inserted, click the "Last page" button <br/>}< br/>

 

Download the complete project:

Http://download.csdn.net/source/2417259

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.