tutorial .NET – p8 – edit in ASP.NET

 

Now we will edit the
Publisher objects .

Add a new WebForm ,
name it frmPublisher_Insert.aspx and make sure the “Place code in separate
file” and “Select master page” are both selected by default.

Change in source view
the title from “
Untitled Page” to “Insert Publisher

Now we must put
controls in place to insert publishers

There must be the name
and site of the publisher.

I prefer enter a table
for this and the page will look like this:

 

<%@ Page Language=”C#” MasterPageFile=”~/Book.master” AutoEventWireup=”true” CodeFile=”frmPublisher_Insert.aspx.cs” Inherits=”frmPublisher_Insert”
Title=”Insert
Publisher”
%>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ Runat=”Server”>

    <table>

        <tr>

            <td colspan=”2″>Enter values

            </td>

        </tr>

        <tr>

            <td>

               Name

            </td>

            <td>

                <asp:TextBox ID=”txtName” runat=”server”>

                </asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

               Site

            </td>

            <td>

                <asp:TextBox ID=”txtSite” runat=”server”>

                </asp:TextBox>

            </td>

        </tr>

        <tr>

            <td><asp:Button ID=”btnSave” Text=”Insert” runat=”server” />

            </td>

            <td><asp:Button ID=”btnCancel” Text=”Cancel” runat=”server” />

            </td>

        </tr>

    </table>

</asp:Content>

 

Now switch to design
view and double click on Insert button in order to generate Click event. Double
click in solution explorer the frmPublisher_Insert.aspx and , in Design view,
double click on Cancel button in order to generate Click event.

For cancel it is clear
what we must do – redirect to the
frmPublisherList.aspx

Response.Redirect(“frmPublisherList.aspx”, false);

For save button, we
must create a new publisher and save

  Publisher p
= new Publisher();

        p.Name = txtName.Text;

        p.Site = txtSite.Text;

        p.Insert();

       
Response.Redirect(“frmPublisherList.aspx”,
false);

Please try it by
setting the frmPublisher_Insert.aspx as start page and run the project (F5)

If all works well
(please ensure that Insert has a call to Save()) you will see in the frmPublisherList.aspx
the item you just selected.

It is clear that frmPublisherList.aspx
has a need for a new button . Let’s put it

<asp:Button ID=”btnNew” runat=”server”
Text=”New”
OnClick=”btnNew_Click”
/>

And on code:

protected void
btnNew_Click(object sender, EventArgs e)

    {

        Response.Redirect(“frmPublisher_Insert.aspx”, false);

    }

That will be ok for
adding a new publisher.

Now for editing and
deleting we can make on list… but I prefer having 2 new pages.

So modify a little bit
the code on the grid, in order to have the edit and delete operations : the
edit will be a link , and the delete will be a button to see how different is
the model on the two implementations.

The list page now
looks like this:

<%@ Page Language=”C#” MasterPageFile=”~/Book.master” AutoEventWireup=”true” CodeFile=”frmPublisherList.aspx.cs” Inherits=”frmPublisherList”
Title=”Publisher
Lists”
%>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ Runat=”Server”>

    <asp:GridView ID=”grdPublisher” runat=”server” AutoGenerateColumns=”false”>

        <Columns>

            <asp:BoundField DataField=”Site” HeaderText=”Site” />

            <asp:BoundField DataField=”Name” HeaderText=”Name” />

            <asp:TemplateField HeaderText=”Operations”>

                <ItemTemplate>

                    <asp:Button runat=”server” ID=”btnDelete” CommandName=”deletepub” CommandArgument=’<%#
Eval(“IDPublisher”) %> Text=”Delete” />

                    <asp:HyperLink runat=”server” ID=”hkEdit” NavigateUrl=’<%#
Eval(“IDPublisher”,”~/frmPublisher_Edit.aspx?ID={0}”) %> Text=”Edit”></asp:HyperLink>

                </ItemTemplate>

            </asp:TemplateField>

        </Columns>

   

    </asp:GridView>

    <br />

    <asp:Button ID=”btnNew” runat=”server” Text=”New” OnClick=”btnNew_Click” />

</asp:Content>

 

The link hkEdit is
self explanatory – it goes to the
frmPublisher_Edit.aspx with the ID of publisher in the row.

For the button we must
create the event – and the event is on the grid itself – is the
RowCommand

On the .cs file:

    protected void grdPublisher_RowCommand(object
sender, GridViewCommandEventArgs e)

    {

        switch(e.CommandName)

        {

            case
deletepub:

                int
idPublisher;

                if(int.TryParse(e.CommandArgument.ToString(),out idPublisher))

                {

                    Response.Redirect(“frmPublisher_Delete.aspx?ID=”+
idPublisher, false);

                    return;

                }

                Response.Write(“Can not find id:” + idPublisher);

                break;

            default:

                Response.Write(“Do not know command : “ +
e.CommandName);

                break;

        }

    }

 

Now create the two
pages
frmPublisher_Delete and frmPublisher_Edit 

On both we will copy
the table from the new page and the source – without the class declaration. One
more thing is to retrieve from ID the editing publisher:

 

  int
idPublisher;

        if(!int.TryParse(Request.QueryString[“ID”],out
idPublisher))

        {

            Response.Redirect(“frmPublisherList.aspx”, false);

            return;

        }

        //we have id of the publisher

 

