我正尽力使我的网络应用程序尽可能快,我正在寻找的一个选项是将代码缩减到每个页面。所以我有三个DDL,如下所述,他们做同样的工作来根据cookie值绑定DDL。所以我如何创建一个类并使用它来绑定所有DDL
-StateHPDDL
-BizstateHPFilterDDL
-filterstathpjob
page_load中每个DDL使用的代码如下所示
if (!IsPostBack)
{
if (cookie["Location"] == null)
{
DataTable filterstathpAdsDT = new DataTable();
using (SqlConnection filterstathpAdsCon = new SqlConnection(cs))
{
SqlCommand filterstathpAdsCMD = new SqlCommand("SELECT State FROM State WHERE Country = @Location", filterstathpAdsCon);
var Location = cookie.Value;
filterstathpAdsCMD.Parameters.AddWithValue("@Location", Location);
SqlDataAdapter filterstathpAdsAP = new SqlDataAdapter();
filterstathpAdsAP.SelectCommand = filterstathpAdsCMD;
filterstathpAdsAP.Fill(filterstathpAdsDT);
StateHPDDL.DataSource = filterstathpAdsDT;
StateHPDDL.DataTextField = "State";
StateHPDDL.DataBind();
}
StateHPDDL.Items.Insert(0, new ListItem("Select Province", ""));
}
}
如果您逐步解释,请让我更清楚地理解并遵循您的步骤,我将不胜感激
答案 0 :(得分:0)
我已经轻松解决了以下问题并且工作正常
if (!IsPostBack)
{
if (cookie["Location"] == null)
{
DataTable filterstathpBizDT = new DataTable();
using (SqlConnection filterstathpBizCon = new SqlConnection(cs))
{
SqlCommand filterstathpBizCMD = new SqlCommand("SELECT State FROM State WHERE Country = @Location", filterstathpBizCon);
var Location = cookie.Value;
filterstathpBizCMD.Parameters.AddWithValue("@Location", Location);
SqlDataAdapter filterstathpBizAP = new SqlDataAdapter();
filterstathpBizAP.SelectCommand = filterstathpBizCMD;
filterstathpBizAP.Fill(filterstathpBizDT);
BizstateHPFilterDDL.DataSource = filterstathpBizDT;
StateHPDDL.DataSource = filterstathpBizDT;
filterstathpjob.DataSource = filterstathpBizDT;
BizstateHPFilterDDL.DataTextField = "State";
StateHPDDL.DataTextField = "State";
filterstathpjob.DataTextField = "State";
BizstateHPFilterDDL.DataBind();
StateHPDDL.DataBind();
filterstathpjob.DataBind();
}
BizstateHPFilterDDL.Items.Insert(0, new ListItem("Select Province", ""));
StateHPDDL.Items.Insert(0, new ListItem("Select Province", ""));
filterstathpjob.Items.Insert(0, new ListItem("Select Province", ""));
}
}