当前位置: 移动技术网 > IT编程>开发语言>.net > 网上购物系统(Task008)——FormView编辑更新商品详细信息

网上购物系统(Task008)——FormView编辑更新商品详细信息

2018年09月21日  | 移动技术网IT编程  | 我要评论

北京科技产业博览会,横山县公安局,2017年国家公务员考试报名

一、前期准备

编辑商品信息属于管理功能,管理功能的网页最好单独放在一个文件夹中,为此,做一些准备工作:

1、修改母版页中的路径为绝对路径

2、复制controls中的itemdetailscontrol.ascx,改名为itemmanagecontrol.ascx

3、在itemmanagecontrol.ascx中的formview的itemplate模板中添加三个linkbutton按钮,编辑、新建、删除。设置一下单元格右对齐,会美观一点。

4、临时在itemdetails.x中添加代码:

response.redirect("manager/itemmanage.aspx" + request.url.query);

5、web中新建文件夹manager,并添加itemmanage.aspx,引用母版页

二、编辑edititemtemplate模板,可直接将模板itemtemplate复制过来进行修改,添加必要的文本框、下拉列表框,模板代码如下:

(注意:<%@outputcacheduration="100000"varybyparam="page;categoryid"%>页面缓存要去掉,否则,嘿嘿嘿)

[html]  <edititemtemplate> 
    <table cellpadding="0" cellspacing="0"> 
        <tr> 
            <td height="18" width="18"> 
            </td> 
            <td height="18" width="96"> 
            </td> 
            <td height="18" width="18"> 
            </td> 
            <td height="18" width="96"> 
            </td> 
            <td height="18" width="180"> 
            </td> 
        </tr> 
        <tr> 
            <td style="height: 18px" width="18"> 
            </td> 
            <td rowspan="8" width="96"> 
                <asp:image id="imgitem" runat="server" alternatetext='<%# eval("name") %>' height="144" 
                    imageurl='<%# eval("image") %>' width="144" /></td> 
            <td style="height: 18px" width="18"> 
            </td> 
            <td colspan="2" style="height: 18px"> 
                <asp:fileupload id="fupimage" runat="server" width="80%" /> 
                <asp:button id="btnupload" runat="server" onclick="btnupload_click" text="上传" /></td> 
        </tr> 
        <tr> 
            <td width="18"> 
            </td> 
            <td width="18"> 
            </td> 
            <td width="96"> 
                商品类别:</td> 
            <td width="180"> 
                <asp:dropdownlist id="ddlcategories" runat="server"  
                autopostback="true"  
                onselectedindexchanged="ddlcategories_selectedindexchanged"> 
                </asp:dropdownlist> 
            </td> 
        </tr> 
        <tr> 
            <td width="18"> 
            </td> 
            <td width="18"> 
            </td> 
            <td width="96"> 
                商品名称:</td> 
            <td width="180"> 
                <asp:textbox id="txtname" runat="server" text='<%# bind("name") %>'></asp:textbox> 
            </td> 
        </tr> 
        <tr> 
            <td width="18"> 
            </td> 
            <td width="18"> 
            </td> 
            <td width="96"> 
                商品价格:</td> 
            <td width="180"> 
                <asp:textbox id="txtprice" runat="server" text='<%# bind("price") %>'></asp:textbox> 
            </td> 
        </tr> 
        <tr> 
            <td width="18"> 
            </td> 
            <td width="18"> 
            </td> 
            <td width="96"> 
                商品描述:</td> 
            <td width="180"> 
                <asp:textbox id="txtdescn" runat="server" text='<%# bind("descn") %>'></asp:textbox> 
            </td> 
        </tr> 
        <tr> 
            <td width="18"> 
            </td> 
            <td width="18"> 
            </td> 
            <td width="96"> 
                供应时间:</td> 
            <td width="180"> 
                <asp:textbox id="txtsupplytime" runat="server" text='<%# bind("supplytime") %>'></asp:textbox> 
            </td> 
        </tr> 
        <tr> 
            <td width="18"> 
            </td> 
            <td width="18"> 
            </td> 
            <td width="96"> 
                供应日期:</td> 
            <td width="180"> 
               <asp:textbox id="txtsupplydate" runat="server" text='<%# bind("supplydate") %>'></asp:textbox> 
            </td> 
        </tr> 
        <tr> 
            <td width="18"> 
            </td> 
            <td width="18"> 
            </td> 
            <td width="96"> 
                供应地区:</td> 
            <td width="180"> 
                <asp:textbox id="txtsupplyarea" runat="server" text='<%# bind("supplyarea") %>'></asp:textbox> 
            </td> 
        </tr> 
        <tr> 
            <td height="18" width="18"> 
            </td> 
            <td height="18" width="96"> 
            </td> 
            <td height="18" width="18"> 
            </td> 
            <td height="18" width="96"> 
            </td> 
            <td height="18" width="180" align="right"> 
                <asp:linkbutton id="lbtndelete" runat="server" commandname="update" text="更新" /> 
                <asp:linkbutton id="lbtnnew" runat="server" commandname="cancel" text="取消" /> 
            </td> 
        </tr> 
    </table> 
