<%@ page language="c#" debug="true" %> <%@ assembly name="system.data" %> <%@ import namespace="system.data.ado" %> <%@ import namespace="system.data" %> <%@ import namespace="system" %> <html><head> <title>welcome to my forum!</title> <script language="c#" runat="server" > //execute this script when the page loads void page_load(object src, eventargs e) { //call the method to databind the datagrid binding() ; } //this method connects to the database, and databinds the database to the datagrid public void binding() { //string to connect to the database, if your database is in some other directory then change the path //to the database below string [email protected]"provider=microsoft.jet.oledb.4.0 ;data source="+server.mappath(".//db//board.mdb") ; //make a connection to the database adoconnection myconn = new adoconnection(strconn) ; //string to select records from the database. newpost table //i have used "order by postid desc" since i want to show the latest post on the top //if you remove this clause then the oldest message will be shown first string strcom = "select postid ,subject ,name ,replies ,views ,date from newpost order by postid desc" ; //open the connection, always remember to open the connection before doing anything else myconn.open(); dataset mydataset = new dataset(); //create a adodatasetcommand and a dataset adodatasetcommand mycommand =new adodatasetcommand(strcom,myconn); //fill the dataset mycommand.filldataset(mydataset,"newpost") ; //connection is closed myconn.close(); //set the dataview of the table "newpost" contained in the dataset for the datagrid datagrid1.datasource = mydataset.tables["newpost"].defaultview ; //databind the datagrid datagrid1.databind(); } //this method is called when the datagrid is paged (i.e. when you change from page 1 to page 2 etc.. ) public void datagrid_updt(object sender, datagridpagechangedeventargs e) { //call the method to databind binding(); } //this method is called when the form is submitted to make a new post public void submit_click(object sender, eventargs e) { //proceed only if all the required fields are filled-in if(page.isvalid&&name.text!=""&&subject.text!=""&&email.text!=""){ //get the current date and time datetime now = datetime.now ; errmess.text="" ; //i am building a custom query which will be used to call the postmessage.aspx page. //since it will be a query we have to encode the query into utf8 format. //so i get all the fields from the form and encode them into utf8 format string req = "name="+ system.web.httputility.urlencodetostring(name.text, system.text.encoding.utf8); req+="&&email="+ system.web.httputility.urlencodetostring(email.text, system.text.encoding.utf8); req+="&&subject="+ system.web.httputility.urlencodetostring(subject.text, system.text.encoding.utf8); //get the hostaddress of the author req+="&&ip="+ system.web.httputility.urlencodetostring(request.userhostaddress.tostring(), system.text.encoding.utf8); req+="&&date="+ system.web.httputility.urlencodetostring(now.tostring(), system.text.encoding.utf8); req+="&&message="+ system.web.httputility.urlencodetostring(message.text, system.text.encoding.utf8); //a 'yes' is used below to tell the postmessage page that this is a new topic post req+="&&newpost="+ system.web.httputility.urlencodetostring("yes", system.text.encoding.utf8); //call the postmessage.aspx page and append the query to it. page.navigate("postmessage.aspx?" + req); } else { errmess.text="fill in all the required fields before posting!<br>" ; } } </script> <link href="mystyle.css" type=text/css rel=stylesheet></head> <body topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0"> <!-- #include file="header.inc" --> <center> <asp:label id="errmess" text="" style="color:#ff0000" runat="server" /> <asp:label class=fodark text="<font color=#00000 >welcome to my discussion forum</font>" runat=server /> <br> <br> <form method="post" runat="server" id=form1> <%-- the datagrid settings. its very interesting how much you can play with it --%> <asp:datagrid id=datagrid1 runat="server" forecolor="black" pagerstyle-mode="numericpages" onpageindexchanged="datagrid_updt" pagesize="20" allowpaging="true" width="80%" autogeneratecolumns="false"> <property name="pagerstyle"> <asp:datagridpagerstyle backcolor="coral" mode="numericpages"> </asp:datagridpagerstyle> </property>
<property name="headerstyle"> <asp:tableitemstyle font-bold="true" forecolor="white" backcolor="coral"> </asp:tableitemstyle> </property> <%-- i am setting up the individual columns myself using templates --%> <property name="columns"> <%-- manipulate the subject entry so that it contains a link to the reply page --%> <asp:templatecolumn headertext="subject" itemstyle-width=50%> <template name="itemtemplate" > <asp:label runat="server" text='<%#"<a href=reply.aspx?postid="+databinder.eval(container, "dataitem.postid")+">" +databinder.eval(container, "dataitem.subject")+"</a>" %>' id=label2></asp:label> </template> </asp:templatecolumn>