我正在使用 VideoGames 数据库 (SQL),并且正在编写一个 ASP 项目 (VISUAL STUDIO)。该页面的编码组织不正确,并且通过重复编码调用了某些功能。我需要通过将代码组织成函数来更好地构建 C# 编码。有人可以给我一个关于如何去做的指导。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ASPLinqToSql
{
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var ctx = new VideoGamesDataContext();
var products = from p in ctx.Products
select new
{
p.ProductID,
p.ProductName,
p.ProductDescription,
p.ListPrice
};
GridView1.DataSource = products;
GridView1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int SupplierID = int.Parse(DropDownList1.SelectedValue);
var ctx = new VideoGamesDataContext();
var products = from p in ctx.Products
where p.SupplierID == SupplierID
select new
{
p.ProductID,
p.ProductName,
p.ProductDescription,
p.ListPrice
};
GridView1.DataSource = products;
GridView1.DataBind();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
if (chkLike.Checked == true)
{
int Supplierid = int.Parse(DropDownList1.SelectedValue);
var ctx = new VideoGamesDataContext();
var products = from p in ctx.Products
where p.SupplierID == Supplierid
&& p.ProductName.StartsWith(txtProductName.Text.ToString())
select new
{
p.ProductID,
p.ProductName,
p.ProductDescription,
p.ListPrice
};
GridView1.DataSource = products;
GridView1.DataBind();
}
else
{
int Supplierid = int.Parse(DropDownList1.SelectedValue);
var ctx = new VideoGamesDataContext();
var products = from p in ctx.Products
where p.SupplierID == Supplierid
select new
{
p.ProductID,
p.ProductName,
p.ProductDescription,
p.ListPrice
};
GridView1.DataSource = products;
GridView1.DataBind();
}
}