</edititemtemplate> 
    <edititemtemplate>
        <table cellpadding="0" cellspacing="0">
            <tr>
                <td height="18" width="18">
                </td>
                <td height="18" width="96">
                </td>
                <td height="18" width="18">
                </td>
                <td height="18" width="96">
                </td>
                <td height="18" width="180">
                </td>
            </tr>
            <tr>
                <td style="height: 18px" width="18">
                </td>
                <td rowspan="8" width="96">
                    <asp:image id="imgitem" runat="server" alternatetext='<%# eval("name") %>' height="144"
                        imageurl='<%# eval("image") %>' width="144" /></td>
                <td style="height: 18px" width="18">
                </td>
                <td colspan="2" style="height: 18px">
                    <asp:fileupload id="fupimage" runat="server" width="80%" />
                    <asp:button id="btnupload" runat="server" onclick="btnupload_click" text="上传" /></td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    商品类别:</td>
                <td width="180">
                    <asp:dropdownlist id="ddlcategories" runat="server"
                    autopostback="true"
                    onselectedindexchanged="ddlcategories_selectedindexchanged">
                    </asp:dropdownlist>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    商品名称:</td>
                <td width="180">
                    <asp:textbox id="txtname" runat="server" text='<%# bind("name") %>'></asp:textbox>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    商品价格:</td>
                <td width="180">
                    <asp:textbox id="txtprice" runat="server" text='<%# bind("price") %>'></asp:textbox>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    商品描述:</td>
                <td width="180">
                    <asp:textbox id="txtdescn" runat="server" text='<%# bind("descn") %>'></asp:textbox>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    供应时间:</td>
                <td width="180">
                    <asp:textbox id="txtsupplytime" runat="server" text='<%# bind("supplytime") %>'></asp:textbox>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    供应日期:</td>
                <td width="180">
                   <asp:textbox id="txtsupplydate" runat="server" text='<%# bind("supplydate") %>'></asp:textbox>
                </td>
            </tr>
            <tr>
                <td width="18">
                </td>
                <td width="18">
                </td>
                <td width="96">
                    供应地区:</td>
                <td width="180">
                    <asp:textbox id="txtsupplyarea" runat="server" text='<%# bind("supplyarea") %>'></asp:textbox>
                </td>
            </tr>
            <tr>
                <td height="18" width="18">
                </td>
                <td height="18" width="96">
                </td>
                <td height="18" width="18">
                </td>
                <td height="18" width="96">
                </td>
                <td height="18" width="180" align="right">
                    <asp:linkbutton id="lbtndelete" runat="server" commandname="update" text="更新" />
                    <asp:linkbutton id="lbtnnew" runat="server" commandname="cancel" text="取消" />
                </td>
            </tr>
        </table>
    </edititemtemplate>
 

三、itemmanagecontrol.ascx的后台代码:

代码页的主任是读取前台窗体的值,主要是fileupload控件和dropdownlist控件的值,两个值,分别使用了viewstate存储了信息。读取数据的时候,注意,如果用户没有做修改,刚使用原来的值,这个值放在了privatestaticilist<itemdetails> itemdetails =newlist<itemdetails>();中。

[csharp] using system; 
using system.web; 
using system.web.ui.webcontrols; 
using system.collections.generic; 
 
using westgarden.dal; 
using westgarden.model; 
 