How can we retrieve
from the ID of the publisher  the object
? Remember that in Windows forms application we did pass from one form to
another the publisher object. Here we have just the Id. For this, we will open
again the Book.sln solution and add the method to load one single object.

I like to put the
method on ColPublisher and make it static … to not apparently create a new
object.

 

public static
Publisher sLoadFromID(int
ID)

        {

            DbConnection
db = Settings.TheConnection;

            using
(db)

            {

                db.Open();

                IDataReader
ir = Settings.Load(“select
IDPublisher, NamePublisher, SitePublisher from Publisher where
IDPublisher=”
+ ID, db);

                while
(ir.Read())

                {

                    Publisher
p = new Publisher();

                    p.FillObject(ir);

                    return
p;

                }

            }

            return
null;

        }

 

Compile and go to Web
project.We can have the publisher:

//we have id of the publisher

        Publisher
p = ColPublisher.sLoadFromID(idPublisher);

        if (p
== null)//maybe
someone deleted

        {

            Response.Redirect(“frmPublisherList.aspx”, false);

            return;

        }

        if
(!IsPostBack)

        {

            //now
fill the text boxes

            txtName.Text = p.Name;

            txtSite.Text = p.Site;

        }

 

Why we have put  (!IsPostBack )
? Simply because the textboxes must be filled only once – the first time. When
the user enter new name and/or new site and after clicks on save, we must
preserve his data .Other problem is that when we have to save the data, we must
have the same code to load the publisher – so we put this into a function into
the page:

private Publisher
pub

    {

        get

        {

            int
idPublisher;

            if
(!int.TryParse(Request.QueryString[“ID”], out
idPublisher))

            {

               

                return
null;

            }

            //we have
id of the publisher

            return
ColPublisher.sLoadFromID(idPublisher); ;

           

        }

    }

 

The code on PageLoad
will be now shorter :

protected void
Page_Load(object sender, EventArgs e)

    {

        Publisher
p = pub;

        if (p
== null)

        {

            Response.Redirect(“frmPublisherList.aspx”, false);

            return;

        }

 

        if
(!IsPostBack)

        {

            //now
fill the text boxes

            txtName.Text = p.Name;

            txtSite.Text = p.Site;

        }

    }

 

And we must modify the
code on saving also :

protected void
btnSave_Click(object sender, EventArgs e)

    {

        Publisher
p = pub;

        if (p
== null)

        {

            //TODO :
throw an exception that someone deleted the publisher

            Response.Redirect(“frmPublisherList.aspx”,
false);

            return;

 

        }

 

        p.Name = txtName.Text;

        p.Site = txtSite.Text;

        p.Update();

        Response.Redirect(“frmPublisherList.aspx”, false);

    }

 

 

You can modify also
the text of btnsave from “Insert” to “Save”

On the delete page we
will put the same code to retrieve the Publisher .Here is the code:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using BookObjects;

 

public partial
class frmPublisher_Delete
: System.Web.UI.Page

{

    private Publisher pub

    {

        get

        {

            int
idPublisher;

            if
(!int.TryParse(Request.QueryString[“ID”], out
idPublisher))

            {

 

                return
null;

            }

            //we have
id of the publisher

            return
ColPublisher.sLoadFromID(idPublisher); ;

 

        }

    }

    protected void Page_Load(object
sender, EventArgs e)

    {

        Publisher
p = pub;

        if (p
== null)

        {

            Response.Redirect(“frmPublisherList.aspx”, false);

            return;

        }

 

        if
(!IsPostBack)

        {

            //now
fill the text boxes

            txtName.Text = p.Name;

            txtSite.Text = p.Site;

        }

    }

    protected void btnSave_Click(object
sender, EventArgs e)

    {

        Publisher
p = pub;

       

        if(p !=
null)

            p.Delete();

 

        Response.Redirect(“frmPublisherList.aspx”, false);

    }

    protected void btnCancel_Click(object
sender, EventArgs e)

    {

        Response.Redirect(“frmPublisherList.aspx”, false);

    }

}

 

 

Do not forget to
change the text “Insert” of btnSave to “Delete”. You also can put readonly
property to true on the textboxes

<%@ Page Language=”C#” MasterPageFile=”~/Book.master” AutoEventWireup=”true” CodeFile=”frmPublisher_Delete.aspx.cs” Inherits=”frmPublisher_Delete”
Title=”Untitled
Page”
%>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ Runat=”Server”>

    <table>

        <tr>

            <td colspan=”2″>Enter values

            </td>

        </tr>

        <tr>

            <td>

               Name

            </td>

            <td>

                <asp:TextBox ID=”txtName” runat=”server” ReadOnly=”true”>

                </asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

               Site

            </td>

            <td>

                <asp:TextBox ID=”txtSite” runat=”server” ReadOnly=”true”>

                </asp:TextBox>

            </td>

        </tr>

        <tr>

            <td><asp:Button ID=”btnSave” Text=”Delete” runat=”server” OnClick=”btnSave_Click” />

            </td>

            <td><asp:Button ID=”btnCancel” Text=”Cancel” runat=”server” OnClick=”btnCancel_Click” />

            </td>

        </tr>

    </table>

</asp:Content>

 

 

Next time we will put
some modifications to the site: a site map(in order to can have indications for
the user where he is)  and code to change
and load resources in English and French languages at run time.

 

Leave a Reply

Your email address will not be published. Required fields are marked *