Create the Code Behind
|
|
The View.ascx file should now resemble
the image on the right. A few code behind methods are now required to complete the module. |
|
Code Behind for the LinqDataSource Control |
|
We want to alter the behavior of the LinqDataSource control so that it only shows the records for this particular instance of the module. In addition, when inserting a record we want to insert the current ModuleId and the current UserID. | |
Right-click on the LinqDataSource control and select Properties. The properties will show up in the Properties window (if it doesn't, switch to source view and click on "<asp:LinqDataSource"). |
|
Click on the yellow "lighting bolt" to switch to the "events" for the control |
|
On the Inserted row type LinqDataSource1_Inserted and click away (or you can just double-click in the box and the name will be inserted for you). |
|
A method will be automatically "wired-up". |
|
Add code so the method reads: | |
protected void LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e) { this.GridView1.DataBind(); } |
|
(this method instructs the GridView to refresh itself after a record has been inserted) | |
On the Inserting row type LinqDataSource1_Inserting and click away. |
|
Add code so the method reads: | |
protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e) { ThingsForSale ThingsForSale = (ThingsForSale)e.NewObject; ThingsForSale.UserID = Entities.Users.UserController.GetCurrentUserInfo().UserID; ThingsForSale.ModuleId = ModuleId; } |
|
(this method casts "e" which is an instance of the object containing the data about to be inserted, as a ThingsForSale object. It then sets the UserID and the ModuleId. ) | |
On the Selecting row type LinqDataSource1_Selecting and click away. |
|
Add code so the method reads: | |
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e) { e.WhereParameters["ModuleId"] = ModuleId; } |
|
(this method passes the current ModuleId
to the LinqDataSource control. You will recall that a
where clause was defined to expect a ModuleId to be
passed.) |
|
Code Behind for the Add My Listing link |
|
Double-click on the Add My Listing link. | |
Add code so the method reads: | |
protected void LinkButton1_Click(object sender, EventArgs e) { this.FormView1.Visible = true; this.GridView1.DataBind(); } |
|
(this method makes the entry form visible. ) |
|
Code Behind for the GridView |
|
Right-click on the GridView control
and select Properties. The properties will show up in the
Properties
window (if it doesn't, switch to source view and click on
"<asp:GridView"). Switch to events and enter GridView1_RowDataBound for the RowDataBound row and click away. |
|
Add code so the method reads: | |
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if ((e.Row.RowType == DataControlRowType.DataRow)) { ThingsForSale ThingsForSale = ((ThingsForSale)(e.Row.DataItem)); if ((PortalSecurity.IsInRole("Administrators")) || (Entities.Users.UserController.GetCurrentUserInfo().UserID == (int)ThingsForSale.UserID)) { e.Row.Cells[0].Enabled = true; } else { e.Row.Cells[0].Text = " "; } } } |
|
(this method casts "e" which is an instance of the
object that contains the data for the current row, as a ThingsForSale
object. It then compares the UserID to the UserID of the current user.
If the UserID matches the current user or the current user is an
administrator it enables the first column on the GridView (this allows a
user to edit the row)). |
|
Add Additional Methods to the Code behind |
|
Add these two methods to the code behind: | |
protected void InsertButton_Click(object
sender, EventArgs e) { this.FormView1.Visible = false; LinkButton1.Text = "Update Successful - Add Another Listing"; this.GridView1.DataBind(); } protected void InsertCancelButton_Click(object sender, EventArgs e) { this.FormView1.Visible = false; this.GridView1.DataBind(); } |
|
(The events for these two methods was created when the code was pasted in the earlier step) | |
Alter the Page_Load method in the code behind so it reads: |
|
protected void Page_Load(object
sender, EventArgs e) { if ((PortalSecurity.IsInRole("Registered Users") || PortalSecurity.IsInRole("Administrators"))) { LinkButton1.Enabled = true; } else { LinkButton1.Text = "You must be logged in to add a Listing"; LinkButton1.Enabled = false; } } |
|
(This code determines if the user is logged in and
displays the Add Listing link if they are). Save the file. From the Toolbar, select Build then Build Page. The page should build without errors. |
|
BACK
|
Next: Create the module definition |
Buy DotNetNuke Modules from Snowcovered |
|
(C) by Michael Washington - ADefWebserver.com - Webmaster@ADefWebserver.comDotNetNuke® is a registered trademark of the DotNetNuke Corporation |