namespace westgarden.web 

    public partial class itemmanagecontrol : system.web.ui.usercontrol 
    { 
        private static ilist<itemdetails> itemdetails = new list<itemdetails>(); 
 
        protected void page_load(object sender, eventargs e) 
        { 
            if (!ispostback) 
            { 
                bindformview(); 
                viewstate["imageurl"] = null; 
                viewstate["selectedcategoryid"] = null; 
            } 
        } 
 
        protected void fvwitemdetails_modechanging(object sender, formviewmodeeventargs e) 
        { 
            switch (e.newmode) 
            { 
                case formviewmode.edit: 
                    this.fvwitemdetails.changemode(formviewmode.edit); 
                    bindformview(); 
                    dropdownlist ddl = (dropdownlist)fvwitemdetails.findcontrol("ddlcategories"); 
                    binddropdownlist(ddl); 
                    break; 
                case formviewmode.readonly: 
                    this.fvwitemdetails.changemode(formviewmode.readonly); 
                    bindformview(); 
                    break; 
                default: 
                    break; 
            } 
        } 
        protected void fvwitemdetails_prerender(object sender, eventargs e) 
        { 
        } 
 
        protected void ddlcategories_selectedindexchanged(object sender, eventargs e) 
        { 
            dropdownlist ddl = (dropdownlist)fvwitemdetails.findcontrol("ddlcategories"); 
            viewstate["selectedcategoryid"] = ddl.selectedvalue; 
 
        } 
 
        protected void btnupload_click(object sender, eventargs e) 
        { 
            fileupload fup = (fileupload)fvwitemdetails.findcontrol("fupimage"); 
 
            if (fup.hasfile) 
            { 
                fup.saveas(server.mappath("~/images/items/") + fup.filename); 
 
                image img = (image)fvwitemdetails.findcontrol("imgitem"); 
                img.imageurl = "~/images/items/" + fup.filename.tostring(); 
                viewstate["imageurl"] = "~/images/items/" + fup.filename.tostring(); 
            } 
            else 
            { 
                response.write("<script>alert('请先浏览并选择图片')</script>"); 
            } 
        } 
 
        protected void fvwitemdetails_itemupdating(object sender, formviewupdateeventargs e) 
        { 
            if (viewstate["imageurl"] != null) 
            { 
                itemdetails[0].image = viewstate["imageurl"].tostring(); 
            } 
 
            if (viewstate["selectedcategoryid"] != null) 
            { 
 
                dropdownlist ddl = (dropdownlist)fvwitemdetails.findcontrol("ddlcategories"); 
                itemdetails[0].categoryid = viewstate["selectedcategoryid"].tostring(); 
            } 
 
            textbox txtname = (textbox)fvwitemdetails.findcontrol("txtname"); 
            itemdetails[0].name = txtname.text; 
 
            textbox txtprice = (textbox)fvwitemdetails.findcontrol("txtprice"); 
            itemdetails[0].price = decimal.parse(txtprice.text); 
 
            textbox txtdescn = (textbox)fvwitemdetails.findcontrol("txtdescn"); 
            itemdetails[0].descn = txtdescn.text; 
 
            textbox txtsupplytime = (textbox)fvwitemdetails.findcontrol("txtsupplytime"); 
            itemdetails[0].supplytime = txtsupplytime.text; 
 
            textbox txtsupplydate = (textbox)fvwitemdetails.findcontrol("txtsupplydate"); 
            itemdetails[0].supplydate = txtsupplydate.text; 
 
            textbox txtsupplyarea = (textbox)fvwitemdetails.findcontrol("txtsupplyarea"); 
            itemdetails[0].supplyarea = txtsupplyarea.text; 
 
 
            item item = new item(); 
            item.updateitem(itemdetails[0]); 
 
            fvwitemdetails.changemode(formviewmode.readonly); 
 
            bindformview(); 
            viewstate["imageurl"] = null; 
            viewstate["selectedcategoryid"] = null; 
        } 
 
 
        private void bindformview() 
        { 
            int itemkey = int.parse(request.querystring["itemid"]); 
            item item = new item(); 
            itemdetails = item.getitemdetailsbyitemid(itemkey); 
            fvwitemdetails.datasource = itemdetails; 
            fvwitemdetails.databind(); 
        } 
 
        private void binddropdownlist(dropdownlist ddl) 
        { 
            ddl.datasource = new category().getcategories(); 
            ddl.datatextfield = "name"; 
            ddl.datavaluefield = "categoryid"; 
            ddl.databind(); 
 
            string selectcategory = request.querystring["categoryid"].tostring(); 
            if (selectcategory != null) 
            { 
                listitem selecteditem = ddl.items.findbyvalue(selectcategory); 
                if (selecteditem != null) 
                    selecteditem.selected = true; 
            } 
        } 
    } 

using system;
using system.web;
using system.web.ui.webcontrols;
using system.collections.generic;

using westgarden.dal;
using westgarden.model;

namespace westgarden.web
{
    public partial class itemmanagecontrol : system.web.ui.usercontrol
    {
        private static ilist<itemdetails> itemdetails = new list<itemdetails>();

        protected void page_load(object sender, eventargs e)
        {
            if (!ispostback)
            {
                bindformview();
                viewstate["imageurl"] = null;
                viewstate["selectedcategoryid"] = null;
            }
        }

        protected void fvwitemdetails_modechanging(object sender, formviewmodeeventargs e)
        {
            switch (e.newmode)
            {
                case formviewmode.edit:
                    this.fvwitemdetails.changemode(formviewmode.edit);
                    bindformview();
                    dropdownlist ddl = (dropdownlist)fvwitemdetails.findcontrol("ddlcategories");
                    binddropdownlist(ddl);
                    break;
                case formviewmode.readonly:
                    this.fvwitemdetails.changemode(formviewmode.readonly);
                    bindformview();
                    break;
                default:
                    break;
            }
        }
        protected void fvwitemdetails_prerender(object sender, eventargs e)
        {
        }

