Los Angeles, CA * Webmaster@ADefWebserver.com |
Move Add and Edit Items to a separate page
|
|
At the end of the last tutorial, the first page of the module resembles the image on the right. When the Add My Listing link is clicked, the add new item form appears. | |
In addition, when the Edit button is clicked, the item is editable inside the GridView. | |
In order to implement the requirements of this tutorial the following changes will be made:
|
|
Create the Edit Item Page |
|
In the Solution Explorer, right-click on the LinqThings4Sale folder (under the DesktopModules directory) and select Add New Item. | |
From the Add New Item box, select the Web User Control template, enter EditItem.ascx for the Name, select Visual C# for the Language, and check the box next to Place code in separate file. | |
EditItem.ascx will appear. | |
Open EditItem.ascx and switch to code view. Replace all the code with the following code: | |
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EditItem.ascx.cs" Inherits="DotNetNuke.Modules.LinqThings4Sale.EditItem" %> <asp:Panel ID="pnlBack" HorizontalAlign="Left" runat="server"> <asp:LinkButton ID="lnkBack" runat="server" OnClick="lnkBack_Click" CausesValidation="False">[Back]</asp:LinkButton> </asp:Panel> <asp:Panel ID="pnlEditItem" HorizontalAlign="Left" runat="server"> <asp:LinqDataSource ID="LDSEditItem" runat="server" ContextTypeName="LinqThings4Sale.LinqThings4SaleDataContext" EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="ThingsForSales" Where="ModuleId == @ModuleId && ID == @ID" OnInserted="LDSEditItem_Inserted" OnInserting="LDSEditItem_Inserting" OnSelecting="LDSEditItem_Selecting" OnUpdated="LDSEditItem_Updated" ondeleted="LDSEditItem_Deleted"> <WhereParameters> <asp:Parameter Name="ModuleId" Type="Int32" /> <asp:Parameter Name="ID" Type="Int32" /> </WhereParameters> </asp:LinqDataSource> <asp:LinqDataSource ID="LDSCategories" runat="server" ContextTypeName="LinqThings4Sale.LinqThings4SaleDataContext" TableName="ThingsForSale_Categories" OrderBy="Category"> </asp:LinqDataSource> <br /> <asp:FormView ID="FVItem" runat="server" DataKeyNames="ID" DataSourceID="LDSEditItem" DefaultMode="Insert"> <EditItemTemplate> Category: <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="LDSCategories" DataTextField="Category" DataValueField="Category" SelectedValue='<%# Bind("Category") %>'> <asp:ListItem>Home</asp:ListItem> </asp:DropDownList> Price: $ <asp:TextBox ID="PriceTextBox" runat="server" CausesValidation="True" EnableViewState="False" Text='<%# Bind("Price") %>' Width="56px"></asp:TextBox> <br /> <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="PriceTextBox" ErrorMessage="Price must be greater than 0" MaximumValue="99999" MinimumValue="1"></asp:RangeValidator> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="PriceTextBox" ErrorMessage="A price is required"></asp:RequiredFieldValidator> <br /> Description:<br /> <asp:TextBox ID="DescriptionTextBox" runat="server" EnableViewState="False" MaxLength="499" Text='<%# Bind("Description") %>' TextMode="MultiLine" Width="286px"></asp:TextBox> <br /> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DescriptionTextBox" ErrorMessage="Description is required."></asp:RequiredFieldValidator> <br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" /> <asp:LinkButton ID="UpdateDeleteButton" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('Are you certain you want to delete ?');" Text="Delete" /> <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" onclick="UpdateCancelButton_Click" /> </EditItemTemplate> <InsertItemTemplate> Category: <asp:DropDownList ID="DropDownList2" runat="server" DataSource='<%# Bind("Category") %>' DataSourceID="LDSCategories" DataTextField="Category" DataValueField="Category" SelectedValue='<%# Bind("Category") %>'> <asp:ListItem>Home</asp:ListItem> </asp:DropDownList> Price: $ <asp:TextBox ID="PriceTextBox" runat="server" CausesValidation="True" EnableViewState="False" Text='<%# Bind("Price") %>' Width="56px"></asp:TextBox> <br /> <asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="PriceTextBox" ErrorMessage="Price must be greater than 0" MaximumValue="99999" MinimumValue="1"></asp:RangeValidator> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="PriceTextBox" ErrorMessage="A price is required"></asp:RequiredFieldValidator> <br /> Description:<br /> <asp:TextBox ID="DescriptionTextBox" runat="server" EnableViewState="False" MaxLength="499" Text='<%# Bind("Description") %>' TextMode="MultiLine" Width="286px"></asp:TextBox> <br /> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="DescriptionTextBox" ErrorMessage="Description is required."></asp:RequiredFieldValidator> <br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert"></asp:LinkButton> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" OnClick="InsertCancelButton_Click" Text="Cancel"></asp:LinkButton> </InsertItemTemplate> </asp:FormView> <br /> </asp:Panel> |
|
Open EditItem.ascx.cs and replace all the code with the following code: | |
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using DotNetNuke; using DotNetNuke.Security; using LinqThings4Sale; using DotNetNuke.Common; namespace DotNetNuke.Modules.LinqThings4Sale { public partial class EditItem : DotNetNuke.Entities.Modules.PortalModuleBase { #region QueryString public int RecordID { get { if (Request.QueryString["RecordID"] == null) { return 0; } else { return Convert.ToInt16(Request.QueryString["RecordID"]); } } } #endregion protected void Page_Load(object sender, EventArgs e) { if (UserId == -1) { lnkBack.Text = "You must be logged in to add or edit a listing"; pnlEditItem.Visible = false; } else { if (RecordID == -1) { FVItem.ChangeMode(FormViewMode.Insert); } else { if (PortalSecurity.IsInRole("Administrators") || AllowedToEdit()) { FVItem.ChangeMode(FormViewMode.Edit); } else { lnkBack.Text = "You are not allowed to edit this listing"; pnlEditItem.Visible = false; } } } } private bool AllowedToEdit() { LinqThings4SaleDataContext LinqThings4SaleDataContext = new LinqThings4SaleDataContext(); var result = from item in LinqThings4SaleDataContext.ThingsForSales where item.ID == RecordID & item.UserID == UserId select item; return (result.Count() > 0); } #region LinqDataSource Events protected void LDSEditItem_Selecting(object sender, LinqDataSourceSelectEventArgs e) { e.WhereParameters["ModuleId"] = ModuleId; e.WhereParameters["ID"] = RecordID; } protected void LDSEditItem_Inserting(object sender, LinqDataSourceInsertEventArgs e) { ThingsForSale ThingsForSale = (ThingsForSale)e.NewObject; ThingsForSale.UserID = Entities.Users.UserController.GetCurrentUserInfo().UserID; ThingsForSale.ModuleId = ModuleId; } #endregion #region LinkButton Events protected void lnkBack_Click(object sender, EventArgs e) { Response.Redirect(Globals.NavigateURL(), true); } protected void LDSEditItem_Inserted(object sender, LinqDataSourceStatusEventArgs e) { Response.Redirect(Globals.NavigateURL(), true); } protected void LDSEditItem_Updated(object sender, LinqDataSourceStatusEventArgs e) { Response.Redirect(Globals.NavigateURL(), true); } protected void InsertCancelButton_Click(object sender, EventArgs e) { Response.Redirect(Globals.NavigateURL(), true); } protected void LDSEditItem_Deleted(object sender, LinqDataSourceStatusEventArgs e) { Response.Redirect(Globals.NavigateURL(), true); } protected void UpdateCancelButton_Click(object sender, EventArgs e) { Response.Redirect(Globals.NavigateURL(), true); } #endregion } } |
|
Save the page. From the Toolbar, select Build then Build Page. The page should build without errors. |
|
Configure the page in module configuration |
|
While logged into your DotNetNuke site as "host", in the web browser, from the menu bar select "Host". Then select "Module Definitions". |
|
Locate the LinqThings4Sale entry and click on the Edit link. | |
Click the Add Control link (at the bottom of the page). | |
Enter:
and click the Update link. |
|
On the Edit Module Definition page, enter 03.00.00 for the Version and click the Update link. | |
Create a link to Edit Item |
|
Navigate to the View.ascx page in the LinqThings4Sale folder (under the DesktopModules directory). | |
Open View.ascx and switch to code view. Replace all the code with the following code: | |
<% @ Control Language="C#" AutoEventWireup="true" CodeFile="View.ascx.cs" Inherits="DotNetNuke.Modules.LinqThings4Sale.View" %><p> <asp:LinkButton ID="lnkAddListing" runat="server" OnClick="lnkAddListing_Click">[Add My Listing]</asp:LinkButton> </p><p> </p> |
|
Open View.ascx.cs and switch to code view. Replace all the code with the following code: |
|
using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using DotNetNuke; using DotNetNuke.Security; using LinqThings4Sale; using DotNetNuke.Entities.Modules; using DotNetNuke.Common; namespace DotNetNuke.Modules.LinqThings4Sale { public partial class View : DotNetNuke.Entities.Modules.PortalModuleBase, Entities.Modules.IActionable { protected void Page_Load(object sender, EventArgs e) { if (UserId != -1) { lnkAddListing.Text = "[Add My Listing]"; lnkAddListing.Enabled = true; } else { lnkAddListing.Text = "You must be logged in to add a Listing"; lnkAddListing.Enabled = false; } } #region LinkButton Events protected void lnkAddListing_Click(object sender, EventArgs e) { ModuleController objModules = new ModuleController(); Response.Redirect(Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "EditItem", "mid=" + ModuleId.ToString(), "RecordID=-1")); } #endregion #region IActionable Members public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions { get { Entities.Modules.Actions.ModuleActionCollection Actions = new Entities.Modules.Actions.ModuleActionCollection(); Actions.Add(GetNextActionID(), "Edit Categories", Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false); return Actions; } } #endregion } } |
|
Save the page. From the Toolbar, select Build then Build Page. The page should build without errors. |
|
|
|
When you view the page in the web browser you will now see a [Add My Listing] link. |
|
When you click on the link you are taken to the Edit Item page where you can add a new item. The [Back] link will return you to the View.ascx page. | |
Adding the Edit Item page is now complete. | |
Back to: Creating a DotNetNuke® Module using LINQ to SQL For absolute beginners! Part 3
DotNetNuke® is a registered trademark of the DotNetNuke Corporation