Online Shopping System (Task009) -- FormView insert and delete product details

Source: Internet
Author: User

1. Enter the insert Template

1. Add code to the protectedvoid fvwItemDetails_ModeChanging (object sender, FormViewModeEventArgs e) function:

Case FormViewMode. Insert:

This. fvwItemDetails. ChangeMode (FormViewMode. Insert );

Break;

2. At this time, you can enter the insert template. However, the PreRender () Message response function must be added without displaying any information or obtaining the handle of the drop-down list box, add the code to fill the drop-down list box in the message response function:

Protected void fvwItemDetails_PreRender (object sender, EventArgs e)

{

If (fvwItemDetails. CurrentMode = FormViewMode. Insert)

{

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

If (ddl! = Null)

{

BindDropDownList (ddl );

}

}

}

Ii. Modify the BindDropDownList () function

[Csharp] 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;
}
}
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;
}
}
 

 

3. Add the message response function fvwItemDetails_ItemInserting ()

[Csharp] 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;

}
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;

}
 

 

4. Add the InsertItem (ItemDetails Item) function to the item. cs class of the data access layer DAL.

[Csharp] 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 );
}
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 );
}
 

 

5. FormView deletion details are relatively simple. However, the steps are the same as the previous steps.

1. Add a message Response Function

[Csharp] 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 ();
}
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 ();
}
 

 

While deleting database data, the server files are also deleted.

2. In the Item. cs class of the data access layer DAL, add the DeleteItem (ItemDetails item) function.

[Csharp] 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 );
}
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 );
}
 

 

6. Browse Default. aspx and view the running result.

 


Author: yousuosi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.