Code Behind Page Page /DesktopModules/DiscussDetails.aspx.cs (C#)
1: using System;
2: using System.Collections;
3: using System.ComponentModel;
4: using System.Data;
5: using System.Drawing;
6: using System.Web;
7: using System.Web.UI;
8: using System.Web.UI.WebControls;
9: using System.Web.UI.HtmlControls;
10: using System.Data.SqlClient;
11:
12: namespace ASPNetPortal {
13:
14:     public class DiscussDetails : System.Web.UI.Page {
15:         protected System.Web.UI.WebControls.LinkButton ReplyBtn;
16:         protected System.Web.UI.WebControls.Panel ButtonPanel;
17:         protected System.Web.UI.WebControls.TextBox TitleField;
18:         protected System.Web.UI.WebControls.TextBox BodyField;
19:         protected System.Web.UI.WebControls.LinkButton updateButton;
20:         protected System.Web.UI.WebControls.LinkButton cancelButton;
21:         protected System.Web.UI.WebControls.Panel EditPanel;
22:         protected System.Web.UI.WebControls.Label Title;
23:         protected System.Web.UI.WebControls.Label CreatedByUser;
24:         protected System.Web.UI.WebControls.Label CreatedDate;
25:         protected System.Web.UI.WebControls.Label Body;
26:         protected System.Web.UI.HtmlControls.HtmlAnchor prevItem;
27:         protected System.Web.UI.HtmlControls.HtmlAnchor nextItem;
28:     
29:
30:         int moduleId = 0;
31:         int itemId = 0;
32:
33:         //*******************************************************  
34:         //  
35:         // The Page_Load server event handler on this page is used  
36:         // to obtain the ModuleId and ItemId of the discussion list,  
37:         // and to then display the message contents.  
38:         //  
39:         //*******************************************************  
40:
41:         private void Page_Load(object sender, System.EventArgs e) {
42:
43:             // Obtain moduleId and ItemId from QueryString  
44:             moduleId = Int32.Parse(Request.Params["Mid"]);
45:         
46:             if (Request.Params["ItemId"] != null) {
47:                 itemId = Int32.Parse(Request.Params["ItemId"]);
48:             }
49:             else {
50:                 itemId = 0;
51:                 EditPanel.Visible = true;
52:                 ButtonPanel.Visible = false;
53:             }
54:
55:             // Populate message contents if this is the first visit to the page  
56:             if (Page.IsPostBack == false) {
57:                 BindData();
58:             }
59:
60:             if (PortalSecurity.HasEditPermissions(moduleId) == false) {
61:         
62:                 if (itemId == 0) {
63:                     Response.Redirect("~/Admin/EditAccessDenied.aspx");
64:                 }
65:                 else {
66:                     ReplyBtn.Visible=false;
67:                 }
68:             }
69:         }
70:
71:         //*******************************************************  
72:         //  
73:         // The ReplyBtn_Click server event handler on this page is used  
74:         // to handle the scenario where a user clicks the message's  
75:         // "Reply" button to perform a post.  
76:         //  
77:         //*******************************************************  
78:
79:         void ReplyBtn_Click(Object Sender, EventArgs e) {
80:
81:             EditPanel.Visible = true;
82:             ButtonPanel.Visible = false;
83:         }
84:
85:         //*******************************************************  
86:         //  
87:         // The UpdateBtn_Click server event handler on this page is used  
88:         // to handle the scenario where a user clicks the "update"  
89:         // button after entering a response to a message post.  
90:         //  
91:         //*******************************************************  
92:
93:         void UpdateBtn_Click(Object sender, EventArgs e) {
94:
95:             // Create new discussion database component  
96:             DiscussionDB discuss = new DiscussionDB();
97:
98:             // Add new message (updating the "itemId" on the page)  
99:             itemId = discuss.AddMessage(moduleId, itemId, User.Identity.Name, Server.HtmlEncode(TitleField.Text), Server.HtmlEncode(BodyField.Text));
100:
101:             // Update visibility of page elements  
102:             EditPanel.Visible = false;
103:             ButtonPanel.Visible = true;
104:
105:             // Repopulate page contents with new message  
106:             BindData();
107:         }
108:
109:         //*******************************************************  
110:         //  
111:         // The CancelBtn_Click server event handler on this page is used  
112:         // to handle the scenario where a user clicks the "cancel"  
113:         // button to discard a message post and toggle out of  
114:         // edit mode.  
115:         //  
116:         //*******************************************************  
117:
118:         void CancelBtn_Click(Object sender, EventArgs e) {
119:
120:             // Update visibility of page elements  
121:             EditPanel.Visible = false;
122:             ButtonPanel.Visible = true;
123:         }
124:
125:         //*******************************************************  
126:         //  
127:         // The BindData method is used to obtain details of a message  
128:         // from the Discussion table, and update the page with  
129:         // the message content.  
130:         //  
131:         //*******************************************************  
132:
133:         void BindData() {
134:
135:             // Obtain the selected item from the Discussion table  
136:             ASPNetPortal.DiscussionDB discuss = new ASPNetPortal.DiscussionDB();
137:             SqlDataReader dr = discuss.GetSingleMessage(itemId);
138:         
139:             // Load first row from database  
140:             dr.Read();
141:
142:             // Update labels with message contents  
143:             Title.Text = (String) dr["Title"];
144:             Body.Text = (String) dr["Body"];
145:             CreatedByUser.Text = (String) dr["CreatedByUser"];
146:             CreatedDate.Text = String.Format("{0:d}", dr["CreatedDate"]);
147:             TitleField.Text = ReTitle(Title.Text);
148:
149:             int prevId = 0;
150:             int nextId = 0;
151:
152:             // Update next and preview links  
153:             object id1 = dr["PrevMessageID"];
154:         
155:             if (id1 != DBNull.Value) {
156:                 prevId = (int) id1;
157:                 prevItem.HRef = Request.Path + "?ItemId=" + prevId + "&mid=" + moduleId;
158:             }
159:
160:             object id2 = dr["NextMessageID"];
161:         
162:             if (id2 != DBNull.Value) {
163:                 nextId = (int) id2;
164:                 nextItem.HRef = Request.Path + "?ItemId=" + nextId + "&mid=" + moduleId;
165:             }
166:         
167:             // close the datareader  
168:             dr.Close();
169:         
170:             // Show/Hide Next/Prev Button depending on whether there is a next/prev message  
171:             if (prevId <= 0) {
172:                 prevItem.Visible = false;
173:             }
174:
175:             if (nextId <= 0) {
176:                 nextItem.Visible = false;
177:             }
178:         }
179:
180:         //*******************************************************  
181:         //  
182:         // The ReTitle helper method is used to create the subject  
183:         // line of a response post to a message.  
184:         //  
185:         //*******************************************************  
186:
187:         String ReTitle(String title) {
188:
189:             if (title.Length > 0 & title.IndexOf("Re: ",0) == -1) {
190:                 title = "Re: " + title;
191:             }
192:
193:             return title;
194:         }
195:         
196:         public DiscussDetails() {
197:             Page.Init += new System.EventHandler(Page_Init);
198:         }
199:
200:         private void Page_Init(object sender, EventArgs e) {
201:             //  
202:             // CODEGEN: This call is required by the ASP.NET Web Form Designer.  
203:             //  
204:             InitializeComponent();
205:         }
206:
207:         #region Web Form Designer generated code
208:         /// <summary>  
209:         /// Required method for Designer support - do not modify  
210:         /// the contents of this method with the code editor.  
211:         /// </summary>  
212:         private void InitializeComponent() {    
213:             this.ReplyBtn.Click += new System.EventHandler(this.ReplyBtn_Click);
214:             this.updateButton.Click += new System.EventHandler(this.UpdateBtn_Click);
215:             this.cancelButton.Click += new System.EventHandler(this.CancelBtn_Click);
216:             this.Load += new System.EventHandler(this.Page_Load);
217:
218:         }
219:         #endregion
220:     }
221: }