        protected void ddlcategories_selectedindexchanged(object sender, eventargs e)
        {
            dropdownlist ddl = (dropdownlist)fvwitemdetails.findcontrol("ddlcategories");
            viewstate["selectedcategoryid"] = ddl.selectedvalue;

        }

        protected void btnupload_click(object sender, eventargs e)
        {
            fileupload fup = (fileupload)fvwitemdetails.findcontrol("fupimage");

            if (fup.hasfile)
            {
                fup.saveas(server.mappath("~/images/items/") + fup.filename);

                image img = (image)fvwitemdetails.findcontrol("imgitem");
                img.imageurl = "~/images/items/" + fup.filename.tostring();
                viewstate["imageurl"] = "~/images/items/" + fup.filename.tostring();
            }
            else
            {
                response.write("<script>alert('请先浏览并选择图片')</script>");
            }
        }

        protected void fvwitemdetails_itemupdating(object sender, formviewupdateeventargs e)
        {
            if (viewstate["imageurl"] != null)
            {
                itemdetails[0].image = viewstate["imageurl"].tostring();
            }

            if (viewstate["selectedcategoryid"] != null)
            {

                dropdownlist ddl = (dropdownlist)fvwitemdetails.findcontrol("ddlcategories");
                itemdetails[0].categoryid = viewstate["selectedcategoryid"].tostring();
            }

            textbox txtname = (textbox)fvwitemdetails.findcontrol("txtname");
            itemdetails[0].name = txtname.text;

            textbox txtprice = (textbox)fvwitemdetails.findcontrol("txtprice");
            itemdetails[0].price = decimal.parse(txtprice.text);

            textbox txtdescn = (textbox)fvwitemdetails.findcontrol("txtdescn");
            itemdetails[0].descn = txtdescn.text;

            textbox txtsupplytime = (textbox)fvwitemdetails.findcontrol("txtsupplytime");
            itemdetails[0].supplytime = txtsupplytime.text;

            textbox txtsupplydate = (textbox)fvwitemdetails.findcontrol("txtsupplydate");
            itemdetails[0].supplydate = txtsupplydate.text;

            textbox txtsupplyarea = (textbox)fvwitemdetails.findcontrol("txtsupplyarea");
            itemdetails[0].supplyarea = txtsupplyarea.text;


            item item = new item();
            item.updateitem(itemdetails[0]);

            fvwitemdetails.changemode(formviewmode.readonly);

            bindformview();
            viewstate["imageurl"] = null;
            viewstate["selectedcategoryid"] = null;
        }


        private void bindformview()
        {
            int itemkey = int.parse(request.querystring["itemid"]);
            item item = new item();
            itemdetails = item.getitemdetailsbyitemid(itemkey);
            fvwitemdetails.datasource = itemdetails;
            fvwitemdetails.databind();
        }

        private void binddropdownlist(dropdownlist ddl)
        {
            ddl.datasource = new category().getcategories();
            ddl.datatextfield = "name";
            ddl.datavaluefield = "categoryid";
            ddl.databind();

            string selectcategory = request.querystring["categoryid"].tostring();
            if (selectcategory != null)
            {
                listitem selecteditem = ddl.items.findbyvalue(selectcategory);
                if (selecteditem != null)
                    selecteditem.selected = true;
            }
        }
    }
}
 
四、数据访问层dal中的item.cs类中添加更新函数updateitem(),代码如下:

[csharp] public void updateitem(itemdetails item) 

    sqlparameter[] parms; 
    parms = new sqlparameter[] 
    { 
        new sqlparameter("@itemid",sqldbtype.int), 
        new sqlparameter("@categoryid",sqldbtype.varchar,20), 
        new sqlparameter("@name",sqldbtype.varchar,80), 
        new sqlparameter("@price",sqldbtype.decimal,10), 
        new sqlparameter("@image",sqldbtype.varchar,80), 
        new sqlparameter("@descn",sqldbtype.varchar,80), 
        new sqlparameter("@supplytime",sqldbtype.varchar,80), 
        new sqlparameter("@supplydate",sqldbtype.varchar,80), 
        new sqlparameter("@supplyarea",sqldbtype.varchar,80) 
    }; 
 
    parms[0].value = item.itemid; 
    parms[1].value = item.categoryid; 
    parms[2].value = item.name; 
    parms[3].value = item.price; 
    parms[4].value = item.image; 
    parms[5].value = item.descn; 
    parms[6].value = item.supplytime; 
    parms[7].value = item.supplydate; 
    parms[8].value = item.supplyarea; 
 
    sqlhelper.executenonquery(sqlhelper.connectionstringlocaltransaction, commandtype.text, sql_update_item, parms); 

 


作者 yousuosi

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网