zoukankan      html  css  js  c++  java
  • 网上购物系统(Task011)——FormView插入删除商品详细信息

    源代码:13033480群共享

    一、进入插入模板

    1protectedvoid fvwItemDetails_ModeChanging(object sender,FormViewModeEventArgs e)函数中添加代码:

    case FormViewMode.Insert:

        this.fvwItemDetails.ChangeMode(FormViewMode.Insert);

        break;

    2、此时,可进入插入模板,不过,不显示任何信息,也不能够获得下拉列表框的句柄,须添加PreRender()消息响应函数,在这个消息响应函数中添加填充下拉列表框的代码:

    protected void fvwItemDetails_PreRender(object sender,EventArgs e)

    {

        if (fvwItemDetails.CurrentMode ==FormViewMode.Insert)

        {

            DropDownList ddl = (DropDownList)fvwItemDetails.FindControl("ddlCategories");

            if (ddl != null)

            {

                BindDropDownList(ddl);

            }

        }

    }

    二、修改BindDropDownList()函数

    private void BindDropDownList(DropDownList ddl)
    {
        ddl.DataSource = new Category().GetCategories();
        ddl.DataTextField = "Name";
        ddl.DataValueField = "CategoryId";
        ddl.DataBind();
    
        if (ViewState["SelectedCategoryId"] != null)
        {
            ListItem selectedItem = ddl.Items.FindByValue(ViewState["SelectedCategoryId"].ToString());
            if (selectedItem != null)
                selectedItem.Selected = true;
        }
        else
        {
            string selectcategory = Request.QueryString["categoryId"].ToString();
            ListItem selectedItem = ddl.Items.FindByValue(selectcategory);
            if (selectedItem != null)
                selectedItem.Selected = true;
        }
    }
    


     

    三、添加消息响应函数fvwItemDetails_ItemInserting()

    protected void fvwItemDetails_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        ItemDetails itemdetails = new ItemDetails();
        if (ViewState["ImageUrl"] != null)
        {
            itemdetails.Image = ViewState["ImageUrl"].ToString();
        }
    
        if (ViewState["SelectedCategoryId"] != null)
        {
    
            DropDownList ddl = (DropDownList)fvwItemDetails.FindControl("ddlCategories");
            itemdetails.CategoryId = ViewState["SelectedCategoryId"].ToString();
        }
    
        TextBox txtname = (TextBox)fvwItemDetails.FindControl("txtName");
        itemdetails.Name = txtname.Text;
    
        TextBox txtPrice = (TextBox)fvwItemDetails.FindControl("txtPrice");
        itemdetails.Price = decimal.Parse(txtPrice.Text);
    
        TextBox txtDescn = (TextBox)fvwItemDetails.FindControl("txtDescn");
        itemdetails.Descn = txtDescn.Text;
    
        TextBox txtSupplyTime = (TextBox)fvwItemDetails.FindControl("txtSupplyTime");
        itemdetails.SupplyTime = txtSupplyTime.Text;
    
        TextBox txtSupplyDate = (TextBox)fvwItemDetails.FindControl("txtSupplyDate");
        itemdetails.SupplyDate = txtSupplyDate.Text;
    
        TextBox txtSupplyArea = (TextBox)fvwItemDetails.FindControl("txtSupplyArea");
        itemdetails.SupplyArea = txtSupplyArea.Text;
    
    
        Item item = new Item();
        item.InsertItem(itemdetails);
    
        fvwItemDetails.ChangeMode(FormViewMode.ReadOnly);
    
        BindFormView();
        ViewState["ImageUrl"] = null;
        ViewState["SelectedCategoryId"] = null;
        
    }
    


     

    四、在数据访问层DALItem.cs类中,添加InsertItem(ItemDetails item)函数

    public void InsertItem(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_INSERT_ITEM, parms);
    }
    


     

    五、FormView删除详细信息比较简单,不过,步骤和前面是一样的。

    1、添加消息响应函数

    protected void fvwItemDetails_ItemDeleting(object sender, FormViewDeleteEventArgs e)
    {
        ItemDetails itemdetails = new ItemDetails();
        itemdetails.ItemId = int.Parse(Request.QueryString["itemId"]);
        Item item = new Item();
        item.DeleteItem(itemdetails);
        Image img = (Image)fvwItemDetails.FindControl("imgItem");
        File.Delete(Server.MapPath(img.ImageUrl));  
        BindFormView();
    }
    


     

    在删除数据库数据的同时,删除了服务器端文件。

    2、在数据访问层DALItem.cs类中,添加DeleteItem(ItemDetails item)函数

    public void DeleteItem(ItemDetails item)
    {
        SqlParameter[] parms;
        parms = new SqlParameter[]
        {
             new SqlParameter("@ItemId",SqlDbType.Int)
        };
    
        parms[0].Value = item.ItemId;
    
        SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_DELETE_ITEM, parms);
    }
    


     

    六、浏览Default.aspx,查看运行结果。

    版权所有©2012,西园电脑工作室.欢迎转载,转载请注明出处.更多文章请参阅博客http://blog.csdn.com/yousuosi

  • 相关阅读:
    在使用触摸屏的情况下插拔USB鼠标,鼠标箭头消失
    使用网卡在接收数据包时不会自动组包
    linux开机发现会有个kworker进程规律性占用CPU负载超过50%
    系统时间是否可以精确到ms级别?
    linux开机进入登录界面,输入密码后屏幕黑屏3-10s,然后重新回到登录界面
    linux多网卡情况下,一个网卡进行组播,一个网卡进行点播,同时配置网关后无法通信
    linux中常见内存分配函数(kmalloc,vmalloc等)
    linux内核中的两个标记GFP_KERNEL和GFP_ATOMIC作用是什么?
    gcc: error: unrecognized argument in option ‘-mabi=aapcs-linux’
    shell脚本100例
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211703.html
Copyright © 2011-2022 走看看