`
380071587
  • 浏览: 446018 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

网上购物系统(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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics